Skip to content
Permalink
Browse files

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 BridgeAR committed Aug 21, 2019
1 parent 892e7b0 commit 1b720aa80202dd5ac6e8823a197ae6d530ac4b70
Showing with 17 additions and 3 deletions.
  1. +2 −0 lib/_http_client.js
  2. +15 −3 test/parallel/test-http-agent-keepalive.js
@@ -618,6 +618,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.
@@ -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 1b720aa

Please sign in to comment.
You can’t perform that action at this time.