Skip to content

Commit 2043944

Browse files
committed
http: client keep-alive for UNIX domain sockets
Makes `Connection: keep-alive` behave correctly when making client connections to UNIX domain sockets. Prior to this, connections would never be re-used, but the keep-alive would cause the connections to stick around until they time out. This would lead to an eventual EMFILE error due to all the connections staying open. This was due to http.Agent not properly supporting UNIX domain sockets. PR-URL: #13214 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 510cab7 commit 2043944

File tree

4 files changed

+57
-19
lines changed

4 files changed

+57
-19
lines changed

lib/_http_agent.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ Agent.prototype.getName = function getName(options) {
131131
if (options.family === 4 || options.family === 6)
132132
name += ':' + options.family;
133133

134+
if (options.socketPath)
135+
name += ':' + options.socketPath;
136+
134137
return name;
135138
};
136139

@@ -147,6 +150,8 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/,
147150

148151
options = util._extend({}, options);
149152
util._extend(options, this.options);
153+
if (options.socketPath)
154+
options.path = options.socketPath;
150155

151156
if (!options.servername) {
152157
options.servername = options.host;
@@ -199,6 +204,8 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
199204
var self = this;
200205
options = util._extend({}, options);
201206
util._extend(options, self.options);
207+
if (options.socketPath)
208+
options.path = options.socketPath;
202209

203210
if (!options.servername) {
204211
options.servername = options.host;

lib/_http_client.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -240,23 +240,7 @@ function ClientRequest(options, cb) {
240240
this._deferToConnect(null, null, () => this._flush());
241241
};
242242

243-
var newSocket;
244-
if (this.socketPath) {
245-
this._last = true;
246-
this.shouldKeepAlive = false;
247-
var optionsPath = {
248-
path: this.socketPath,
249-
timeout: this.timeout,
250-
rejectUnauthorized: !!options.rejectUnauthorized
251-
};
252-
newSocket = this.agent.createConnection(optionsPath, oncreate);
253-
if (newSocket && !called) {
254-
called = true;
255-
this.onSocket(newSocket);
256-
} else {
257-
return;
258-
}
259-
} else if (this.agent) {
243+
if (this.agent) {
260244
// If there is an agent we should default to Connection:keep-alive,
261245
// but only if the Agent will actually reuse the connection!
262246
// If it's not a keepAlive agent, and the maxSockets==Infinity, then
@@ -274,7 +258,7 @@ function ClientRequest(options, cb) {
274258
this._last = true;
275259
this.shouldKeepAlive = false;
276260
if (typeof options.createConnection === 'function') {
277-
newSocket = options.createConnection(options, oncreate);
261+
const newSocket = options.createConnection(options, oncreate);
278262
if (newSocket && !called) {
279263
called = true;
280264
this.onSocket(newSocket);

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

3-
require('../common');
3+
const common = require('../common');
44
const assert = require('assert');
55
const http = require('http');
6+
const path = require('path');
67

78
const agent = new http.Agent();
89

@@ -31,6 +32,15 @@ assert.strictEqual(
3132
'0.0.0.0:80:192.168.1.1'
3233
);
3334

35+
// unix socket
36+
const socketPath = path.join(common.tmpDir, 'foo', 'bar');
37+
assert.strictEqual(
38+
agent.getName({
39+
socketPath
40+
}),
41+
`localhost:::${socketPath}`
42+
);
43+
3444
for (const family of [0, null, undefined, 'bogus'])
3545
assert.strictEqual(agent.getName({ family }), 'localhost::');
3646

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const http = require('http');
5+
6+
const server = http.createServer((req, res) => res.end());
7+
8+
common.refreshTmpDir();
9+
10+
server.listen(common.PIPE, common.mustCall(() =>
11+
asyncLoop(makeKeepAliveRequest, 10, common.mustCall(() =>
12+
server.getConnections(common.mustCall((err, conns) => {
13+
assert.ifError(err);
14+
assert.strictEqual(conns, 1);
15+
server.close();
16+
}))
17+
))
18+
));
19+
20+
function asyncLoop(fn, times, cb) {
21+
fn(function handler() {
22+
if (--times) {
23+
fn(handler);
24+
} else {
25+
cb();
26+
}
27+
});
28+
}
29+
function makeKeepAliveRequest(cb) {
30+
http.get({
31+
socketPath: common.PIPE,
32+
headers: { connection: 'keep-alive' }
33+
}, (res) => res.on('data', common.mustNotCall())
34+
.on('error', assert.fail)
35+
.on('end', cb)
36+
);
37+
}

0 commit comments

Comments
 (0)