In Files

  • bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb

Class/Module Index [+]

Quicksearch

Bundler::ConnectionPool::TimedStack

Examples:

ts = TimedStack.new(1) { MyConnection.new }

# fetch a connection
conn = ts.pop

# return a connection
ts.push conn

conn = ts.pop
ts.pop timeout: 5
#=> raises Timeout::Error after 5 seconds

Attributes

max[R]

Public Class Methods

new(size = 0, &block) click to toggle source

Creates a new pool with size connections that are created from the given block.

 
               # File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 37
def initialize(size = 0, &block)
  @create_block = block
  @created = 0
  @que = []
  @max = size
  @mutex = Mutex.new
  @resource = ConditionVariable.new
  @shutdown_block = nil
end
            

Public Instance Methods

<<(obj, options = {}) click to toggle source
Alias for: push
empty?() click to toggle source

Returns true if there are no available connections.

 
               # File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 111
def empty?
  (@created - @que.length) >= @max
end
            
length() click to toggle source

The number of connections available on the stack.

 
               # File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 118
def length
  @max - @created + @que.length
end
            
pop(timeout = 0.5, options = {}) click to toggle source

Retrieves a connection from the stack. If a connection is available it is immediately returned. If no connection is available within the given timeout a Timeout::Error is raised.

:timeout is the only checked entry in options and is preferred over the timeout argument (which will be removed in a future release). Other options may be used by subclasses that extend TimedStack.

 
               # File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 73
def pop(timeout = 0.5, options = {})
  options, timeout = timeout, 0.5 if Hash === timeout
  timeout = options.fetch :timeout, timeout

  deadline = Bundler::ConnectionPool.monotonic_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 - Bundler::ConnectionPool.monotonic_time
      raise Timeout::Error, "Waited #{timeout} sec" if to_wait <= 0
      @resource.wait(@mutex, to_wait)
    end
  end
end
            
push(obj, options = {}) click to toggle source

Returns obj to the stack. options is ignored in TimedStack but may be used by subclasses that extend TimedStack.

 
               # File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 51
def push(obj, options = {})
  @mutex.synchronize do
    if @shutdown_block
      @shutdown_block.call(obj)
    else
      store_connection obj, options
    end

    @resource.broadcast
  end
end
            
Also aliased as: <<
shutdown(&block) click to toggle source

Shuts down the TimedStack which prevents connections from being checked out. The block is called once for each connection on the stack.

 
               # File bundler/vendor/connection_pool/lib/connection_pool/timed_stack.rb, line 97
def shutdown(&block)
  raise ArgumentError, "shutdown must receive a block" unless block_given?

  @mutex.synchronize do
    @shutdown_block = block
    @resource.broadcast

    shutdown_connections
  end
end