http: simplify timeout handling
Avoids allocating and registering extra listeners for 'timeout'. PR-URL: https://github.com/nodejs/node/pull/29200 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
parent
98b718572f
commit
cad3a21c1d
@ -629,6 +629,10 @@ function responseOnEnd() {
|
|||||||
const res = this;
|
const res = this;
|
||||||
const req = this.req;
|
const req = this.req;
|
||||||
|
|
||||||
|
if (req.socket && req.timeoutCb) {
|
||||||
|
req.socket.removeListener('timeout', emitRequestTimeout);
|
||||||
|
}
|
||||||
|
|
||||||
req._ended = true;
|
req._ended = true;
|
||||||
if (!req.shouldKeepAlive || req.finished)
|
if (!req.shouldKeepAlive || req.finished)
|
||||||
responseKeepAlive(res, req);
|
responseKeepAlive(res, req);
|
||||||
@ -683,11 +687,17 @@ function tickOnSocket(req, socket) {
|
|||||||
req.emit('socket', socket);
|
req.emit('socket', socket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function emitRequestTimeout() {
|
||||||
|
const req = this._httpMessage;
|
||||||
|
if (req) {
|
||||||
|
req.emit('timeout');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function listenSocketTimeout(req) {
|
function listenSocketTimeout(req) {
|
||||||
if (req.timeoutCb) {
|
if (req.timeoutCb) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const emitRequestTimeout = () => req.emit('timeout');
|
|
||||||
// Set timeoutCb so it will get cleaned up on request end.
|
// Set timeoutCb so it will get cleaned up on request end.
|
||||||
req.timeoutCb = emitRequestTimeout;
|
req.timeoutCb = emitRequestTimeout;
|
||||||
// Delegate socket timeout event.
|
// Delegate socket timeout event.
|
||||||
@ -698,12 +708,6 @@ function listenSocketTimeout(req) {
|
|||||||
socket.once('timeout', emitRequestTimeout);
|
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) {
|
ClientRequest.prototype.onSocket = function onSocket(socket) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user