class EzPool::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

Public Class Methods

new(connection_manager, size = 0) click to toggle source

Creates a new pool with size connections that are created by constructing the given connection_wrapper class

# File lib/ezpool/timed_stack.rb, line 37
def initialize(connection_manager, size = 0)
  @created = 0
  @que = []
  @max = size
  @mutex = Mutex.new
  @resource = ConditionVariable.new
  @connection_manager = connection_manager
  @shutting_down = false
end

Public Instance Methods

<<(wrapper, options = {})
Alias for: push
abandon(connection_wrapper) click to toggle source

Mark a connection as abandoned so that it cannot be used again. Will call the pre-configured shutdown proc, if provided.

# File lib/ezpool/timed_stack.rb, line 97
def abandon(connection_wrapper)
  @mutex.synchronize do
    connection_wrapper.shutdown!
    @created -= 1
  end
end
add_one() click to toggle source

Add one connection to the queue

Returns true iff a connection was successfully created

# File lib/ezpool/timed_stack.rb, line 143
def add_one
  connection = try_create
  if connection.nil?
    false
  else
    push(connection)
    true
  end
end
empty?() click to toggle source

Returns true if there are no available connections.

# File lib/ezpool/timed_stack.rb, line 121
def empty?
  (@created - @que.length) >= @max
end
fill() click to toggle source

Pre-create all possible connections

# File lib/ezpool/timed_stack.rb, line 134
def fill
  while add_one
  end
end
length() click to toggle source

The number of connections available on the stack.

# File lib/ezpool/timed_stack.rb, line 128
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 lib/ezpool/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 = EzPool.monotonic_time + timeout
  @mutex.synchronize do
    loop do
      raise EzPool::PoolShuttingDownError if @shutting_down
      return fetch_connection(options) if connection_stored?(options)

      connection = try_create(options)
      return connection if connection

      to_wait = deadline - EzPool.monotonic_time
      raise Timeout::Error, "Waited #{timeout} sec" if to_wait <= 0
      @resource.wait(@mutex, to_wait)
    end
  end
end
push(wrapper, 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 lib/ezpool/timed_stack.rb, line 51
def push(wrapper, options = {})
  @mutex.synchronize do
    if @shutting_down
      wrapper.shutdown!
    else
      store_connection wrapper, options
    end

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

Shuts down the TimedStack which prevents connections from being checked out. Calls the shutdown program specified in the EzPool initializer

# File lib/ezpool/timed_stack.rb, line 109
def shutdown()
  @mutex.synchronize do
    @shutting_down = true
    @resource.broadcast

    shutdown_connections
  end
end

Private Instance Methods

connection_stored?(options = nil) click to toggle source

This is an extension point for TimedStack and is called with a mutex.

This method must returns true if a connection is available on the stack.

# File lib/ezpool/timed_stack.rb, line 160
def connection_stored?(options = nil)
  !@que.empty?
end
fetch_connection(options = nil) click to toggle source

This is an extension point for TimedStack and is called with a mutex.

This method must return a connection from the stack.

# File lib/ezpool/timed_stack.rb, line 169
def fetch_connection(options = nil)
  @que.pop
end
shutdown_connections(options = nil) click to toggle source

This is an extension point for TimedStack and is called with a mutex.

This method must shut down all connections on the stack.

# File lib/ezpool/timed_stack.rb, line 178
def shutdown_connections(options = nil)
  while connection_stored?(options)
    conn = fetch_connection(options)
    conn.shutdown!
  end
end
store_connection(obj, options = nil) click to toggle source

This is an extension point for TimedStack and is called with a mutex.

This method must return obj to the stack.

# File lib/ezpool/timed_stack.rb, line 190
def store_connection(obj, options = nil)
  @que.push obj
end
try_create(options = nil) click to toggle source

This is an extension point for TimedStack and is called with a mutex.

This method must create a connection if and only if the total number of connections allowed has not been met.

# File lib/ezpool/timed_stack.rb, line 200
def try_create(options = nil)
  unless @created == @max
    object = @connection_manager.create_new()
    @created += 1
    object
  end
end