Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improving code readability
  • Loading branch information
imjacobclark committed Dec 5, 2014
commit a4d605e0b165e43fda21c9141ccfaba91fbe229f
27 changes: 15 additions & 12 deletions lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: avaliableFreeSockets -> availableFreeSockets

Copy link
Copy Markdown

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 ^^

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed 🎉

// we have a free socket, so use that.
var socket = this.freeSockets[name].shift();
debug('have free socket');

var hasFreeSockets = !this.freeSockets[name].length
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be accomplished with a comment rather than creating another var to keep track of? I find the // don't leak to be more sloppy than this particular change.

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] = [];
}
Expand Down