Skip to content

Commit 99536b6

Browse files
committed
http: fix http agent keep alive
1 parent 917fcb2 commit 99536b6

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/_http_agent.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,11 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
342342
installListeners(this, s, options);
343343
cb(null, s);
344344
});
345-
345+
// When keepAlive is true, pass the related options to createConnection
346+
if (this.keepAlive) {
347+
options.keepAlive = this.keepAlive;
348+
options.keepAliveInitialDelay = this.keepAliveMsecs;
349+
}
346350
const newSocket = this.createConnection(options, oncreate);
347351
if (newSocket)
348352
oncreate(null, newSocket);

test/parallel/test-http-agent-keepalive.js

+11
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ function second() {
8181
}));
8282
}
8383

84+
function checkSocketKeepAlive(socket) {
85+
const symbols = Object.getOwnPropertySymbols(socket);
86+
for (const [_, symbol] of Object.entries(symbols)) {
87+
if (symbol.toString() === "Symbol(kSetKeepAliveInitialDelay)") {
88+
assert.strictEqual(socket[symbol], agent.keepAliveMsecs / 1000);
89+
return;
90+
}
91+
}
92+
}
93+
8494
function remoteClose() {
8595
// Mock remote server close the socket
8696
const req = get('/remote_close', common.mustCall((res) => {
@@ -94,6 +104,7 @@ function remoteClose() {
94104
assert.strictEqual(agent.sockets[name], undefined);
95105
assert.strictEqual(agent.freeSockets[name].length, 1);
96106
assert.strictEqual(agent.totalSocketCount, 1);
107+
checkSocketKeepAlive(agent.freeSockets[name][0]);
97108
// Waiting remote server close the socket
98109
setTimeout(common.mustCall(() => {
99110
assert.strictEqual(agent.sockets[name], undefined);

0 commit comments

Comments
 (0)