[前][次][番号順一覧][スレッド一覧]

ruby-changes:69581

From: Kazuki <ko1@a...>
Date: Thu, 4 Nov 2021 20:44:16 +0900 (JST)
Subject: [ruby-changes:69581] 5f2c4e344d (master): [ruby/net-http] Reset keep_alive timer on new connection

https://git.ruby-lang.org/ruby.git/commit/?id=5f2c4e344d

From 5f2c4e344dc2f19aab54523ae418800b08adaa61 Mon Sep 17 00:00:00 2001
From: Kazuki Yamaguchi <k@r...>
Date: Thu, 29 Apr 2021 20:42:33 +0900
Subject: [ruby/net-http] Reset keep_alive timer on new connection

The last_communicated timestamp is for HTTP persistent connection, to
decide whether the current TCP connection may be reused for the
subsequent requests or not. Naturally, the timer must be reset if the
connection is recreated since it is no longer relevant.

https://github.com/ruby/net-http/commit/0a013de42d
---
 lib/net/http.rb            |  1 +
 test/net/http/test_http.rb | 24 ++++++++++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/lib/net/http.rb b/lib/net/http.rb
index aaf3e3104da..552f818e5dc 100644
--- a/lib/net/http.rb
+++ b/lib/net/http.rb
@@ -1055,6 +1055,7 @@ module Net   #:nodoc: https://github.com/ruby/ruby/blob/trunk/lib/net/http.rb#L1055
                                write_timeout: @write_timeout,
                                continue_timeout: @continue_timeout,
                                debug_output: @debug_output)
+      @last_communicated = nil
       on_connect
     rescue => exception
       if s
diff --git a/test/net/http/test_http.rb b/test/net/http/test_http.rb
index c8590219560..b5156078a44 100644
--- a/test/net/http/test_http.rb
+++ b/test/net/http/test_http.rb
@@ -1168,6 +1168,30 @@ class TestNetHTTPKeepAlive < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/net/http/test_http.rb#L1168
     }
   end
 
+  def test_keep_alive_reset_on_new_connection
+    # Using WEBrick's debug log output on accepting connection:
+    #
+    #   "[2021-04-29 20:36:46] DEBUG accept: 127.0.0.1:50674\n"
+    @log_tester = nil
+    @server.logger.level = WEBrick::BasicLog::DEBUG
+
+    start {|http|
+      res = http.get('/')
+      http.keep_alive_timeout = 1
+      assert_kind_of Net::HTTPResponse, res
+      assert_kind_of String, res.body
+      http.finish
+      assert_equal 1, @log.grep(/accept/i).size
+
+      sleep 1.5
+      http.start
+      res = http.get('/')
+      assert_kind_of Net::HTTPResponse, res
+      assert_kind_of String, res.body
+      assert_equal 2, @log.grep(/accept/i).size
+    }
+  end
+
   class MockSocket
     attr_reader :count
     def initialize(success_after: nil)
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]