Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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 <anna@addaleax.net>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
ronag authored and BethGriggs committed Feb 6, 2020
1 parent aad2578 commit 350cfa7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/_http_client.js
Expand Up @@ -620,6 +620,8 @@ function responseKeepAlive(req) {
}
socket.removeListener('close', socketCloseListener);
socket.removeListener('error', socketErrorListener);
socket.removeListener('data', socketOnData);
socket.removeListener('end', socketOnEnd);
socket.once('error', freeSocketErrorListener);
// There are cases where _handle === null. Avoid those. Passing null to
// nextTick() will call getDefaultTriggerAsyncId() to retrieve the id.
Expand Down
18 changes: 15 additions & 3 deletions test/parallel/test-http-agent-keepalive.js
Expand Up @@ -140,9 +140,21 @@ server.listen(0, common.mustCall(() => {

// Check for listener leaks when reusing sockets.
function checkListeners(socket) {
assert.strictEqual(socket.listenerCount('data'), 1);
assert.strictEqual(socket.listenerCount('drain'), 1);
const callback = common.mustCall(() => {
if (!socket.destroyed) {
assert.strictEqual(socket.listenerCount('data'), 0);
assert.strictEqual(socket.listenerCount('drain'), 0);
// Sockets have freeSocketErrorListener.
assert.strictEqual(socket.listenerCount('error'), 1);
// Sockets have onReadableStreamEnd.
assert.strictEqual(socket.listenerCount('end'), 1);
}

socket.off('free', callback);
socket.off('close', callback);
});
assert.strictEqual(socket.listenerCount('error'), 1);
// Sockets have onReadableStreamEnd.
assert.strictEqual(socket.listenerCount('end'), 2);
socket.once('free', callback);
socket.once('close', callback);
}

0 comments on commit 350cfa7

Please sign in to comment.