From 9cd73fb186b2edd812a29af4768ddcc3ed46cee8 Mon Sep 17 00:00:00 2001 From: Rusty Conover Date: Mon, 27 Jan 2020 10:20:57 -0500 Subject: [PATCH] http: Change agent free socket behavior Rather than destroying the last used socket destroy the oldest socket in the free list in push() on the last recently used socket. --- lib/_http_agent.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) 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);