Skip to content

Commit 0a013de

Browse files
committed
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.
1 parent 4f2c419 commit 0a013de

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/net/http.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,7 @@ def connect
10481048
write_timeout: @write_timeout,
10491049
continue_timeout: @continue_timeout,
10501050
debug_output: @debug_output)
1051+
@last_communicated = nil
10511052
on_connect
10521053
rescue => exception
10531054
if s

test/net/http/test_http.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,30 @@ def test_keep_alive_get_auto_retry
11411141
}
11421142
end
11431143

1144+
def test_keep_alive_reset_on_new_connection
1145+
# Using WEBrick's debug log output on accepting connection:
1146+
#
1147+
# "[2021-04-29 20:36:46] DEBUG accept: 127.0.0.1:50674\n"
1148+
@log_tester = nil
1149+
@server.logger.level = WEBrick::BasicLog::DEBUG
1150+
1151+
start {|http|
1152+
res = http.get('/')
1153+
http.keep_alive_timeout = 1
1154+
assert_kind_of Net::HTTPResponse, res
1155+
assert_kind_of String, res.body
1156+
http.finish
1157+
assert_equal 1, @log.grep(/accept/i).size
1158+
1159+
sleep 1.5
1160+
http.start
1161+
res = http.get('/')
1162+
assert_kind_of Net::HTTPResponse, res
1163+
assert_kind_of String, res.body
1164+
assert_equal 2, @log.grep(/accept/i).size
1165+
}
1166+
end
1167+
11441168
class MockSocket
11451169
attr_reader :count
11461170
def initialize(success_after: nil)

0 commit comments

Comments
 (0)