From df819923f2262f6448f1cf39a96d6bbec0ff241e Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Mon, 26 Sep 2022 13:19:08 +0200 Subject: [PATCH] Expose `ConnectionPool#reload` Ref: https://github.com/mperham/connection_pool/pull/140 When forking a process, you need to close existing connection to avoid sharing them across processes. `shutdown` does that, but it also mark the pool as no longer usable. `connection_pool` `2.2.4` introduced `#reload` that discard existing connections, but let the pool be used again later. It's a much better fit for an `after_fork` callback. --- lib/net/http/persistent.rb | 14 +++++++++++++- net-http-persistent.gemspec | 2 +- test/test_net_http_persistent.rb | 11 +++++++++++ test/test_net_http_persistent_timed_stack_multi.rb | 2 +- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/net/http/persistent.rb b/lib/net/http/persistent.rb index 420f0d0..4658650 100644 --- a/lib/net/http/persistent.rb +++ b/lib/net/http/persistent.rb @@ -947,7 +947,8 @@ def request_setup req_or_uri # :nodoc: end ## - # Shuts down all connections + # Shuts down all connections. Attempting to checkout a connection after + # shutdown will raise an error. # # *NOTE*: Calling shutdown for can be dangerous! # @@ -958,6 +959,17 @@ def shutdown @pool.shutdown { |http| http.finish } end + ## + # Discard all existing connections. Subsequent checkouts will create + # new connections as needed. + # + # If any thread is still using a connection it may cause an error! Call + # #reload when you are completely done making requests! + + def reload + @pool.reload { |http| http.finish } + end + ## # Enables SSL on +connection+ diff --git a/net-http-persistent.gemspec b/net-http-persistent.gemspec index a32fedf..5eb683f 100644 --- a/net-http-persistent.gemspec +++ b/net-http-persistent.gemspec @@ -17,6 +17,6 @@ Gem::Specification.new do |s| s.required_ruby_version = ">= 2.4".freeze s.summary = "Manages persistent connections using Net::HTTP including a thread pool for connecting to multiple hosts".freeze - s.add_runtime_dependency(%q.freeze, ["~> 2.2"]) + s.add_runtime_dependency(%q.freeze, ["~> 2.2", ">= 2.2.4"]) end diff --git a/test/test_net_http_persistent.rb b/test/test_net_http_persistent.rb index 6746f77..ecd5426 100644 --- a/test/test_net_http_persistent.rb +++ b/test/test_net_http_persistent.rb @@ -1244,6 +1244,17 @@ def test_shutdown refute c2.http.finished?, 'present generation connection must not be finished' end + def test_reload + c = connection + + @http.reload + + c2 = connection + + assert c.http.finished?, 'last-generation connection must be finished' + refute c2.http.finished?, 'present generation connection must not be finished' + end + def test_ssl skip 'OpenSSL is missing' unless HAVE_OPENSSL diff --git a/test/test_net_http_persistent_timed_stack_multi.rb b/test/test_net_http_persistent_timed_stack_multi.rb index ae3e34b..834d0ae 100644 --- a/test/test_net_http_persistent_timed_stack_multi.rb +++ b/test/test_net_http_persistent_timed_stack_multi.rb @@ -57,7 +57,7 @@ def test_pop_empty @stack.pop timeout: 0 end - assert_equal 'Waited 0 sec', e.message + assert_includes e.message, 'Waited 0 sec' end def test_pop_full