class WoolenCommon::ConnectionPool
Constants
- DEFAULTS
- GLOBAL_MONOTONIC_CLOCK
Clock that cannot be set and represents monotonic time since some unspecified starting point.
@!visibility private
Public Class Methods
monotonic_time()
click to toggle source
Returns the current time a tracked by the application monotonic clock.
@return [Float] The current monotonic time when `since` not given else
the elapsed monotonic time between `since` and the current time
# File lib/woolen_common/connection_pool/monotonic_time.rb, line 63 def monotonic_time GLOBAL_MONOTONIC_CLOCK.get_time end
new(options = {}, &block)
click to toggle source
# File lib/woolen_common/connection_pool.rb, line 44 def initialize(options = {}, &block) raise ArgumentError, 'Connection pool requires a block' unless block options = DEFAULTS.merge(options) @size = options.fetch(:size) @timeout = options.fetch(:timeout) @available = TimedStack.new(@size, &block) @key = :"current-#{@available.object_id}" end
wrap(options, &block)
click to toggle source
# File lib/woolen_common/connection_pool.rb, line 40 def self.wrap(options, &block) Wrapper.new(options, &block) end
Public Instance Methods
checkin()
click to toggle source
# File lib/woolen_common/connection_pool.rb, line 98 def checkin conn = pop_connection # mutates stack, must be on its own line @available.push(conn) if stack.empty? nil end
checkout(options = {})
click to toggle source
# File lib/woolen_common/connection_pool.rb, line 86 def checkout(options = {}) conn = if stack.empty? timeout = options[:timeout] || @timeout @available.pop(timeout: timeout) else stack.last end stack.push conn conn end
get_time()
click to toggle source
@!visibility private
# File lib/woolen_common/connection_pool/monotonic_time.rb, line 16 def get_time Process.clock_gettime(Process::CLOCK_MONOTONIC) end
shutdown(&block)
click to toggle source
# File lib/woolen_common/connection_pool.rb, line 105 def shutdown(&block) @available.shutdown(&block) end
with(options = {}) { |conn| ... }
click to toggle source
MRI
# File lib/woolen_common/connection_pool.rb, line 59 def with(options = {}) Thread.handle_interrupt(Exception => :never) do conn = checkout(options) begin Thread.handle_interrupt(Exception => :immediate) do yield conn end ensure checkin end end end
Private Instance Methods
pop_connection()
click to toggle source
# File lib/woolen_common/connection_pool.rb, line 111 def pop_connection if stack.empty? raise ConnectionPool::Error, 'no connections are checked out' else stack.pop end end
stack()
click to toggle source
# File lib/woolen_common/connection_pool.rb, line 119 def stack ::Thread.current[@key] ||= [] end