diff --git a/lib/proxy.js b/lib/proxy.js index 106c94dc7..039407b8b 100755 --- a/lib/proxy.js +++ b/lib/proxy.js @@ -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) { diff --git a/lib/server.js b/lib/server.js index 29377506e..41cce4bea 100755 --- a/lib/server.js +++ b/lib/server.js @@ -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 diff --git a/package.json b/package.json index e9950b0f9..15982aaf4 100755 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/unit/server.js b/test/unit/server.js index 0a8f863ef..af0c2884a 100755 --- a/test/unit/server.js +++ b/test/unit/server.js @@ -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'); @@ -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';