Skip to content

Commit df81992

Browse files
committed
Expose ConnectionPool#reload
Ref: mperham/connection_pool#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.
1 parent 857c3ba commit df81992

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

lib/net/http/persistent.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,8 @@ def request_setup req_or_uri # :nodoc:
947947
end
948948

949949
##
950-
# Shuts down all connections
950+
# Shuts down all connections. Attempting to checkout a connection after
951+
# shutdown will raise an error.
951952
#
952953
# *NOTE*: Calling shutdown for can be dangerous!
953954
#
@@ -958,6 +959,17 @@ def shutdown
958959
@pool.shutdown { |http| http.finish }
959960
end
960961

962+
##
963+
# Discard all existing connections. Subsequent checkouts will create
964+
# new connections as needed.
965+
#
966+
# If any thread is still using a connection it may cause an error! Call
967+
# #reload when you are completely done making requests!
968+
969+
def reload
970+
@pool.reload { |http| http.finish }
971+
end
972+
961973
##
962974
# Enables SSL on +connection+
963975

net-http-persistent.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ Gem::Specification.new do |s|
1717
s.required_ruby_version = ">= 2.4".freeze
1818
s.summary = "Manages persistent connections using Net::HTTP including a thread pool for connecting to multiple hosts".freeze
1919

20-
s.add_runtime_dependency(%q<connection_pool>.freeze, ["~> 2.2"])
20+
s.add_runtime_dependency(%q<connection_pool>.freeze, ["~> 2.2", ">= 2.2.4"])
2121
end
2222

test/test_net_http_persistent.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1244,6 +1244,17 @@ def test_shutdown
12441244
refute c2.http.finished?, 'present generation connection must not be finished'
12451245
end
12461246

1247+
def test_reload
1248+
c = connection
1249+
1250+
@http.reload
1251+
1252+
c2 = connection
1253+
1254+
assert c.http.finished?, 'last-generation connection must be finished'
1255+
refute c2.http.finished?, 'present generation connection must not be finished'
1256+
end
1257+
12471258
def test_ssl
12481259
skip 'OpenSSL is missing' unless HAVE_OPENSSL
12491260

test/test_net_http_persistent_timed_stack_multi.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def test_pop_empty
5757
@stack.pop timeout: 0
5858
end
5959

60-
assert_equal 'Waited 0 sec', e.message
60+
assert_includes e.message, 'Waited 0 sec'
6161
end
6262

6363
def test_pop_full

0 commit comments

Comments
 (0)