nest icon indicating copy to clipboard operation
nest copied to clipboard

NATS transport gracefull shutdown

Open alinowrouzii opened this issue 1 year ago • 0 comments
trafficstars

Is there an existing issue that is already proposing this?

  • [x] I have searched the existing issues

Is your feature request related to a problem? Please describe it

When using NATS transport for request/reply, there is no option for gracefully shutting down the server.

The NATS server close method implementation calls natsClient.close() and nullifies the client, meaning all the ongoing requests will be dropped.

Describe the solution you'd like

I want the NATS server to unsubscribe all the subscriptions on the close method if a graceful flag is provided to not listening on any topic and not accepting any new request and then wait for some seconds for all ongoing requests to be performed.

For this, we need to store all the subscription references and use these references when calling the close method

Teachability, documentation, adoption, migration strategy

Just need to add two properties to NatsOptions. One of these is the gracefulShutdown flag, similar to GrpcOptions. Another property is gracePeriod which specifies the time for the close method of the NATS server to be resolved. We wait for that period, and after that, the app.close will free up the resources, such as the database connection, etc.

What is the motivation / use case for changing the behavior?

When you use the shutdown hook for a specific signal, the app.close() is called when the signal is sent. Normally, app.close() first calls server.close(), which closes the server. In the case of NATS, this leads to dropping the ongoing requests. By adding this, the NATS server can be gracefully shut down, and all ongoing requests will be processed.

alinowrouzii avatar Aug 27 '24 13:08 alinowrouzii