diff --git a/lib/connection_pool.rb b/lib/connection_pool.rb index 3f49a5a..88d8ffb 100644 --- a/lib/connection_pool.rb +++ b/lib/connection_pool.rb @@ -104,6 +104,15 @@ def shutdown(&block) @available.shutdown(&block) end + ## + # Reloads the ConnectionPool by passing each connection to +block+ and then + # removing it the pool. Subsequent checkouts will create new connections as + # needed. + + def reload(&block) + @available.shutdown(reload: true, &block) + end + # Size of this connection pool attr_reader :size diff --git a/lib/connection_pool/timed_stack.rb b/lib/connection_pool/timed_stack.rb index 78524dc..3cde991 100644 --- a/lib/connection_pool/timed_stack.rb +++ b/lib/connection_pool/timed_stack.rb @@ -83,9 +83,10 @@ def pop(timeout = 0.5, options = {}) ## # Shuts down the TimedStack by passing each connection to +block+ and then # removing it from the pool. Attempting to checkout a connection after - # shutdown will raise +ConnectionPool::PoolShuttingDownError+. + # shutdown will raise +ConnectionPool::PoolShuttingDownError+ unless + # +:reload+ is +true+. - def shutdown(&block) + def shutdown(reload: false, &block) raise ArgumentError, "shutdown must receive a block" unless block_given? @mutex.synchronize do @@ -94,6 +95,7 @@ def shutdown(&block) shutdown_connections end + @shutdown_block = nil if reload end ## @@ -144,6 +146,7 @@ def shutdown_connections(options = nil) conn = fetch_connection(options) @shutdown_block.call(conn) end + @created = 0 end ## diff --git a/test/test_connection_pool_timed_stack.rb b/test/test_connection_pool_timed_stack.rb index d8742de..6a9c6d9 100644 --- a/test/test_connection_pool_timed_stack.rb +++ b/test/test_connection_pool_timed_stack.rb @@ -102,6 +102,16 @@ def test_pop_shutdown end end + def test_pop_shutdown_reload + stack = ConnectionPool::TimedStack.new(1) { Object.new } + object = stack.pop + stack.push(object) + + stack.shutdown(reload: true) {} + + refute_equal object, stack.pop + end + def test_push stack = ConnectionPool::TimedStack.new(1) { Object.new }