Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b840eb7

Browse files
ronagtargos
authored andcommittedJan 14, 2020
http: free listeners on free sockets
Reduced memory usage by ensuring free sockets don't have extra listeners while in the pool. PR-URL: #29259 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 5edfd50 commit b840eb7

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed
 

‎lib/_http_client.js

+2
Original file line numberDiff line numberDiff line change
@@ -612,6 +612,8 @@ function responseKeepAlive(req) {
612612
}
613613
socket.removeListener('close', socketCloseListener);
614614
socket.removeListener('error', socketErrorListener);
615+
socket.removeListener('data', socketOnData);
616+
socket.removeListener('end', socketOnEnd);
615617
socket.once('error', freeSocketErrorListener);
616618
// There are cases where _handle === null. Avoid those. Passing null to
617619
// nextTick() will call getDefaultTriggerAsyncId() to retrieve the id.

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,21 @@ server.listen(0, common.mustCall(() => {
140140

141141
// Check for listener leaks when reusing sockets.
142142
function checkListeners(socket) {
143-
assert.strictEqual(socket.listenerCount('data'), 1);
144-
assert.strictEqual(socket.listenerCount('drain'), 1);
143+
const callback = common.mustCall(() => {
144+
if (!socket.destroyed) {
145+
assert.strictEqual(socket.listenerCount('data'), 0);
146+
assert.strictEqual(socket.listenerCount('drain'), 0);
147+
// Sockets have freeSocketErrorListener.
148+
assert.strictEqual(socket.listenerCount('error'), 1);
149+
// Sockets have onReadableStreamEnd.
150+
assert.strictEqual(socket.listenerCount('end'), 1);
151+
}
152+
153+
socket.off('free', callback);
154+
socket.off('close', callback);
155+
});
145156
assert.strictEqual(socket.listenerCount('error'), 1);
146-
// Sockets have onReadableStreamEnd.
147157
assert.strictEqual(socket.listenerCount('end'), 2);
158+
socket.once('free', callback);
159+
socket.once('close', callback);
148160
}

0 commit comments

Comments
 (0)
Please sign in to comment.