Browse Source

http: eliminate capture of ClientRequest in Agent

Keepalive sockets that are returned to the agent's freesocket pool were
previously capturing a reference to the ClientRequest that initiated the
request.

This commit eliminates that by moving the installation of the socket
listeners to a different function.

Backport-PR-URL: https://github.com/nodejs/node/pull/15500
PR-URL: https://github.com/nodejs/node/pull/10134
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
v6.x
Evan Torrie 8 years ago
committed by Myles Borins
parent
commit
0c670e0339
No known key found for this signature in database GPG Key ID: 933B01F40B5CA946
  1. 16
      lib/_http_agent.js

16
lib/_http_agent.js

@ -206,9 +206,15 @@ Agent.prototype.createSocket = function(req, options, cb) {
}
self.sockets[name].push(s);
debug('sockets', name, self.sockets[name].length);
installListeners(self, s, options);
cb(null, s);
}
};
function installListeners(agent, s, options) {
function onFree() {
self.emit('free', s, options);
debug('CLIENT socket onFree');
agent.emit('free', s, options);
}
s.on('free', onFree);
@ -217,7 +223,7 @@ Agent.prototype.createSocket = function(req, options, cb) {
// This is the only place where sockets get removed from the Agent.
// If you want to remove a socket from the pool, just close it.
// All socket errors end in a close event anyway.
self.removeSocket(s, options);
agent.removeSocket(s, options);
}
s.on('close', onClose);
@ -226,17 +232,15 @@ Agent.prototype.createSocket = function(req, options, cb) {
// (defined by WebSockets) where we need to remove a socket from the
// pool because it'll be locked up indefinitely
debug('CLIENT socket onRemove');
self.removeSocket(s, options);
agent.removeSocket(s, options);
s.removeListener('close', onClose);
s.removeListener('free', onFree);
s.removeListener('agentRemove', onRemove);
}
s.on('agentRemove', onRemove);
cb(null, s);
}
};
Agent.prototype.removeSocket = function(s, options) {
Agent.prototype.removeSocket = function removeSocket(s, options) {
var name = this.getName(options);
debug('removeSocket', name, 'writable:', s.writable);
var sets = [this.sockets];

Loading…
Cancel
Save