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 7, 2014
1 parent a9ca84e commit 533ad96
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 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
7 changes: 4 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,10 @@ module.exports = internals.Server = function (/* host, port, options */) {
this.listener = Http.createServer(this._dispatch());
}

if (this.settings.maxSockets !== null) {
Https.globalAgent.maxSockets = this.settings.maxSockets;
Http.globalAgent.maxSockets = this.settings.maxSockets;
if (this.settings.maxSockets !== Infinity) {
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
16 changes: 16 additions & 0 deletions test/unit/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var Fs = require('fs');
var Lab = require('lab');
var Http = require('http');
var Https = require('https');
var Hapi = require('../..');
var Path = require('path');
Expand Down Expand Up @@ -178,6 +179,21 @@ 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 });
expect(mySockets).to.equal(server.httpAgent.maxSockets);
expect(mySockets).to.equal(server.httpsAgent.maxSockets);

expect(globalHttpSockets).to.equal(Http.globalAgent.maxSockets);
expect(globalHttpsSockets).to.equal(Https.globalAgent.maxSockets);

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 533ad96

Please sign in to comment.