Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set default idle timeout to 4 seconds #279

Merged
merged 4 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Options:
- `idleTimeout`, the timeout after which a socket with no active requests
will be released and no longer re-used for subsequent requests. This value
is an upper bound and might be reduced by keep-alive hints from the server.
Default: `30e3` milliseconds (30s).
Default: `4e3` milliseconds (4s).

- `requestTimeout`, the timeout after which a request will time out, in
milliseconds. Monitors time between request being enqueued and receiving
Expand Down
11 changes: 7 additions & 4 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class Client extends EventEmitter {
this[kUrl] = url
this[kSocketPath] = socketPath
this[kSocketTimeout] = socketTimeout == null ? 30e3 : socketTimeout
this[kIdleTimeout] = idleTimeout == null ? 30e3 : idleTimeout
this[kIdleTimeout] = idleTimeout == null ? 4e3 : idleTimeout
this[kKeepAliveTimeout] = this[kIdleTimeout]
this[kRequestTimeout] = requestTimeout == null ? 30e3 : requestTimeout
this[kClosed] = false
Expand Down Expand Up @@ -442,9 +442,12 @@ class Parser extends HTTPParser {
if (headers['keep-alive']) {
const m = headers['keep-alive'].match(/timeout=(\d+)/)
if (m) {
const keepAliveTimeout = Number(m[1]) * 1000
// Set timeout to half of hint to account for timing inaccuracies.
client[kKeepAliveTimeout] = Math.min(keepAliveTimeout - 500, client[kIdleTimeout])
const timeout = Number(m[1]) * 1000
// Set timeout to 1 second less than hint to account for timing inaccuracies.
const keepAliveTimeout = timeout > 2000 ? timeout - 1000 : timeout / 2
client[kKeepAliveTimeout] = Math.min(keepAliveTimeout, client[kIdleTimeout])

// TODO: What if client[kKeepAliveTimeout] === 0?
}
} else {
client[kKeepAliveTimeout] = client[kIdleTimeout]
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