class HealthyPools::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
Public Class Methods
Creates a new pool with size
connections that are created from the given block
.
# File lib/healthy_pools/timed_stack.rb, line 37 def initialize(size = 0, health_check: nil, &block) @create_block = block @health_check = health_check @created = 0 @que = [] @max = size @mutex = Mutex.new @resource = ConditionVariable.new @shutdown_block = nil end
Public Instance Methods
Removes conn
from the stack. This is useful if connections die or otherwise become stale.
# File lib/healthy_pools/timed_stack.rb, line 52 def delete(conn) if @mutex.owned? @created -= 1 else @mutex.synchronize do @created -= 1 end end end
Returns true
if there are no available connections.
# File lib/healthy_pools/timed_stack.rb, line 136 def empty? (@created - @que.length) >= @max end
The number of connections available on the stack.
# File lib/healthy_pools/timed_stack.rb, line 143 def length @max - @created + @que.length end
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/healthy_pools/timed_stack.rb, line 88 def pop(timeout = 0.5, options = {}) options, timeout = timeout, 0.5 if Hash === timeout timeout = options.fetch :timeout, timeout deadline = HealthyPools.monotonic_time + timeout @mutex.synchronize do loop do raise HealthyPools::PoolShuttingDownError if @shutdown_block while connection_stored?(options) conn = fetch_connection(options) begin if @health_check.nil? || @health_check.call(conn) return conn end rescue end # Health check has failed, delete conn and retry. delete(conn) end connection = try_create(options) return connection if connection to_wait = deadline - HealthyPools.monotonic_time raise Timeout::Error, "Waited #{timeout} sec" if to_wait <= 0 @resource.wait(@mutex, to_wait) end end end
Returns obj
to the stack. options
is ignored in TimedStack
but may be used by subclasses that extend TimedStack
.
# File lib/healthy_pools/timed_stack.rb, line 66 def push(obj, options = {}) @mutex.synchronize do if @shutdown_block @shutdown_block.call(obj) else store_connection obj, options end @resource.broadcast end end
Shuts down the TimedStack
which prevents connections from being checked out. The block
is called once for each connection on the stack.
# File lib/healthy_pools/timed_stack.rb, line 122 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
Private Instance Methods
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/healthy_pools/timed_stack.rb, line 154 def connection_stored?(options = nil) !@que.empty? end
This is an extension point for TimedStack
and is called with a mutex.
This method must return a connection from the stack.
# File lib/healthy_pools/timed_stack.rb, line 163 def fetch_connection(options = nil) @que.pop end
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/healthy_pools/timed_stack.rb, line 172 def shutdown_connections(options = nil) while connection_stored?(options) conn = fetch_connection(options) @shutdown_block.call(conn) end end
This is an extension point for TimedStack
and is called with a mutex.
This method must return obj
to the stack.
# File lib/healthy_pools/timed_stack.rb, line 184 def store_connection(obj, options = nil) @que.push obj end
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/healthy_pools/timed_stack.rb, line 194 def try_create(options = nil) unless @created == @max object = @create_block.call @created += 1 object end end