From 1dcdf06f111227f0396afd9b12ab94bea2c636e2 Mon Sep 17 00:00:00 2001 From: Dylan Thacker-Smith Date: Thu, 9 Jan 2014 00:24:39 -0500 Subject: [PATCH] Set connection.keep_alive_timeout using idle_timeout in ruby 2.0. 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. --- lib/net/http/persistent.rb | 1 + test/test_net_http_persistent.rb | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/net/http/persistent.rb b/lib/net/http/persistent.rb index f99f062..8d7fedf 100644 --- a/lib/net/http/persistent.rb +++ b/lib/net/http/persistent.rb @@ -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 diff --git a/test/test_net_http_persistent.rb b/test/test_net_http_persistent.rb index 2599743..7621aae 100644 --- a/test/test_net_http_persistent.rb +++ b/test/test_net_http_persistent.rb @@ -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 @@ -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']