From 887c5808c90b7128c040e510e237ddb4d034fe3e Mon Sep 17 00:00:00 2001 From: indexzero Date: Wed, 25 May 2011 00:48:44 -0600 Subject: [PATCH] [refactor] Manage our own internal list of Agent instances --- lib/node-http-proxy.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/node-http-proxy.js b/lib/node-http-proxy.js index dc32f2e..f96cf5c 100644 --- a/lib/node-http-proxy.js +++ b/lib/node-http-proxy.js @@ -36,6 +36,11 @@ var util = require('util'), // exports.version = [0, 5, 7]; +// +// Track our own list of agents internal to `node-http-proxy` +// +var _agents = {}; + // // ### function _getAgent (host, port, secure) // #### @host {string} Host of the agent to get @@ -45,13 +50,23 @@ exports.version = [0, 5, 7]; // and sets the `maxSockets` property appropriately. // function _getAgent (host, port, secure) { - var agent = !secure ? http.getAgent(host, port) : https.getAgent({ - host: host, - port: port - }); - - agent.maxSockets = maxSockets; - return agent; + var Agent, id = [host, port].join(':'); + + if (!port) { + port = secure ? 443 : 80; + } + + if (!_agents[id]) { + Agent = secure ? https.Agent : http.Agent; + + _agents[id] = new Agent({ + host: host, + port: port, + maxSockets: maxSockets + }); + } + + return _agents[id]; } //