Skip to content

Commit

Permalink
closes hapijs#1346, set maxSockets on proxy requests without trashing…
Browse files Browse the repository at this point in the history
… the global value
  • Loading branch information
Johnny Domino committed Feb 8, 2014
1 parent a9ca84e commit 1f8283b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ exports.handler = function (route, options) {
options.headers['Content-Type'] = contentType;
}

// Send request
if (request.server.httpAgent) {
options.httpAgent = request.server.httpAgent;
}

if (request.server.httpsAgent) {
options.httpsAgent = request.server.httpsAgent;
}

// Send request
Nipple.request(request.method, uri, options, function (err, res) {

if (err) {
Expand Down
5 changes: 3 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,9 @@ module.exports = internals.Server = function (/* host, port, options */) {
}

if (this.settings.maxSockets !== null) {
Https.globalAgent.maxSockets = this.settings.maxSockets;
Http.globalAgent.maxSockets = this.settings.maxSockets;
var params = { maxSockets: this.settings.maxSockets };
this.httpAgent = new Http.Agent(params);
this.httpsAgent = new Https.Agent(params);
}

// Server information
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"joi": "^2.4.x",
"catbox": "1.x.x",
"shot": "1.x.x",
"nipple": "2.x.x",
"nipple": "2.3.x",
"cryptiles": "2.x.x",
"iron": "2.x.x",
"async": "0.2.x",
Expand Down
39 changes: 39 additions & 0 deletions test/unit/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

var Fs = require('fs');
var Lab = require('lab');
var Http = require('http');
var Https = require('https');
var Hapi = require('../..');
var Path = require('path');
var Defaults = require('../../lib/defaults');

// Declare internals

Expand Down Expand Up @@ -178,6 +180,43 @@ describe('Server', function () {
});
});

it('uses a custom http/https agent to provide maxSockets for proxy requests', function (done) {
var globalHttpSockets = Http.globalAgent.maxSockets;
var globalHttpsSockets = Https.globalAgent.maxSockets;

var mySockets = 23;
var server = new Hapi.Server({ maxSockets: mySockets });

// server has the provided maxSockets
expect(mySockets).to.equal(server.settings.maxSockets);

// server's agent has the provided maxSockets
expect(mySockets).to.equal(server.httpAgent.maxSockets);
expect(mySockets).to.equal(server.httpsAgent.maxSockets);

// ensure global sockets are unchanged
expect(globalHttpSockets).to.equal(Http.globalAgent.maxSockets);
expect(globalHttpsSockets).to.equal(Https.globalAgent.maxSockets);

done();
});

it('use Hapi defaults when maxSockets are null', function (done) {
var globalHttpSockets = Http.globalAgent.maxSockets;
var globalHttpsSockets = Https.globalAgent.maxSockets;

var server = new Hapi.Server({ maxSockets: null });

// ensure defaults are used
expect(server.settings.maxSockets).to.equal(Defaults.server.maxSockets);

// ensure global sockets are unchanged
expect(Http.globalAgent.maxSockets).to.equal(globalHttpSockets);
expect(Https.globalAgent.maxSockets).to.equal(globalHttpsSockets);

done();
});

it('creates a server listening on a windows named pipe', function (done) {

var host = '\\\\.\\pipe\\6653e55f-26ec-4268-a4f2-882f4089315c';
Expand Down

0 comments on commit 1f8283b

Please sign in to comment.