Skip to content
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

Closed
wants to merge 2 commits into from
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
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) {

Choose a reason for hiding this comment

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

typo: avaliableFreeSockets -> availableFreeSockets

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

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