nodejs / help

:sparkles: Need help with Node.js? File an Issue here. :rocket:
1.44k stars 276 forks source link

stop accepting new connections #4355

Closed ORESoftware closed 3 weeks ago

ORESoftware commented 3 months ago

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 commented 3 months ago

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