help icon indicating copy to clipboard operation
help copied to clipboard

stop accepting new connections

Open ORESoftware opened this issue 2 years ago • 1 comments

Details

Here: https://stackoverflow.com/questions/78144749/using-node-js-how-to-stop-new-requests-before-closing-existing-ones

The question is:

  1. is this the best way to stop accepting new connections before ending the existing connections/requests?
  2. 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

ORESoftware avatar Mar 12 '24 05:03 ORESoftware

I updated an answer to the OP: https://stackoverflow.com/questions/78144749/how-to-stop-new-requests-before-closing-existing-ones/78163844#78163844

ORESoftware avatar Mar 14 '24 22:03 ORESoftware