From 1a3a6dee6488a5ed481485b7e4bbb1ddad8e70ce Mon Sep 17 00:00:00 2001 From: Andrew Marshall Date: Tue, 15 Dec 2020 14:40:42 -0500 Subject: [PATCH] Allow restarting pool The implementation of shutdown from https://github.com/mperham/connection_pool/issues/27 does not actually provide for a way for the pool to re-create connections, only render the pool unusable. This implements such a behavior. A new method is added so as to not change the existing behavior of `shutdown`. --- lib/connection_pool.rb | 4 ++++ lib/connection_pool/timed_stack.rb | 12 ++++++++++++ test/test_connection_pool_timed_stack.rb | 10 ++++++++++ 3 files changed, 26 insertions(+) diff --git a/lib/connection_pool.rb b/lib/connection_pool.rb index 4ce44e6..4072cab 100644 --- a/lib/connection_pool.rb +++ b/lib/connection_pool.rb @@ -99,6 +99,10 @@ def shutdown(&block) @available.shutdown(&block) end + def restart(&block) + @available.restart(&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 ad44070..cdcf7e2 100644 --- a/lib/connection_pool/timed_stack.rb +++ b/lib/connection_pool/timed_stack.rb @@ -95,6 +95,17 @@ def shutdown(&block) end end + ## + # Like +shutdown+, but will allow the pool to re-create connections + # afterwards. + + def restart(&block) + raise ArgumentError, "restart must receive a block" unless block_given? + + shutdown(&block) + @shutdown_block = nil + end + ## # Returns +true+ if there are no available connections. @@ -143,6 +154,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..1d47243 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_restart + stack = ConnectionPool::TimedStack.new(1) { Object.new } + object = stack.pop + stack.push(object) + + stack.restart {} + + refute_equal object, stack.pop + end + def test_push stack = ConnectionPool::TimedStack.new(1) { Object.new }