From 97f42e8b41539f0b24787d71a00f533e4180d755 Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Tue, 18 Mar 2014 20:45:08 -0700 Subject: [PATCH] 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. --- lib/connection_pool.rb | 2 +- lib/connection_pool/timed_stack.rb | 25 ++++++++++++------------ test/test_connection_pool_timed_stack.rb | 2 +- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/connection_pool.rb b/lib/connection_pool.rb index 82f9efb..2ecd25c 100644 --- a/lib/connection_pool.rb +++ b/lib/connection_pool.rb @@ -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 diff --git a/lib/connection_pool/timed_stack.rb b/lib/connection_pool/timed_stack.rb index a145217..aab517b 100644 --- a/lib/connection_pool/timed_stack.rb +++ b/lib/connection_pool/timed_stack.rb @@ -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 diff --git a/test/test_connection_pool_timed_stack.rb b/test/test_connection_pool_timed_stack.rb index 2ceeec9..461c245 100644 --- a/test/test_connection_pool_timed_stack.rb +++ b/test/test_connection_pool_timed_stack.rb @@ -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