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

http: avoid create new socket #1242

Closed
wants to merge 1 commit into from
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);
});
});
});