Skip to content

Commit 7956a13

Browse files
fengmk2Fishrock123
authored andcommitted
http: logically respect maxSockets
Allows the number of pooled free sockets to equal maxSockets. Previously it would only allow maxSockets - 1. PR-URL: #1242 Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Christian Tellnes <[email protected]>
1 parent cd60ff0 commit 7956a13

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

lib/_http_agent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function Agent(options) {
6565
if (self.sockets[name])
6666
count += self.sockets[name].length;
6767

68-
if (count >= self.maxSockets || freeLen >= self.maxFreeSockets) {
68+
if (count > self.maxSockets || freeLen >= self.maxFreeSockets) {
6969
self.removeSocket(socket, options);
7070
socket.destroy();
7171
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var common = require('../common');
2+
var assert = require('assert');
3+
var http = require('http');
4+
5+
var agent = new http.Agent({
6+
keepAlive: true,
7+
keepAliveMsecs: 1000,
8+
maxSockets: 2,
9+
maxFreeSockets: 2
10+
});
11+
12+
var server = http.createServer(function(req, res) {
13+
res.end('hello world');
14+
});
15+
16+
function get(path, callback) {
17+
return http.get({
18+
host: 'localhost',
19+
port: common.PORT,
20+
agent: agent,
21+
path: path
22+
}, callback);
23+
}
24+
25+
var count = 0;
26+
function done() {
27+
if (++count !== 2) {
28+
return;
29+
}
30+
var freepool = agent.freeSockets[Object.keys(agent.freeSockets)[0]];
31+
assert.equal(freepool.length, 2,
32+
'expect keep 2 free sockets, but got ' + freepool.length);
33+
agent.destroy();
34+
server.close();
35+
}
36+
37+
server.listen(common.PORT, function() {
38+
get('/1', function(res) {
39+
assert.equal(res.statusCode, 200);
40+
res.resume();
41+
res.on('end', function() {
42+
process.nextTick(done);
43+
});
44+
});
45+
46+
get('/2', function(res) {
47+
assert.equal(res.statusCode, 200);
48+
res.resume();
49+
res.on('end', function() {
50+
process.nextTick(done);
51+
});
52+
});
53+
});

0 commit comments

Comments
 (0)