Skip to content

Commit

Permalink
Merge pull request #152 from fatkodima/close-discarded-connections
Browse files Browse the repository at this point in the history
Close discarded connections
  • Loading branch information
tenderlove authored Dec 4, 2024
2 parents 2cd8bec + 51feefe commit a4d93fd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/net/http/persistent/timed_stack_multi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ def try_create options = {} # :nodoc:
if @created >= @max && @enqueued >= 1
oldest, = @lru.first
@lru.delete oldest
@ques[oldest].pop
connection = @ques[oldest].pop
connection.close if connection.respond_to?(:close)

@created -= 1
end
Expand Down
19 changes: 18 additions & 1 deletion test/test_net_http_persistent_timed_stack_multi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
class TestNetHttpPersistentTimedStackMulti < Minitest::Test

class Connection
attr_reader :host
attr_reader :host, :closed

def initialize(host)
@host = host
@closed = false
end

def close
@closed = true
end
end

Expand Down Expand Up @@ -69,6 +74,18 @@ def test_pop_full
assert_empty stack
end

def test_pop_closes_extra_connections
stack = Net::HTTP::Persistent::TimedStackMulti.new(1) { |host| Connection.new(host) }

a_conn = stack.pop connection_args: 'a.example'
stack.push a_conn, connection_args: 'a.example'

b_conn = stack.pop connection_args: 'b.example'

assert a_conn.closed
refute b_conn.closed
end

def test_pop_wait
thread = Thread.start do
@stack.pop
Expand Down

0 comments on commit a4d93fd

Please sign in to comment.