help
help copied to clipboard
stop accepting new connections
Details
Here: https://stackoverflow.com/questions/78144749/using-node-js-how-to-stop-new-requests-before-closing-existing-ones
The question is:
- is this the best way to stop accepting new connections before ending the existing connections/requests?
- how are "idle" connections defined?
Node.js version
version 20+
Example code
const server = app.listen(3900, '0.0.0.0', () => {
console.log('listening...');
});
process.once('SIGINT', () => {
console.log('got SIGINT.')
setTimeout(() => {
console.log('ending process due to SIGINT + subsequent Timeout.')
process.exit(0);
}, 3000);
server.closeIdleConnections();
server.closeAllConnections();
server.close(err => {
if (err) {
console.error(err);
}
console.log('closed server, and now shutting down due to SIGINT.')
process.exit(0);
});
});
Operating system
darwin/linux
Scope
http
Module and version
http
I updated an answer to the OP: https://stackoverflow.com/questions/78144749/how-to-stop-new-requests-before-closing-existing-ones/78163844#78163844