Skip to content

Commit

Permalink
http: fix http agent keep alive
Browse files Browse the repository at this point in the history
  • Loading branch information
theanarkh committed Jun 11, 2022
1 parent 917fcb2 commit 99536b6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,11 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
installListeners(this, s, options);
cb(null, s);
});

// When keepAlive is true, pass the related options to createConnection
if (this.keepAlive) {
options.keepAlive = this.keepAlive;
options.keepAliveInitialDelay = this.keepAliveMsecs;
}
const newSocket = this.createConnection(options, oncreate);
if (newSocket)
oncreate(null, newSocket);
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-http-agent-keepalive.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ function second() {
}));
}

function checkSocketKeepAlive(socket) {
const symbols = Object.getOwnPropertySymbols(socket);
for (const [_, symbol] of Object.entries(symbols)) {
if (symbol.toString() === "Symbol(kSetKeepAliveInitialDelay)") {
assert.strictEqual(socket[symbol], agent.keepAliveMsecs / 1000);
return;
}
}
}

function remoteClose() {
// Mock remote server close the socket
const req = get('/remote_close', common.mustCall((res) => {
Expand All @@ -94,6 +104,7 @@ function remoteClose() {
assert.strictEqual(agent.sockets[name], undefined);
assert.strictEqual(agent.freeSockets[name].length, 1);
assert.strictEqual(agent.totalSocketCount, 1);
checkSocketKeepAlive(agent.freeSockets[name][0]);
// Waiting remote server close the socket
setTimeout(common.mustCall(() => {
assert.strictEqual(agent.sockets[name], undefined);
Expand Down

0 comments on commit 99536b6

Please sign in to comment.