From a4d605e0b165e43fda21c9141ccfaba91fbe229f Mon Sep 17 00:00:00 2001 From: Jacob Clark Date: Fri, 5 Dec 2014 14:22:15 +0000 Subject: [PATCH 1/2] Improving code readability --- lib/_http_agent.js | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lib/_http_agent.js b/lib/_http_agent.js index 6fbff37d85db26..dde198bf6d7739 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -78,13 +78,15 @@ function Agent(options) { req.shouldKeepAlive && !socket.destroyed && self.options.keepAlive) { + var freeSockets = self.freeSockets[name]; - var freeLen = freeSockets ? freeSockets.length : 0; - var count = freeLen; + var totalSocketLength = freeSockets ? freeSockets.length : 0; + var socketsAvaliable = totalSocketLength < this.maxSockets || totalSocketLength >= self.maxFreeSockets; + if (self.sockets[name]) - count += self.sockets[name].length; + totalSocketLength += self.sockets[name].length; - if (count >= self.maxSockets || freeLen >= self.maxFreeSockets) { + if (socketsAvaliable) { self.removeSocket(socket, options); socket.destroy(); } else { @@ -145,28 +147,29 @@ Agent.prototype.addRequest = function(req, options) { this.sockets[name] = []; } - var freeLen = this.freeSockets[name] ? this.freeSockets[name].length : 0; - var sockLen = freeLen + this.sockets[name].length; + var avaliableFreeSockets = this.freeSockets[name] ? this.freeSockets[name].length : 0; + var totalSocketLength = avaliableFreeSockets + this.sockets[name].length; + var socketsAvaliable = totalSocketLength < this.maxSockets; - if (freeLen) { + if (avaliableFreeSockets) { // we have a free socket, so use that. var socket = this.freeSockets[name].shift(); debug('have free socket'); + var hasFreeSockets = !this.freeSockets[name].length + // don't leak - if (!this.freeSockets[name].length) + if (hasFreeSockets) delete this.freeSockets[name]; socket.ref(); req.onSocket(socket); this.sockets[name].push(socket); - } else if (sockLen < this.maxSockets) { - debug('call onSocket', sockLen, freeLen); - // If we are under maxSockets create a new one. + } else if (socketsAvaliable) { + debug('call onSocket', totalSocketLength, avaliableFreeSockets); req.onSocket(this.createSocket(req, options)); } else { debug('wait for socket'); - // We are over limit so we'll add it to the queue. if (!this.requests[name]) { this.requests[name] = []; } From de1a4b0f50c2bd74197e8defe5e3fe4370295632 Mon Sep 17 00:00:00 2001 From: Jacob Clark Date: Fri, 5 Dec 2014 14:32:35 +0000 Subject: [PATCH 2/2] Fixing typos --- lib/_http_agent.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/_http_agent.js b/lib/_http_agent.js index dde198bf6d7739..5ed77033648e6a 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -81,12 +81,12 @@ function Agent(options) { var freeSockets = self.freeSockets[name]; var totalSocketLength = freeSockets ? freeSockets.length : 0; - var socketsAvaliable = totalSocketLength < this.maxSockets || totalSocketLength >= self.maxFreeSockets; + var socketsAvailable = totalSocketLength < this.maxSockets || totalSocketLength >= self.maxFreeSockets; if (self.sockets[name]) totalSocketLength += self.sockets[name].length; - if (socketsAvaliable) { + if (socketsAvailable) { self.removeSocket(socket, options); socket.destroy(); } else { @@ -147,11 +147,11 @@ Agent.prototype.addRequest = function(req, options) { this.sockets[name] = []; } - var avaliableFreeSockets = this.freeSockets[name] ? this.freeSockets[name].length : 0; - var totalSocketLength = avaliableFreeSockets + this.sockets[name].length; - var socketsAvaliable = totalSocketLength < this.maxSockets; + var availableFreeSockets = this.freeSockets[name] ? this.freeSockets[name].length : 0; + var totalSocketLength = availableFreeSockets + this.sockets[name].length; + var socketsAvailable = totalSocketLength < this.maxSockets; - if (avaliableFreeSockets) { + if (availableFreeSockets) { // we have a free socket, so use that. var socket = this.freeSockets[name].shift(); debug('have free socket'); @@ -165,8 +165,8 @@ Agent.prototype.addRequest = function(req, options) { socket.ref(); req.onSocket(socket); this.sockets[name].push(socket); - } else if (socketsAvaliable) { - debug('call onSocket', totalSocketLength, avaliableFreeSockets); + } else if (socketsAvailable) { + debug('call onSocket', totalSocketLength, availableFreeSockets); req.onSocket(this.createSocket(req, options)); } else { debug('wait for socket');