Skip to content

Commit

Permalink
http: start connections checking interval on listen
Browse files Browse the repository at this point in the history
  • Loading branch information
ShogunPanda committed Jul 3, 2023
1 parent 32eb492 commit e86f329
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 6 deletions.
19 changes: 14 additions & 5 deletions lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,14 +497,14 @@ function storeHTTPOptions(options) {
}
}

function setupConnectionsTracking(server) {
function setupConnectionsTracking() {
// Start connection handling
server[kConnections] = new ConnectionsList();
this[kConnections] = new ConnectionsList();

// This checker is started without checking whether any headersTimeout or requestTimeout is non zero
// otherwise it would not be started if such timeouts are modified after createServer.
server[kConnectionsCheckingInterval] =
setInterval(checkConnections.bind(server), server.connectionsCheckingInterval).unref();
this[kConnectionsCheckingInterval] =
setInterval(checkConnections.bind(this), this.connectionsCheckingInterval).unref();
}

function httpServerPreClose(server) {
Expand Down Expand Up @@ -542,11 +542,12 @@ function Server(options, requestListener) {
this.httpAllowHalfOpen = false;

this.on('connection', connectionListener);
this.on('listening', setupConnectionsTracking);

this.timeout = 0;
this.maxHeadersCount = null;
this.maxRequestsPerSocket = 0;
setupConnectionsTracking(this);

this[kUniqueHeaders] = parseUniqueHeadersOption(options.uniqueHeaders);
}
ObjectSetPrototypeOf(Server.prototype, net.Server.prototype);
Expand All @@ -558,6 +559,10 @@ Server.prototype.close = function() {
};

Server.prototype.closeAllConnections = function() {
if (!this[kConnections]) {
return;
}

const connections = this[kConnections].all();

for (let i = 0, l = connections.length; i < l; i++) {
Expand All @@ -566,6 +571,10 @@ Server.prototype.closeAllConnections = function() {
};

Server.prototype.closeIdleConnections = function() {
if (!this[kConnections]) {
return;
}

const connections = this[kConnections].idle();

for (let i = 0, l = connections.length; i < l; i++) {
Expand Down
3 changes: 2 additions & 1 deletion lib/https.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ function Server(opts, requestListener) {

this.timeout = 0;
this.maxHeadersCount = null;
setupConnectionsTracking(this);
this.on('listening', setupConnectionsTracking);
}

ObjectSetPrototypeOf(Server.prototype, tls.Server.prototype);
ObjectSetPrototypeOf(Server, tls.Server);

Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-http-server-connections-checking-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

// Flags: --expose-gc

// Check that creating a server without listening is not leaking resources.

require('../common');
const onGC = require('../common/ongc');
const Countdown = require('../common/countdown');

const http = require('http');
const max = 100;
const countdown = new Countdown(max, () => {});

for (let i = 0; i <= max; i++) {
const server = http.createServer((req, res) => {});
onGC(server, { ongc: countdown.dec.bind(countdown) });
}

global.gc();
25 changes: 25 additions & 0 deletions test/parallel/test-https-server-connections-checking-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

// Flags: --expose-gc

// Check that creating a server without listening is not leaking resources.

const common = require('../common');

if (!common.hasCrypto) {
common.skip('missing crypto');
}

const onGC = require('../common/ongc');
const Countdown = require('../common/countdown');

const https = require('https');
const max = 100;
const countdown = new Countdown(max, () => {});

for (let i = 0; i <= max; i++) {
const server = https.createServer((req, res) => {});
onGC(server, { ongc: countdown.dec.bind(countdown) });
}

global.gc();

0 comments on commit e86f329

Please sign in to comment.