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: add more idle timeout options #291

Merged
merged 1 commit into from
Aug 4, 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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ Options:
This value may be overriden by keep-alive hints from the server.
Default: `4e3` milliseconds (4s).

- `maxIdleTimeout: Number`, the maximum allowed `idleTimeout` when overriden by
keep-alive hints from the server.
Default: `600e3` milliseconds (10min).

- `keepAliveTimeoutThreshold: Number`, a number subtracted from server keep-alive hints
when overriding `idleTimeout` to account for timing inaccuries caused by e.g.
transport latency.
Default: `1e3` milliseconds (1s).

- `requestTimeout: Number`, the timeout after which a request will time out.
Monitors time between request being enqueued and receiving
a response. Use `0` to disable it entirely.
Expand Down
30 changes: 25 additions & 5 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ const {
kEnqueue,
kKeepAliveTimeout,
kMaxHeadersSize,
kHeadersTimeout
kHeadersTimeout,
kMaxIdleTimeout,
kKeepAliveTimeoutThreshold
} = require('./symbols')
const makeStream = require('./client-stream')
const makeRequest = require('./client-request')
Expand All @@ -69,6 +71,8 @@ class Client extends EventEmitter {
headersTimeout,
socketTimeout,
idleTimeout,
maxIdleTimeout,
keepAliveTimeoutThreshold,
socketPath,
requestTimeout,
pipelining,
Expand Down Expand Up @@ -120,6 +124,18 @@ class Client extends EventEmitter {
throw new InvalidArgumentError('invalid idleTimeout')
}

if (idleTimeout != null && (!Number.isFinite(idleTimeout) || idleTimeout <= 0)) {
throw new InvalidArgumentError('invalid idleTimeout')
}

if (maxIdleTimeout != null && (!Number.isFinite(maxIdleTimeout) || maxIdleTimeout <= 0)) {
throw new InvalidArgumentError('invalid maxIdleTimeout')
}

if (keepAliveTimeoutThreshold != null && !Number.isFinite(keepAliveTimeoutThreshold)) {
throw new InvalidArgumentError('invalid keepAliveTimeoutThreshold')
}

if (requestTimeout != null && !Number.isFinite(requestTimeout)) {
throw new InvalidArgumentError('invalid requestTimeout')
}
Expand All @@ -136,7 +152,9 @@ class Client extends EventEmitter {
this[kUrl] = url
this[kSocketPath] = socketPath
this[kSocketTimeout] = socketTimeout == null ? 30e3 : socketTimeout
this[kMaxIdleTimeout] = maxIdleTimeout == null ? 600e3 : maxIdleTimeout
this[kIdleTimeout] = idleTimeout == null ? 4e3 : idleTimeout
this[kKeepAliveTimeoutThreshold] = keepAliveTimeoutThreshold == null ? 1e3 : keepAliveTimeoutThreshold
this[kKeepAliveTimeout] = this[kIdleTimeout]
this[kRequestTimeout] = requestTimeout == null ? 30e3 : requestTimeout
this[kClosed] = false
Expand Down Expand Up @@ -494,12 +512,14 @@ class Parser extends HTTPParser {
if (headers['keep-alive']) {
const m = headers['keep-alive'].match(/timeout=(\d+)/)
if (m) {
const timeout = Number(m[1])
if (!timeout) {
const keepAliveTimeout = Math.min(
Number(m[1]) * 1000 - client[kKeepAliveTimeoutThreshold],
client[kMaxIdleTimeout]
)
if (!keepAliveTimeout || keepAliveTimeout < 1e3) {
client[kReset] = true
} else {
// Set timeout to 500ms less than hint to account for timing inaccuracies.
client[kKeepAliveTimeout] = timeout * 1000 - 500
client[kKeepAliveTimeout] = keepAliveTimeout
}
}
} else {
Expand Down
16 changes: 2 additions & 14 deletions lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,15 @@ const {
class Pool {
constructor (url, {
connections,
maxAbortedPayload,
socketTimeout,
requestTimeout,
pipelining,
tls,
socketPath
...options
} = {}) {
if (connections != null && (!Number.isFinite(connections) || connections <= 0)) {
throw new InvalidArgumentError('invalid connections')
}

this[kClients] = Array.from({
length: connections || 10
}, () => new Client(url, {
maxAbortedPayload,
socketTimeout,
requestTimeout,
pipelining,
tls,
socketPath
}))
}, () => new Client(url, options))
}

/* istanbul ignore next: use by benchmark */
Expand Down
2 changes: 2 additions & 0 deletions lib/symbols.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module.exports = {
kConnect: Symbol('connect'),
kSocketTimeout: Symbol('socket timeout'),
kIdleTimeout: Symbol('idle timeout'),
kMaxIdleTimeout: Symbol('max idle timeout'),
kKeepAliveTimeoutThreshold: Symbol('keep alive threshold'),
kRequestTimeout: Symbol('request timeout'),
kKeepAliveTimeout: Symbol('keep alive timeout'),
kServerName: Symbol('server name'),
Expand Down