From 4ef4a2d137326d897dc5201d81732b675744bed1 Mon Sep 17 00:00:00 2001 From: Rusty Conover Date: Mon, 27 Jan 2020 13:05:19 -0500 Subject: [PATCH] Refactor socket add logic --- lib/_http_agent.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/_http_agent.js b/lib/_http_agent.js index c47c7f0c597448..eff456cf38b089 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -112,11 +112,12 @@ function Agent(options) { if (this.sockets[name]) count += this.sockets[name].length; - if (count > this.maxSockets) { - socket.destroy(); - } else if (this.keepSocketAlive(socket)) { + if (this.keepSocketAlive(socket) && + this.maxFreeSockets > 0 && + count <= this.maxSockets) { if (freeLen >= this.maxFreeSockets) { - this.freeSockets[name].shift().destroy(); + const oldest = this.freeSockets[name].shift(); + oldest.destroy(); } else { freeSockets = freeSockets || []; this.freeSockets[name] = freeSockets;