Skip to content
Merged
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
20 changes: 18 additions & 2 deletions lib/net/http/persistent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,13 @@
# The amount of time allowed between reading two chunks from the socket. Set
# through #read_timeout
#
# === Max Requests
#
# The number of requests that should be made before opening a new connection.
# Typically many keep-alive capable servers tune this to 100 or less, so the
# 101st request will fail with ECONNRESET. If unset (default), this value has no
# effect, if set, connections will be reset on the request after max_requests.
#
# === Open Timeout
#
# The amount of time to wait for a connection to be opened. Set through
Expand Down Expand Up @@ -322,6 +329,12 @@ def self.detect_idle_timeout uri, max = 10

attr_accessor :idle_timeout

##
# Maximum number of requests on a connection before it is considered expired
# and automatically closed.

attr_accessor :max_requests

##
# The value sent in the Keep-Alive header. Defaults to 30. Not needed for
# HTTP/1.1 servers.
Expand Down Expand Up @@ -477,6 +490,7 @@ def initialize name = nil, proxy = nil
@open_timeout = nil
@read_timeout = nil
@idle_timeout = 5
@max_requests = nil
@socket_options = []

@socket_options << [Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1] if
Expand Down Expand Up @@ -651,10 +665,12 @@ def escape str
end

##
# Returns true if the connection should be reset due to an idle timeout,
# false otherwise.
# Returns true if the connection should be reset due to an idle timeout, or
# maximum request count, false otherwise.

def expired? connection
requests = Thread.current[@request_key][connection.object_id]
return true if @max_requests && @max_requests >= requests
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be requests >= @max_requests, I believe

return false unless @idle_timeout
return true if @idle_timeout.zero?

Expand Down
15 changes: 15 additions & 0 deletions test/test_net_http_persistent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ def test_escape

def test_expired_eh
c = basic_connection
reqs[c.object_id] = 0
touts[c.object_id] = Time.now - 11

@http.idle_timeout = 0
Expand All @@ -664,6 +665,20 @@ def test_expired_eh
refute @http.expired? c
end

def test_expired_due_to_max_requests
c = basic_connection
reqs[c.object_id] = 0
touts[c.object_id] = Time.now

refute @http.expired? c

reqs[c.object_id] = 10
refute @http.expired? c

@http.max_requests = 10
assert @http.expired? c
end

def test_finish
c = basic_connection
reqs[c.object_id] = 5
Expand Down