Skip to content

Commit 131eef6

Browse files
benglMylesBorins
authored andcommitted
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 01bee5d commit 131eef6

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
@@ -241,23 +241,7 @@ function ClientRequest(options, cb) {
241241
this._deferToConnect(null, null, () => this._flush());
242242
};
243243

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