summaryrefslogtreecommitdiff
path: root/lib/_http_agent.js
diff options
context:
space:
mode:
authorEvan Torrie <evant@yahoo-inc.com>2016-12-04 17:51:51 -0800
committerJames M Snell <jasnell@gmail.com>2017-01-06 10:07:32 -0800
commitc04d4df08c0ba10d4402b09796233fb4f7b52c6c (patch)
treefe5b57f276b033e012fddfa38887e287ca331372 /lib/_http_agent.js
parentfc647fddffd522911f50cca4bd1b94f6e9725285 (diff)
downloadandroid-node-v8-c04d4df08c0ba10d4402b09796233fb4f7b52c6c.tar.gz
android-node-v8-c04d4df08c0ba10d4402b09796233fb4f7b52c6c.tar.bz2
android-node-v8-c04d4df08c0ba10d4402b09796233fb4f7b52c6c.zip
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. 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>
Diffstat (limited to 'lib/_http_agent.js')
-rw-r--r--lib/_http_agent.js56
1 files changed, 30 insertions, 26 deletions
diff --git a/lib/_http_agent.js b/lib/_http_agent.js
index 2a07cd25e2..eebdb24246 100644
--- a/lib/_http_agent.js
+++ b/lib/_http_agent.js
@@ -206,36 +206,40 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
}
self.sockets[name].push(s);
debug('sockets', name, self.sockets[name].length);
-
- function onFree() {
- self.emit('free', s, options);
- }
- s.on('free', onFree);
-
- function onClose(err) {
- debug('CLIENT socket onClose');
- // 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);
- }
- s.on('close', onClose);
-
- function onRemove() {
- // We need this function for cases like HTTP 'upgrade'
- // (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);
- s.removeListener('close', onClose);
- s.removeListener('free', onFree);
- s.removeListener('agentRemove', onRemove);
- }
- s.on('agentRemove', onRemove);
+ installListeners(self, s, options);
cb(null, s);
}
};
+function installListeners(agent, s, options) {
+ function onFree() {
+ debug('CLIENT socket onFree');
+ agent.emit('free', s, options);
+ }
+ s.on('free', onFree);
+
+ function onClose(err) {
+ debug('CLIENT socket onClose');
+ // 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.
+ agent.removeSocket(s, options);
+ }
+ s.on('close', onClose);
+
+ function onRemove() {
+ // We need this function for cases like HTTP 'upgrade'
+ // (defined by WebSockets) where we need to remove a socket from the
+ // pool because it'll be locked up indefinitely
+ debug('CLIENT socket onRemove');
+ agent.removeSocket(s, options);
+ s.removeListener('close', onClose);
+ s.removeListener('free', onFree);
+ s.removeListener('agentRemove', onRemove);
+ }
+ s.on('agentRemove', onRemove);
+}
+
Agent.prototype.removeSocket = function removeSocket(s, options) {
var name = this.getName(options);
debug('removeSocket', name, 'writable:', s.writable);