diff --git a/lib/_http_agent.js b/lib/_http_agent.js index 6784e884859986..791a43c8d5d029 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -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); diff --git a/test/parallel/test-http-agent-keepalive.js b/test/parallel/test-http-agent-keepalive.js index f7424634df1bd5..d3cbf6c9d216f3 100644 --- a/test/parallel/test-http-agent-keepalive.js +++ b/test/parallel/test-http-agent-keepalive.js @@ -81,6 +81,17 @@ function second() { })); } +function checkSocketKeepAlive(socket) { + const symbols = Object.getOwnPropertySymbols(socket); + for (let i = 0; i < symbols.length; i++) { + const symbol = symbols[i]; + 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) => { @@ -94,6 +105,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);