Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 80ce97f

Browse filesBrowse files
jodevsatargos
authored andcommitted
http: change totalSocketCount only on socket creation/close
PR-URL: #40572 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Voltrex <mohammadkeyvanzade94@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Minwoo Jung <nodecorelab@gmail.com> Reviewed-By: Ricky Zhou <0x19951125@gmail.com>
1 parent d3070d8 commit 80ce97f
Copy full SHA for 80ce97f

File tree

Expand file treeCollapse file tree

4 files changed

+15
-5
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+15
-5
lines changed
Open diff view settings
Collapse file

‎lib/_http_agent.js‎

Copy file name to clipboardExpand all lines: lib/_http_agent.js
+2-2Lines changed: 2 additions & 2 deletions
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
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-http-agent-keepalive.js
+7Lines changed: 7 additions & 0 deletions
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
}));
Collapse file

‎test/parallel/test-http-upgrade-agent.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-http-upgrade-agent.js
+6-2Lines changed: 6 additions & 2 deletions
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();
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-http-upgrade-client.js
-1Lines changed: 0 additions & 1 deletion
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.