Skip to content

Commit 3737bd9

Browse files
ronagBridgeAR
authored andcommitted
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>
1 parent d82ecb9 commit 3737bd9

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

lib/_http_client.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,10 @@ function responseOnEnd() {
625625
const res = this;
626626
const req = this.req;
627627

628+
if (req.socket && req.timeoutCb) {
629+
req.socket.removeListener('timeout', emitRequestTimeout);
630+
}
631+
628632
req._ended = true;
629633
if (!req.shouldKeepAlive || req.finished)
630634
responseKeepAlive(res, req);
@@ -680,11 +684,17 @@ function tickOnSocket(req, socket) {
680684
req.emit('socket', socket);
681685
}
682686

687+
function emitRequestTimeout() {
688+
const req = this._httpMessage;
689+
if (req) {
690+
req.emit('timeout');
691+
}
692+
}
693+
683694
function listenSocketTimeout(req) {
684695
if (req.timeoutCb) {
685696
return;
686697
}
687-
const emitRequestTimeout = () => req.emit('timeout');
688698
// Set timeoutCb so it will get cleaned up on request end.
689699
req.timeoutCb = emitRequestTimeout;
690700
// Delegate socket timeout event.
@@ -695,12 +705,6 @@ function listenSocketTimeout(req) {
695705
socket.once('timeout', emitRequestTimeout);
696706
});
697707
}
698-
// Remove socket timeout listener after response end.
699-
req.once('response', (res) => {
700-
res.once('end', () => {
701-
req.socket.removeListener('timeout', emitRequestTimeout);
702-
});
703-
});
704708
}
705709

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

0 commit comments

Comments
 (0)