Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixup
  • Loading branch information
ronag committed Jul 30, 2020
commit 8317a384589225035e52b41a806b9e2952e5f721
5 changes: 3 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,9 +442,10 @@ class Parser extends HTTPParser {
if (headers['keep-alive']) {
const m = headers['keep-alive'].match(/timeout=(\d+)/)
if (m) {
const keepAliveTimeout = Number(m[1]) * 1000
let keepAliveTimeout = Number(m[1]) * 1000
keepAliveTimeout = keepAliveTimeout > 2000 ? keepAliveTimeout - 1000 : keepAliveTimeout / 2
Copy link
Member Author

Choose a reason for hiding this comment

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

We do 1s as to take into account bad rounding from ms to s on server side.

// Set timeout to 1 second less than hint to account for timing inaccuracies.
client[kKeepAliveTimeout] = Math.min(keepAliveTimeout - 1000, client[kIdleTimeout])
client[kKeepAliveTimeout] = Math.min(keepAliveTimeout, client[kIdleTimeout])

// TODO: What if client[kKeepAliveTimeout] === 0?
}
Expand Down
4 changes: 2 additions & 2 deletions test/client-keep-alive.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ test('keep-alive header', (t) => {
const server = createServer((socket) => {
socket.write('HTTP/1.1 200 OK\r\n')
socket.write('Content-Length: 0\r\n')
socket.write('Keep-Alive: timeout=1s\r\n')
socket.write('Keep-Alive: timeout=2s\r\n')
socket.write('Connection: keep-alive\r\n')
socket.write('\r\n\r\n')
})
Expand All @@ -29,7 +29,7 @@ test('keep-alive header', (t) => {
body.on('end', () => {
const timeout = setTimeout(() => {
t.fail()
}, 1e3)
}, 3e3)
client.on('disconnect', () => {
t.pass()
clearTimeout(timeout)
Expand Down