diff options
author | Shogun <paolo@cowtech.it> | 2022-04-28 13:05:55 +0300 |
---|---|---|
committer | Shogun <paolo@cowtech.it> | 2022-05-03 15:04:07 +0300 |
commit | f714a0fa6e5630aa959ee4bda10345ba81161fc5 (patch) | |
tree | c6acfe431b006553d9e55acae8e380c6cd5be271 /lib | |
parent | 6ebe5a4ff0f2f196eb8502c067da4f2cca3d241a (diff) |
http: added connection closing methods
Fixes: https://github.com/nodejs/node/issues/41578
PR-URL: https://github.com/nodejs/node/pull/42812
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/_http_server.js | 29 | ||||
-rw-r--r-- | lib/https.js | 4 |
2 files changed, 29 insertions, 4 deletions
diff --git a/lib/_http_server.js b/lib/_http_server.js index 84be32c78c4..c8dc22929bf 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -409,10 +409,11 @@ function storeHTTPOptions(options) { function setupConnectionsTracking(server) { // Start connection handling server[kConnections] = new ConnectionsList(); - if (server.headersTimeout > 0 || server.requestTimeout > 0) { - server[kConnectionsCheckingInterval] = - setInterval(checkConnections.bind(server), server.connectionsCheckingInterval).unref(); - } + + // 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(); } function Server(options, requestListener) { @@ -458,6 +459,22 @@ Server.prototype.close = function() { ReflectApply(net.Server.prototype.close, this, arguments); }; +Server.prototype.closeAllConnections = function() { + const connections = this[kConnections].all(); + + for (let i = 0, l = connections.length; i < l; i++) { + connections[i].socket.destroy(); + } +}; + +Server.prototype.closeIdleConnections = function() { + const connections = this[kConnections].idle(); + + for (let i = 0, l = connections.length; i < l; i++) { + connections[i].socket.destroy(); + } +}; + Server.prototype.setTimeout = function setTimeout(msecs, callback) { this.timeout = msecs; if (callback) @@ -489,6 +506,10 @@ Server.prototype[EE.captureRejectionSymbol] = function(err, event, ...args) { }; function checkConnections() { + if (this.headersTimeout === 0 && this.requestTimeout === 0) { + return; + } + const expired = this[kConnections].expired(this.headersTimeout, this.requestTimeout); for (let i = 0; i < expired.length; i++) { diff --git a/lib/https.js b/lib/https.js index 74ce93baaaf..6ca3f335d0f 100644 --- a/lib/https.js +++ b/lib/https.js @@ -87,6 +87,10 @@ function Server(opts, requestListener) { ObjectSetPrototypeOf(Server.prototype, tls.Server.prototype); ObjectSetPrototypeOf(Server, tls.Server); +Server.prototype.closeAllConnections = HttpServer.prototype.closeAllConnections; + +Server.prototype.closeIdleConnections = HttpServer.prototype.closeIdleConnections; + Server.prototype.setTimeout = HttpServer.prototype.setTimeout; /** |