http: remove excess calls to removeSocket
authorDave <dave@jut.io>
Sun, 6 Dec 2015 12:55:02 +0000 (04:55 -0800)
committerMyles Borins <mborins@us.ibm.com>
Tue, 19 Jan 2016 19:52:31 +0000 (11:52 -0800)
commit39dc05457281b196a376e1b943f4ef2046608beb
treed668e67684795c91c5b8b7d2ba64191eb0a4cf84
parentd19da6638d840eef076a96356dfc4d6adef442dc
http: remove excess calls to removeSocket

socket.destroy() triggers a 'close' event from the socket which triggers
the onClose handler of HTTPAgent which calls self.removeSocket(). So by
calling self.removeSocket() prior to socket.destroy() we end up with two
calls to self.removeSocket().

If there are pending requests, removeSocket ends up creating a new socket.
So if there are pending requests, each time a request completes, we tear
down one socket and create two more. So the total number of sockets grows
exponentially and without regard for any maxSockets settings. This was
noticed in https://github.com/nodejs/node/issues/4050. Let's get rid of
the extra calls to removeSocket so we only call it once per completed
request.

PR-URL: https://github.com/nodejs/node/pull/4172
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Fedor Indutny <fedor@indutny.com>
lib/_http_agent.js
test/parallel/test-http-agent-maxsockets-regress-4050.js [new file with mode: 0644]