Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function Agent(options) {
if (self.sockets[name])
count += self.sockets[name].length;

if (count >= self.maxSockets || freeLen >= self.maxFreeSockets) {
if (count > self.maxSockets || freeLen >= self.maxFreeSockets) {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't the second check be freeLen > self.maxFreeSockets as well?

EDIT: No wait, that would let freeLen get larger than self.maxFreeSockets.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

freeLen >= self.maxFreeSockets is right here.
If freeLen equal maxFreeSockets, should close the socket instead of push it to freeSockets list.

self.removeSocket(socket, options);
socket.destroy();
} else {
Expand Down
53 changes: 53 additions & 0 deletions test/parallel/test-http-agent-maxsockets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
var common = require('../common');
var assert = require('assert');
var http = require('http');

var agent = new http.Agent({
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: 2,
maxFreeSockets: 2
});

var server = http.createServer(function(req, res) {
res.end('hello world');
});

function get(path, callback) {
return http.get({
host: 'localhost',
port: common.PORT,
agent: agent,
path: path
}, callback);
}

var count = 0;
function done() {
if (++count !== 2) {
return;
}
var freepool = agent.freeSockets[Object.keys(agent.freeSockets)[0]];
assert.equal(freepool.length, 2,
'expect keep 2 free sockets, but got ' + freepool.length);
agent.destroy();
server.close();
}

server.listen(common.PORT, function() {
get('/1', function(res) {
assert.equal(res.statusCode, 200);
res.resume();
res.on('end', function() {
process.nextTick(done);
});
});

get('/2', function(res) {
assert.equal(res.statusCode, 200);
res.resume();
res.on('end', function() {
process.nextTick(done);
});
});
});