class Bundler::ConnectionPool::TimedStack

def connection_stored?(options = nil)

def connection_stored?(options = nil)
  !@que.empty?
end

def current_time

def current_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

def empty?

def empty?
  (@created - @que.length) >= @max
end

def fetch_connection(options = nil)

def fetch_connection(options = nil)
  @que.pop
end

def initialize(size = 0, &block)

def initialize(size = 0, &block)
  @create_block = block
  @created = 0
  @que = []
  @max = size
  @mutex = Thread::Mutex.new
  @resource = Thread::ConditionVariable.new
  @shutdown_block = nil
end

def length

def length
  @max - @created + @que.length
end

def pop(timeout = 0.5, options = {})

def pop(timeout = 0.5, options = {})
  options, timeout = timeout, 0.5 if Hash === timeout
  timeout = options.fetch :timeout, timeout
  deadline = current_time + timeout
  @mutex.synchronize do
    loop do
      raise Bundler::ConnectionPool::PoolShuttingDownError if @shutdown_block
      return fetch_connection(options) if connection_stored?(options)
      connection = try_create(options)
      return connection if connection
      to_wait = deadline - current_time
      raise Bundler::ConnectionPool::TimeoutError, "Waited #{timeout} sec, #{length}/#{@max} available" if to_wait <= 0
      @resource.wait(@mutex, to_wait)
    end
  end
end

def push(obj, options = {})

def push(obj, options = {})
  @mutex.synchronize do
    if @shutdown_block
      @shutdown_block.call(obj)
    else
      store_connection obj, options
    end
    @resource.broadcast
  end
end

def shutdown(reload: false, &block)

def shutdown(reload: false, &block)
  raise ArgumentError, "shutdown must receive a block" unless block
  @mutex.synchronize do
    @shutdown_block = block
    @resource.broadcast
    shutdown_connections
    @shutdown_block = nil if reload
  end
end

def shutdown_connections(options = nil)

def shutdown_connections(options = nil)
  while connection_stored?(options)
    conn = fetch_connection(options)
    @shutdown_block.call(conn)
  end
  @created = 0
end

def store_connection(obj, options = nil)

def store_connection(obj, options = nil)
  @que.push obj
end

def try_create(options = nil)

def try_create(options = nil)
  unless @created == @max
    object = @create_block.call
    @created += 1
    object
  end
end