diff --git a/lib/_http_agent.js b/lib/_http_agent.js index cd991dea75d587..c47c7f0c597448 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -112,11 +112,15 @@ function Agent(options) { if (this.sockets[name]) count += this.sockets[name].length; - if (count > this.maxSockets || freeLen >= this.maxFreeSockets) { + if (count > this.maxSockets) { socket.destroy(); } else if (this.keepSocketAlive(socket)) { - freeSockets = freeSockets || []; - this.freeSockets[name] = freeSockets; + if (freeLen >= this.maxFreeSockets) { + this.freeSockets[name].shift().destroy(); + } else { + freeSockets = freeSockets || []; + this.freeSockets[name] = freeSockets; + } socket[async_id_symbol] = -1; socket._httpMessage = null; this.removeSocket(socket, options);