Browse Source

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: https://github.com/nodejs/node/pull/13214
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
canary-base
Bryan English 8 years ago
parent
commit
2043944a4c
  1. 7
      lib/_http_agent.js
  2. 20
      lib/_http_client.js
  3. 12
      test/parallel/test-http-agent-getname.js
  4. 37
      test/parallel/test-http-unix-socket-keep-alive.js

7
lib/_http_agent.js

@ -131,6 +131,9 @@ Agent.prototype.getName = function getName(options) {
if (options.family === 4 || options.family === 6) if (options.family === 4 || options.family === 6)
name += ':' + options.family; name += ':' + options.family;
if (options.socketPath)
name += ':' + options.socketPath;
return name; return name;
}; };
@ -147,6 +150,8 @@ Agent.prototype.addRequest = function addRequest(req, options, port/*legacy*/,
options = util._extend({}, options); options = util._extend({}, options);
util._extend(options, this.options); util._extend(options, this.options);
if (options.socketPath)
options.path = options.socketPath;
if (!options.servername) { if (!options.servername) {
options.servername = options.host; options.servername = options.host;
@ -199,6 +204,8 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
var self = this; var self = this;
options = util._extend({}, options); options = util._extend({}, options);
util._extend(options, self.options); util._extend(options, self.options);
if (options.socketPath)
options.path = options.socketPath;
if (!options.servername) { if (!options.servername) {
options.servername = options.host; options.servername = options.host;

20
lib/_http_client.js

@ -240,23 +240,7 @@ function ClientRequest(options, cb) {
this._deferToConnect(null, null, () => this._flush()); this._deferToConnect(null, null, () => this._flush());
}; };
var newSocket; if (this.agent) {
if (this.socketPath) {
this._last = true;
this.shouldKeepAlive = false;
var optionsPath = {
path: this.socketPath,
timeout: this.timeout,
rejectUnauthorized: !!options.rejectUnauthorized
};
newSocket = this.agent.createConnection(optionsPath, oncreate);
if (newSocket && !called) {
called = true;
this.onSocket(newSocket);
} else {
return;
}
} else if (this.agent) {
// If there is an agent we should default to Connection:keep-alive, // If there is an agent we should default to Connection:keep-alive,
// but only if the Agent will actually reuse the connection! // but only if the Agent will actually reuse the connection!
// If it's not a keepAlive agent, and the maxSockets==Infinity, then // If it's not a keepAlive agent, and the maxSockets==Infinity, then
@ -274,7 +258,7 @@ function ClientRequest(options, cb) {
this._last = true; this._last = true;
this.shouldKeepAlive = false; this.shouldKeepAlive = false;
if (typeof options.createConnection === 'function') { if (typeof options.createConnection === 'function') {
newSocket = options.createConnection(options, oncreate); const newSocket = options.createConnection(options, oncreate);
if (newSocket && !called) { if (newSocket && !called) {
called = true; called = true;
this.onSocket(newSocket); this.onSocket(newSocket);

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

@ -1,8 +1,9 @@
'use strict'; 'use strict';
require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const http = require('http'); const http = require('http');
const path = require('path');
const agent = new http.Agent(); const agent = new http.Agent();
@ -31,6 +32,15 @@ assert.strictEqual(
'0.0.0.0:80:192.168.1.1' '0.0.0.0:80:192.168.1.1'
); );
// unix socket
const socketPath = path.join(common.tmpDir, 'foo', 'bar');
assert.strictEqual(
agent.getName({
socketPath
}),
`localhost:::${socketPath}`
);
for (const family of [0, null, undefined, 'bogus']) for (const family of [0, null, undefined, 'bogus'])
assert.strictEqual(agent.getName({ family }), 'localhost::'); assert.strictEqual(agent.getName({ family }), 'localhost::');

37
test/parallel/test-http-unix-socket-keep-alive.js

@ -0,0 +1,37 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const http = require('http');
const server = http.createServer((req, res) => res.end());
common.refreshTmpDir();
server.listen(common.PIPE, common.mustCall(() =>
asyncLoop(makeKeepAliveRequest, 10, common.mustCall(() =>
server.getConnections(common.mustCall((err, conns) => {
assert.ifError(err);
assert.strictEqual(conns, 1);
server.close();
}))
))
));
function asyncLoop(fn, times, cb) {
fn(function handler() {
if (--times) {
fn(handler);
} else {
cb();
}
});
}
function makeKeepAliveRequest(cb) {
http.get({
socketPath: common.PIPE,
headers: { connection: 'keep-alive' }
}, (res) => res.on('data', common.mustNotCall())
.on('error', assert.fail)
.on('end', cb)
);
}
Loading…
Cancel
Save