Skip to content

Commit ea3ca87

Browse files
theanarkhtargos
authored andcommitted
http: fix http agent keep alive
PR-URL: #43380 Reviewed-By: Paolo Insogna <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Ricky Zhou <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent bfb8a0b commit ea3ca87

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

lib/_http_agent.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,11 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
339339
installListeners(this, s, options);
340340
cb(null, s);
341341
});
342-
342+
// When keepAlive is true, pass the related options to createConnection
343+
if (this.keepAlive) {
344+
options.keepAlive = this.keepAlive;
345+
options.keepAliveInitialDelay = this.keepAliveMsecs;
346+
}
343347
const newSocket = this.createConnection(options, oncreate);
344348
if (newSocket)
345349
oncreate(null, newSocket);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const assert = require('assert');
5+
const http = require('http');
6+
const { Agent } = require('_http_agent');
7+
8+
const agent = new Agent({
9+
keepAlive: true,
10+
keepAliveMsecs: 1000,
11+
});
12+
13+
const server = http.createServer(common.mustCall((req, res) => {
14+
res.end('ok');
15+
}));
16+
17+
server.listen(0, common.mustCall(() => {
18+
const createConnection = agent.createConnection;
19+
agent.createConnection = (options, ...args) => {
20+
assert.strictEqual(options.keepAlive, true);
21+
assert.strictEqual(options.keepAliveInitialDelay, agent.keepAliveMsecs);
22+
return createConnection.call(agent, options, ...args);
23+
};
24+
http.get({
25+
host: 'localhost',
26+
port: server.address().port,
27+
agent: agent,
28+
path: '/'
29+
}, common.mustCall((res) => {
30+
// for emit end event
31+
res.on('data', () => {});
32+
res.on('end', () => {
33+
server.close();
34+
});
35+
}));
36+
}));

0 commit comments

Comments
 (0)