Skip to content
Permalink
Browse files

http: simplify timeout handling

Avoids allocating and registering extra listeners for 'timeout'.

PR-URL: #29200
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information...
ronag authored and BridgeAR committed Aug 19, 2019
1 parent 2efd72f commit e5a9a8522d4063f32e6a39d32f8c5dbad5fd8e2f
Showing with 11 additions and 7 deletions.
  1. +11 −7 lib/_http_client.js
@@ -625,6 +625,10 @@ function responseOnEnd() {
const res = this;
const req = this.req;

if (req.socket && req.timeoutCb) {
req.socket.removeListener('timeout', emitRequestTimeout);
}

req._ended = true;
if (!req.shouldKeepAlive || req.finished)
responseKeepAlive(res, req);
@@ -680,11 +684,17 @@ function tickOnSocket(req, socket) {
req.emit('socket', socket);
}

function emitRequestTimeout() {
const req = this._httpMessage;
if (req) {
req.emit('timeout');
}
}

function listenSocketTimeout(req) {
if (req.timeoutCb) {
return;
}
const emitRequestTimeout = () => req.emit('timeout');
// Set timeoutCb so it will get cleaned up on request end.
req.timeoutCb = emitRequestTimeout;
// Delegate socket timeout event.
@@ -695,12 +705,6 @@ function listenSocketTimeout(req) {
socket.once('timeout', emitRequestTimeout);
});
}
// Remove socket timeout listener after response end.
req.once('response', (res) => {
res.once('end', () => {
req.socket.removeListener('timeout', emitRequestTimeout);
});
});
}

ClientRequest.prototype.onSocket = function onSocket(socket) {

0 comments on commit e5a9a85

Please sign in to comment.
You can’t perform that action at this time.