Skip to content

Commit 09ab76b

Browse files
committed
http: change totalSocketCount only on socket creation/close
1 parent ced518c commit 09ab76b

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

lib/_http_agent.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,6 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
284284
this.reuseSocket(socket, req);
285285
setRequestSocket(this, req, socket);
286286
ArrayPrototypePush(this.sockets[name], socket);
287-
this.totalSocketCount++;
288287
} else if (sockLen < this.maxSockets &&
289288
this.totalSocketCount < this.maxTotalSockets) {
290289
debug('call onSocket', sockLen, freeLen);
@@ -383,6 +382,7 @@ function installListeners(agent, s, options) {
383382
// This is the only place where sockets get removed from the Agent.
384383
// If you want to remove a socket from the pool, just close it.
385384
// All socket errors end in a close event anyway.
385+
agent.totalSocketCount--;
386386
agent.removeSocket(s, options);
387387
}
388388
s.on('close', onClose);
@@ -406,6 +406,7 @@ function installListeners(agent, s, options) {
406406
// (defined by WebSockets) where we need to remove a socket from the
407407
// pool because it'll be locked up indefinitely
408408
debug('CLIENT socket onRemove');
409+
agent.totalSocketCount--;
409410
agent.removeSocket(s, options);
410411
s.removeListener('close', onClose);
411412
s.removeListener('free', onFree);
@@ -438,7 +439,6 @@ Agent.prototype.removeSocket = function removeSocket(s, options) {
438439
// Don't leak
439440
if (sockets[name].length === 0)
440441
delete sockets[name];
441-
this.totalSocketCount--;
442442
}
443443
}
444444
}

test/parallel/test-http-agent-keepalive.js

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ function checkDataAndSockets(body) {
5959
assert.strictEqual(body.toString(), 'hello world');
6060
assert.strictEqual(agent.sockets[name].length, 1);
6161
assert.strictEqual(agent.freeSockets[name], undefined);
62+
assert.strictEqual(agent.totalSocketCount, 1);
6263
}
6364

6465
function second() {
@@ -73,6 +74,7 @@ function second() {
7374
process.nextTick(common.mustCall(() => {
7475
assert.strictEqual(agent.sockets[name], undefined);
7576
assert.strictEqual(agent.freeSockets[name].length, 1);
77+
assert.strictEqual(agent.totalSocketCount, 1);
7678
remoteClose();
7779
}));
7880
}));
@@ -91,10 +93,12 @@ function remoteClose() {
9193
process.nextTick(common.mustCall(() => {
9294
assert.strictEqual(agent.sockets[name], undefined);
9395
assert.strictEqual(agent.freeSockets[name].length, 1);
96+
assert.strictEqual(agent.totalSocketCount, 1);
9497
// Waiting remote server close the socket
9598
setTimeout(common.mustCall(() => {
9699
assert.strictEqual(agent.sockets[name], undefined);
97100
assert.strictEqual(agent.freeSockets[name], undefined);
101+
assert.strictEqual(agent.totalSocketCount, 0);
98102
remoteError();
99103
}), common.platformTimeout(200));
100104
}));
@@ -110,10 +114,12 @@ function remoteError() {
110114
assert.strictEqual(err.message, 'socket hang up');
111115
assert.strictEqual(agent.sockets[name].length, 1);
112116
assert.strictEqual(agent.freeSockets[name], undefined);
117+
assert.strictEqual(agent.totalSocketCount, 1);
113118
// Wait socket 'close' event emit
114119
setTimeout(common.mustCall(() => {
115120
assert.strictEqual(agent.sockets[name], undefined);
116121
assert.strictEqual(agent.freeSockets[name], undefined);
122+
assert.strictEqual(agent.totalSocketCount, 0);
117123
server.close();
118124
}), common.platformTimeout(1));
119125
}));
@@ -132,6 +138,7 @@ server.listen(0, common.mustCall(() => {
132138
process.nextTick(common.mustCall(() => {
133139
assert.strictEqual(agent.sockets[name], undefined);
134140
assert.strictEqual(agent.freeSockets[name].length, 1);
141+
assert.strictEqual(agent.totalSocketCount, 1);
135142
second();
136143
}));
137144
}));

test/parallel/test-http-upgrade-client.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,20 @@ server.listen(0, common.mustCall(function() {
6666
const countdown = new Countdown(headers.length, () => server.close());
6767

6868
headers.forEach(function(h) {
69+
const agent = new http.Agent();
6970
const req = http.get({
71+
agent,
7072
port: port,
7173
headers: h
7274
});
7375
let sawUpgrade = false;
76+
req.on('socket', common.mustCall(function() {
77+
assert.strictEqual(agent.totalSocketCount, 1);
78+
}));
79+
7480
req.on('upgrade', common.mustCall(function(res, socket, upgradeHead) {
81+
assert.strictEqual(agent.totalSocketCount, 0);
82+
7583
sawUpgrade = true;
7684
let recvData = upgradeHead;
7785
socket.on('data', function(d) {
@@ -82,7 +90,6 @@ server.listen(0, common.mustCall(function() {
8290
assert.strictEqual(recvData.toString(), expectedRecvData);
8391
}));
8492

85-
console.log(res.headers);
8693
const expectedHeaders = {
8794
hello: 'world',
8895
connection: 'upgrade',

0 commit comments

Comments
 (0)