1
0
Fork 0
mirror of https://github.com/mperham/connection_pool synced 2023-03-27 23:22:21 -04:00

Pass options through TimedStack methods to hooks

This allows the hooks to do arbitrary things based on options (keyword
arguments) such as creating a particular type of connection based on,
say, connection_args.

The ** syntax for arbitrary options is not used as it is not supported
on ruby 1.9.
This commit is contained in:
Eric Hodel 2014-03-18 20:45:08 -07:00
parent 821a72afcb
commit 97f42e8b41
3 changed files with 15 additions and 14 deletions

View file

@ -66,7 +66,7 @@ class ConnectionPool
if stack.empty?
timeout = options[:timeout] || @timeout
conn = @available.pop(timeout)
conn = @available.pop(timeout: timeout)
else
conn = stack.last
end

View file

@ -15,12 +15,12 @@ class ConnectionPool::TimedStack
@shutdown_block = nil
end
def push(obj)
def push(obj, options = {})
@mutex.synchronize do
if @shutdown_block
@shutdown_block.call(obj)
else
store_connection obj
store_connection obj, options
end
@resource.broadcast
@ -28,14 +28,15 @@ class ConnectionPool::TimedStack
end
alias_method :<<, :push
def pop(timeout=0.5)
def pop(options = {})
timeout = options.fetch :timeout, 0.5
deadline = Time.now + timeout
@mutex.synchronize do
loop do
raise ConnectionPool::PoolShuttingDownError if @shutdown_block
return fetch_connection if connection_stored?
return fetch_connection(options) if connection_stored?(options)
connection = try_create
connection = try_create(options)
return connection if connection
to_wait = deadline - Time.now
@ -66,26 +67,26 @@ class ConnectionPool::TimedStack
private
def connection_stored? # :nodoc:
def connection_stored?(options = nil) # :nodoc:
!@que.empty?
end
def fetch_connection # :nodoc:
def fetch_connection(options = nil) # :nodoc:
@que.pop
end
def shutdown_connections # :nodoc:
while connection_stored?
conn = fetch_connection
def shutdown_connections(options = nil) # :nodoc:
while connection_stored?(options)
conn = fetch_connection(options)
@shutdown_block.call(conn)
end
end
def store_connection obj # :nodoc:
def store_connection(obj, options = nil) # :nodoc:
@que.push obj
end
def try_create # :nodoc:
def try_create(options = nil) # :nodoc:
unless @created == @max
@created += 1
@create_block.call

View file

@ -45,7 +45,7 @@ class TestConnectionPoolTimedStack < Minitest::Test
def test_pop_empty
e = assert_raises Timeout::Error do
@stack.pop 0
@stack.pop timeout: 0
end
assert_equal 'Waited 0 sec', e.message