Skip to content

Commit 80ce97f

Browse files
jodevsatargos
authored andcommitted
http: change totalSocketCount only on socket creation/close
PR-URL: #40572 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Voltrex <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: Ricky Zhou <[email protected]>
1 parent d3070d8 commit 80ce97f

4 files changed

+15
-5
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-agent.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ server.listen(0, '127.0.0.1', common.mustCall(function() {
6161
const req = http.request(options);
6262
req.end();
6363

64+
req.on('socket', common.mustCall(function() {
65+
assert.strictEqual(req.agent.totalSocketCount, 1);
66+
}));
67+
6468
req.on('upgrade', common.mustCall(function(res, socket, upgradeHead) {
69+
assert.strictEqual(req.agent.totalSocketCount, 0);
6570
let recvData = upgradeHead;
6671
socket.on('data', function(d) {
6772
recvData += d;
@@ -71,14 +76,13 @@ server.listen(0, '127.0.0.1', common.mustCall(function() {
7176
assert.strictEqual(recvData.toString(), 'nurtzo');
7277
}));
7378

74-
console.log(res.headers);
7579
const expectedHeaders = { 'hello': 'world',
7680
'connection': 'upgrade',
7781
'upgrade': 'websocket' };
7882
assert.deepStrictEqual(expectedHeaders, res.headers);
7983

8084
// Make sure this request got removed from the pool.
81-
assert(!(name in http.globalAgent.sockets));
85+
assert(!(name in req.agent.sockets));
8286

8387
req.on('close', common.mustCall(function() {
8488
socket.end();

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

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ server.listen(0, common.mustCall(function() {
8282
assert.strictEqual(recvData.toString(), expectedRecvData);
8383
}));
8484

85-
console.log(res.headers);
8685
const expectedHeaders = {
8786
hello: 'world',
8887
connection: 'upgrade',

0 commit comments

Comments
 (0)