-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improving code readability on lib/_http_agent.js #83
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could this be accomplished with a comment rather than creating another I don't like this pattern... but I applaud the effort of improving readability and variable naming as you have above. 👍 |
||
|
||
// 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] = []; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo:
avaliableFreeSockets
->availableFreeSockets
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's done consistently though, haha ^^
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed 🎉