Skip to content

Commit

Permalink
Set connection.keep_alive_timeout using idle_timeout in ruby 2.0.
Browse files Browse the repository at this point in the history
Ruby 2.0 added keep_alive_timeout, which behaves the same way as
idle_timeout, except that the default timeout is 5 seconds rather than 2
seconds. Set keep_alive_timeout using idle_timeout to prevent the
connection from getting closed before idle_timeout due to delays between
requests.
  • Loading branch information
dylanahsmith committed Jan 9, 2014
1 parent 7dcbf7c commit 1dcdf06
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/net/http/persistent.rb
Expand Up @@ -631,6 +631,7 @@ def connection_for uri
start connection unless connection.started?

connection.read_timeout = @read_timeout if @read_timeout
connection.keep_alive_timeout = @idle_timeout if @idle_timeout && connection.respond_to?(:keep_alive_timeout=)

connection
rescue Errno::ECONNREFUSED
Expand Down
2 changes: 2 additions & 0 deletions test/test_net_http_persistent.rb
Expand Up @@ -274,6 +274,7 @@ def test_certificate_equals
def test_connection_for
@http.open_timeout = 123
@http.read_timeout = 321
@http.idle_timeout = 42
c = @http.connection_for @uri

assert_kind_of @http_class, c
Expand All @@ -283,6 +284,7 @@ def test_connection_for

assert_equal 123, c.open_timeout
assert_equal 321, c.read_timeout
assert_equal 42, c.keep_alive_timeout unless RUBY_1

assert_includes conns[0].keys, 'example.com:80'
assert_same c, conns[0]['example.com:80']
Expand Down

0 comments on commit 1dcdf06

Please sign in to comment.