Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions lib/net/http/persistent/timed_stack_multi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def length
@max - @created + @enqueued
end

def idle
@enqueued
end

private

def connection_stored? options = {} # :nodoc:
Expand All @@ -38,7 +42,8 @@ def fetch_connection options = {} # :nodoc:

@enqueued -= 1
lru_update connection_args
@ques[connection_args].pop
connection, = @ques[connection_args].pop
connection
end

def lru_update connection_args # :nodoc:
Expand All @@ -53,7 +58,7 @@ def shutdown_connections # :nodoc:
end

def store_connection obj, options = {} # :nodoc:
@ques[options[:connection_args]].push obj
@ques[options[:connection_args]].push [obj, current_time]
@enqueued += 1
end

Expand All @@ -63,7 +68,7 @@ def try_create options = {} # :nodoc:
if @created >= @max && @enqueued >= 1
oldest, = @lru.first
@lru.delete oldest
connection = @ques[oldest].pop
connection, = @ques[oldest].pop
connection.close if connection.respond_to?(:close)

@created -= 1
Expand All @@ -76,5 +81,21 @@ def try_create options = {} # :nodoc:
end
end

def reserve_idle_connection(idle_seconds)
@ques.each_value do |que|
next if que.empty?

if current_time - que.first.last > idle_seconds
@created -= 1
@enqueued -= 1

connection, = que.shift
return connection
end
end

nil
end

end

36 changes: 36 additions & 0 deletions test/test_net_http_persistent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1530,4 +1530,40 @@ def test_connection_pool_after_fork
connection.close
end
end

def test_reap_pool_removes_idle_connections
assert_equal 0, @http.pool.idle

connection
@http.request(@uri)

assert_equal 1, @http.pool.idle

@http.pool.reap(0) {}
assert_equal 0, @http.pool.idle
end

def test_reap_pool_removes_all_idle_connections
connection
connection @uri_v6

@http.request(@uri)
@http.request(@uri_v6)

assert_equal 2, @http.pool.idle

@http.pool.reap(0) {}
assert_equal 0, @http.pool.idle
end

def test_reap_pool_does_not_remove_connections_if_outside_idle_time
connection
@http.request(@uri)

assert_equal 1, @http.pool.idle

@http.pool.reap(1000) {}
assert_equal 1, @http.pool.idle
end

end
Loading