nest icon indicating copy to clipboard operation
nest copied to clipboard

Provide a callback handle config for NatsServer. Currently if NatsServer disconnects a hybrid application has no way of getting notified.

Open arkh-consensys opened this issue 1 year ago • 4 comments

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 NATS client (js) reaches to a ProtocolError it will close the connection without reconnecting: ref, but the NatsServer used underneath by nestJS framework only logs this event as an error and doesn't provide any handle, callback to recover or exit from it. NestJS ref

This is ok for single server microservices because either they are configured for reconnect or upon a disconnect event the process is killed. However, for a hybrid micorservice which has a NatsServer and an HTTP Server, the process continues serving http request whereas the NatsServer is down.

Describe the solution you'd like

Getting a callback handle errorCallback from Nats config (NatsOptions) and call it upon detecting an error from nats.

  public async handleStatusUpdates(client: Client) {
    for await (const status of client.status()) {
      ....
      if (status.type === 'disconnect' || status.type === 'error') {
        this.logger.error(
          `NatsError: type: "${status.type}", data: "${data}".`,
        );
        
        if(status.type === 'error') {
        this.errorCallback()
        }
        
      } else {
        ....
    }
  }`

### Teachability, documentation, adoption, migration strategy

It is not a breaking change as the default `errorCallback` could be an empty function doing nothing.



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

To be able to react upon the nats server closes connection upon NatsProtocolError or other uncaptured nats errors for a hybrid application.

arkh-consensys avatar Nov 21 '22 11:11 arkh-consensys

The same issue is with the RabbitMQ server. While you can set how many times the server can try to connect to the broker by using maxConnectionAttempts option, if the connection is lost after the initial connection (after a channel is created), the app is trying to retry connection indefinitely (ref).

I would like to react to such an event, or at least have the option to specify that the app should try up to X times.

arkus7 avatar Jan 06 '23 15:01 arkus7

Would you like to create a PR for this issue?

kamilmysliwiec avatar Feb 01 '23 13:02 kamilmysliwiec

Hey @kamilmysliwiec what is the status for this ?

Muhamed23 avatar Nov 10 '23 09:11 Muhamed23

Would you like to create a PR for this issue?

kamilmysliwiec avatar Nov 13 '23 08:11 kamilmysliwiec