Skip to content

Commit d0efbbb

Browse files
npoltorapavloMichael Fiess
authored and
Michael Fiess
committed
cache http agents (pxscene#1682)
1 parent 2252f37 commit d0efbbb

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

examples/pxScene2d/src/rcvrcore/http_wrap.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,11 @@ function Request(moduleName, appSceneContext, options, callback) {
111111
// convert a dummy Agent into a real one
112112
if (options.agent instanceof Agent) {
113113
var agentObj = options.agent;
114-
var newAgent = isV8 ? null : new module.Agent(agentObj.options);
115-
options.agent = newAgent;
114+
var newAgent = isV8 ? null : (AgentCache[agentObj.uid] || new module.Agent(agentObj.options));
115+
options.agent = AgentCache[agentObj.uid] = newAgent;
116116
agentObj.once('destroy', function () {
117117
if (newAgent) {
118+
log.message(4, 'destroying the agent');
118119
newAgent.destroy.apply(newAgent, arguments);
119120
}
120121
});
@@ -305,8 +306,11 @@ Response.prototype.blocked = false;
305306
function Agent(options) {
306307
EventEmitter.call(this);
307308

309+
this.setMaxListeners(Infinity);
310+
308311
log.message(4, "creating a new agent");
309312
this.options = options;
313+
this.uid = Math.floor(100000 + Math.random() * 900000);
310314

311315
var self = this;
312316
this.destroy = function () {
@@ -318,6 +322,8 @@ function Agent(options) {
318322
Agent.prototype = Object.create(EventEmitter.prototype);
319323
Agent.prototype.constructor = Agent;
320324

325+
var AgentCache = {};
326+
321327
function Utils() {
322328
}
323329

0 commit comments

Comments
 (0)