-
-
Notifications
You must be signed in to change notification settings - Fork 934
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
Customize timeouts and generally improve the whole thing #534
Changes from all commits
4bcbb3b
0ff85f5
a8bee05
e54cc20
77a96c9
0975372
caae921
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ const timedOut = require('./timed-out'); | |
const getBodySize = require('./get-body-size'); | ||
const getResponse = require('./get-response'); | ||
const progress = require('./progress'); | ||
const {CacheError, UnsupportedProtocolError, MaxRedirectsError, RequestError} = require('./errors'); | ||
const {GotError, CacheError, UnsupportedProtocolError, MaxRedirectsError, RequestError} = require('./errors'); | ||
|
||
const getMethodRedirectCodes = new Set([300, 301, 302, 303, 304, 305, 307, 308]); | ||
const allMethodRedirectCodes = new Set([300, 303, 307, 308]); | ||
|
@@ -91,13 +91,11 @@ module.exports = (options = {}) => { | |
return; | ||
} | ||
|
||
setImmediate(() => { | ||
try { | ||
getResponse(response, options, emitter, redirects); | ||
} catch (error) { | ||
emitter.emit('error', error); | ||
} | ||
}); | ||
try { | ||
getResponse(response, options, emitter, redirects); | ||
} catch (error) { | ||
emitter.emit('error', error); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we leave this unchanged as this is being discussed in #525? Try not to duplicate PRs. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, this should not have been included here, but since I'm gonna accept #525, I'll allow it. |
||
}); | ||
|
||
cacheReq.on('error', error => { | ||
|
@@ -119,23 +117,23 @@ module.exports = (options = {}) => { | |
return; | ||
} | ||
|
||
const err = new RequestError(error, options); | ||
emitter.emit('retry', err, retried => { | ||
if (!(error instanceof GotError)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't sure that this was the precise change needed. My objective was to expose the TimeoutError to a client using the Promise interface. |
||
error = new RequestError(error, options); | ||
} | ||
emitter.emit('retry', error, retried => { | ||
if (!retried) { | ||
emitter.emit('error', err); | ||
emitter.emit('error', error); | ||
} | ||
}); | ||
}); | ||
|
||
progress.upload(req, emitter, uploadBodySize); | ||
|
||
if (options.gotTimeout) { | ||
timedOut(req, options.gotTimeout); | ||
timedOut(req, options); | ||
} | ||
|
||
setImmediate(() => { | ||
emitter.emit('request', req); | ||
}); | ||
emitter.emit('request', req); | ||
}); | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,131 @@ | ||
'use strict'; | ||
const net = require('net'); | ||
const {TimeoutError} = require('./errors'); | ||
|
||
// Forked from https://github.com/floatdrop/timed-out | ||
const reentry = Symbol('reentry'); | ||
|
||
module.exports = function (req, delays) { | ||
if (req.timeoutTimer) { | ||
return req; | ||
function addTimeout(delay, callback, ...args) { | ||
// Event loop order is timers, poll, immediates. | ||
// The timed event may emit during the current tick poll phase, so | ||
// defer calling the handler until the poll phase completes. | ||
let immediate; | ||
const timeout = setTimeout( | ||
() => { | ||
immediate = setImmediate(callback, delay, ...args); | ||
if (immediate.unref) { | ||
// Added in node v9.7.0 | ||
immediate.unref(); | ||
} | ||
}, | ||
delay | ||
); | ||
timeout.unref(); | ||
return () => { | ||
clearTimeout(timeout); | ||
clearImmediate(immediate); | ||
}; | ||
} | ||
|
||
module.exports = function (req, options) { | ||
if (req[reentry]) { | ||
return; | ||
} | ||
req[reentry] = true; | ||
const {gotTimeout: delays, host, hostname} = options; | ||
const timeoutHandler = (delay, event) => { | ||
req.abort(); | ||
req.emit('error', new TimeoutError(delay, event, options)); | ||
}; | ||
const cancelers = []; | ||
const cancelTimeouts = () => { | ||
cancelers.forEach(cancelTimeout => cancelTimeout()); | ||
}; | ||
|
||
const host = req._headers ? (' to ' + req._headers.host) : ''; | ||
req.on('error', cancelTimeouts); | ||
req.once('response', response => { | ||
response.once('end', cancelTimeouts); | ||
}); | ||
|
||
function throwESOCKETTIMEDOUT() { | ||
req.abort(); | ||
const e = new Error('Socket timed out on request' + host); | ||
e.code = 'ESOCKETTIMEDOUT'; | ||
req.emit('error', e); | ||
if (delays.request !== undefined) { | ||
const cancelTimeout = addTimeout( | ||
delays.request, | ||
timeoutHandler, | ||
'request' | ||
); | ||
cancelers.push(cancelTimeout); | ||
} | ||
|
||
function throwETIMEDOUT() { | ||
req.abort(); | ||
const e = new Error('Connection timed out on request' + host); | ||
e.code = 'ETIMEDOUT'; | ||
req.emit('error', e); | ||
if (delays.socket !== undefined) { | ||
req.setTimeout( | ||
delays.socket, | ||
() => { | ||
timeoutHandler(delays.socket, 'socket'); | ||
} | ||
); | ||
} | ||
if (delays.lookup !== undefined && !req.socketPath && !net.isIP(hostname || host)) { | ||
req.once('socket', socket => { | ||
if (socket.connecting) { | ||
const cancelTimeout = addTimeout( | ||
delays.lookup, | ||
timeoutHandler, | ||
'lookup' | ||
); | ||
cancelers.push(cancelTimeout); | ||
socket.once('lookup', cancelTimeout); | ||
} | ||
}); | ||
} | ||
|
||
if (delays.connect !== undefined) { | ||
req.timeoutTimer = setTimeout(throwETIMEDOUT, delays.connect); | ||
req.once('socket', socket => { | ||
if (socket.connecting) { | ||
const timeConnect = () => { | ||
const cancelTimeout = addTimeout( | ||
delays.connect, | ||
timeoutHandler, | ||
'connect' | ||
); | ||
cancelers.push(cancelTimeout); | ||
return cancelTimeout; | ||
}; | ||
if (req.socketPath || net.isIP(hostname || host)) { | ||
socket.once('connect', timeConnect()); | ||
} else { | ||
socket.once('lookup', () => { | ||
socket.once('connect', timeConnect()); | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
if (delays.request !== undefined) { | ||
req.requestTimeoutTimer = setTimeout(() => { | ||
clear(); | ||
|
||
if (req.connection.connecting) { | ||
throwETIMEDOUT(); | ||
if (delays.send !== undefined) { | ||
req.once('socket', socket => { | ||
const timeRequest = () => { | ||
const cancelTimeout = addTimeout( | ||
delays.send, | ||
timeoutHandler, | ||
'send' | ||
); | ||
cancelers.push(cancelTimeout); | ||
return cancelTimeout; | ||
}; | ||
if (socket.connecting) { | ||
socket.once('connect', () => { | ||
req.once('upload-complete', timeRequest()); | ||
}); | ||
} else { | ||
throwESOCKETTIMEDOUT(); | ||
req.once('upload-complete', timeRequest()); | ||
} | ||
}, delays.request); | ||
} | ||
|
||
// Clear the connection timeout timer once a socket is assigned to the | ||
// request and is connected. | ||
req.on('socket', socket => { | ||
// Socket may come from Agent pool and may be already connected. | ||
if (!socket.connecting) { | ||
connect(); | ||
return; | ||
} | ||
|
||
socket.once('connect', connect); | ||
}); | ||
|
||
function clear() { | ||
if (req.timeoutTimer) { | ||
clearTimeout(req.timeoutTimer); | ||
req.timeoutTimer = null; | ||
} | ||
}); | ||
} | ||
|
||
function connect() { | ||
clear(); | ||
|
||
if (delays.socket !== undefined) { | ||
// Abort the request if there is no activity on the socket for more | ||
// than `delays.socket` milliseconds. | ||
req.setTimeout(delays.socket, throwESOCKETTIMEDOUT); | ||
} | ||
|
||
req.on('response', res => { | ||
res.on('end', () => { | ||
// The request is finished, cancel request timeout. | ||
clearTimeout(req.requestTimeoutTimer); | ||
}); | ||
if (delays.response !== undefined) { | ||
req.once('upload-complete', () => { | ||
const cancelTimeout = addTimeout( | ||
delays.response, | ||
timeoutHandler, | ||
'response' | ||
); | ||
cancelers.push(cancelTimeout); | ||
req.once('response', cancelTimeout); | ||
}); | ||
} | ||
|
||
return req.on('error', clear); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Being able to set timeouts around each phase is more useful that timing each phase from the beginning of the request.
A concrete example is being able to time the
response
phase (TTFB) when the target is a load-balanced java service which may be undergoing garbage collection. We often want to set a very aggressive response timeout in this case, so that we can retry the request immediately against another instance.