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: set default timeout in agent keepSocketAlive #33127

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
@@ -138,11 +138,6 @@ function Agent(options) {
socket._httpMessage = null;
this.removeSocket(socket, options);

const agentTimeout = this.options.timeout || 0;
if (socket.timeout !== agentTimeout) {
socket.setTimeout(agentTimeout);
}

socket.once('error', freeSocketErrorListener);
freeSockets.push(socket);
});
@@ -402,6 +397,11 @@ Agent.prototype.keepSocketAlive = function keepSocketAlive(socket) {
socket.setKeepAlive(true, this.keepAliveMsecs);
socket.unref();

const agentTimeout = this.options.timeout || 0;
if (socket.timeout !== agentTimeout) {
socket.setTimeout(agentTimeout);
}

return true;
};

40 changes: 40 additions & 0 deletions test/parallel/test-http-agent-timeout.js
Original file line number Diff line number Diff line change
@@ -92,3 +92,43 @@ const http = require('http');
}));
}));
}

{
// Ensure custom keepSocketAlive timeout is respected

const CUSTOM_TIMEOUT = 60;
const AGENT_TIMEOUT = 50;

class CustomAgent extends http.Agent {
keepSocketAlive(socket) {
if (!super.keepSocketAlive(socket)) {
return false;
}

socket.setTimeout(CUSTOM_TIMEOUT);
return true;
}
}

const agent = new CustomAgent({ keepAlive: true, timeout: AGENT_TIMEOUT });

const server = http.createServer((req, res) => {
res.end();
});

server.listen(0, common.mustCall(() => {
http.get({ port: server.address().port, agent })
.on('response', common.mustCall((res) => {
const socket = res.socket;
assert(socket);
res.resume();
socket.on('free', common.mustCall(() => {
socket.on('timeout', common.mustCall(() => {
assert.strictEqual(socket.timeout, CUSTOM_TIMEOUT);
agent.destroy();
server.close();
}));
}));
}));
}));
}