Skip to content

Commit 0c670e0

Browse files
Evan TorrieMylesBorins
authored andcommitted
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: #15500 PR-URL: #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>
1 parent 8e00315 commit 0c670e0

1 file changed

Lines changed: 31 additions & 27 deletions

File tree

‎lib/_http_agent.js‎

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -206,37 +206,41 @@ Agent.prototype.createSocket = function(req, options, cb) {
206206
}
207207
self.sockets[name].push(s);
208208
debug('sockets',name,self.sockets[name].length);
209-
210-
functiononFree(){
211-
self.emit('free',s,options);
212-
}
213-
s.on('free',onFree);
214-
215-
functiononClose(err){
216-
debug('CLIENT socket onClose');
217-
// This is the only place where sockets get removed from the Agent.
218-
// If you want to remove a socket from the pool, just close it.
219-
// All socket errors end in a close event anyway.
220-
self.removeSocket(s,options);
221-
}
222-
s.on('close',onClose);
223-
224-
functiononRemove(){
225-
// We need this function for cases like HTTP 'upgrade'
226-
// (defined by WebSockets) where we need to remove a socket from the
227-
// pool because it'll be locked up indefinitely
228-
debug('CLIENT socket onRemove');
229-
self.removeSocket(s,options);
230-
s.removeListener('close',onClose);
231-
s.removeListener('free',onFree);
232-
s.removeListener('agentRemove',onRemove);
233-
}
234-
s.on('agentRemove',onRemove);
209+
installListeners(self,s,options);
235210
cb(null,s);
236211
}
237212
};
238213

239-
Agent.prototype.removeSocket=function(s,options){
214+
functioninstallListeners(agent,s,options){
215+
functiononFree(){
216+
debug('CLIENT socket onFree');
217+
agent.emit('free',s,options);
218+
}
219+
s.on('free',onFree);
220+
221+
functiononClose(err){
222+
debug('CLIENT socket onClose');
223+
// This is the only place where sockets get removed from the Agent.
224+
// If you want to remove a socket from the pool, just close it.
225+
// All socket errors end in a close event anyway.
226+
agent.removeSocket(s,options);
227+
}
228+
s.on('close',onClose);
229+
230+
functiononRemove(){
231+
// We need this function for cases like HTTP 'upgrade'
232+
// (defined by WebSockets) where we need to remove a socket from the
233+
// pool because it'll be locked up indefinitely
234+
debug('CLIENT socket onRemove');
235+
agent.removeSocket(s,options);
236+
s.removeListener('close',onClose);
237+
s.removeListener('free',onFree);
238+
s.removeListener('agentRemove',onRemove);
239+
}
240+
s.on('agentRemove',onRemove);
241+
}
242+
243+
Agent.prototype.removeSocket=functionremoveSocket(s,options){
240244
varname=this.getName(options);
241245
debug('removeSocket',name,'writable:',s.writable);
242246
varsets=[this.sockets];

0 commit comments

Comments
 (0)