Skip to content
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

http: simplify timeout handling #29200

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 11 additions & 7 deletions lib/_http_client.js
Expand Up @@ -620,6 +620,10 @@ function responseOnEnd() {
const res = this;
const req = this.req;

if (req.socket && req.timeoutCb) {
req.socket.removeListener('timeout', emitRequestTimeout);
ronag marked this conversation as resolved.
Show resolved Hide resolved
}

req._ended = true;
if (!req.shouldKeepAlive || req.finished)
responseKeepAlive(res, req);
Expand Down Expand Up @@ -676,11 +680,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.
Expand All @@ -691,12 +701,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) {
Expand Down