Skip to content

Commit cad3a21

Browse files
ronaglpinca
authored andcommitted
http: simplify timeout handling
Avoids allocating and registering extra listeners for 'timeout'. PR-URL: #29200 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 98b7185 commit cad3a21

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

lib/_http_client.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,10 @@ function responseOnEnd() {
629629
const res = this;
630630
const req = this.req;
631631

632+
if (req.socket && req.timeoutCb) {
633+
req.socket.removeListener('timeout', emitRequestTimeout);
634+
}
635+
632636
req._ended = true;
633637
if (!req.shouldKeepAlive || req.finished)
634638
responseKeepAlive(res, req);
@@ -683,11 +687,17 @@ function tickOnSocket(req, socket) {
683687
req.emit('socket', socket);
684688
}
685689

690+
function emitRequestTimeout() {
691+
const req = this._httpMessage;
692+
if (req) {
693+
req.emit('timeout');
694+
}
695+
}
696+
686697
function listenSocketTimeout(req) {
687698
if (req.timeoutCb) {
688699
return;
689700
}
690-
const emitRequestTimeout = () => req.emit('timeout');
691701
// Set timeoutCb so it will get cleaned up on request end.
692702
req.timeoutCb = emitRequestTimeout;
693703
// Delegate socket timeout event.
@@ -698,12 +708,6 @@ function listenSocketTimeout(req) {
698708
socket.once('timeout', emitRequestTimeout);
699709
});
700710
}
701-
// Remove socket timeout listener after response end.
702-
req.once('response', (res) => {
703-
res.once('end', () => {
704-
req.socket.removeListener('timeout', emitRequestTimeout);
705-
});
706-
});
707711
}
708712

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

0 commit comments

Comments
 (0)