Skip to content

Commit d89057a

Browse files
authored
Merge pull request #98 from drbrain/memory-leak
fix memory leak introduced by connection pooling
2 parents d4ebd9a + 5be3611 commit d89057a

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

lib/net/http/persistent.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,9 +1048,7 @@ def request_setup req_or_uri # :nodoc:
10481048
# #shutdown when you are completely done making requests!
10491049

10501050
def shutdown
1051-
@pool.available.shutdown do |http|
1052-
http.finish
1053-
end
1051+
@pool.shutdown { |http| http.finish }
10541052
end
10551053

10561054
##

lib/net/http/persistent/pool.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ def initialize(options = {}, &block)
77
super
88

99
@available = Net::HTTP::Persistent::TimedStackMulti.new(@size, &block)
10-
@key = :"current-#{@available.object_id}"
10+
@key = "current-#{@available.object_id}"
1111
end
1212

1313
def checkin net_http_args
14-
stack = Thread.current[@key][net_http_args]
14+
stack = Thread.current[@key][net_http_args] ||= []
1515

1616
raise ConnectionPool::Error, 'no connections are checked out' if
1717
stack.empty?
@@ -26,8 +26,8 @@ def checkin net_http_args
2626
end
2727

2828
def checkout net_http_args
29-
stacks = Thread.current[@key] ||= Hash.new { |h, k| h[k] = [] }
30-
stack = stacks[net_http_args]
29+
stacks = Thread.current[@key] ||= {}
30+
stack = stacks[net_http_args] ||= []
3131

3232
if stack.empty? then
3333
conn = @available.pop connection_args: net_http_args
@@ -40,6 +40,10 @@ def checkout net_http_args
4040
conn
4141
end
4242

43+
def shutdown
44+
Thread.current[@key] = nil
45+
super
46+
end
4347
end
4448

4549
require 'net/http/persistent/timed_stack_multi'

0 commit comments

Comments
 (0)