Jay P
Jay P

Reputation: 626

NestJs how to configure GRPC client interceptors

I am having an issue where i am getting intermittent connection issues when communicating over GRPC between 2 NestJs servers. I have opened a previous question on stackoverflow in which someone has lead me to a thread which seems to indicate that there is an issue with @grpc-js library.

The suggestiong for a fix has been to downgrade node to 16.8.0, although this worked for another user, it did not work for me. It was suggested that I create a GRPC interceptor, however, i cannot work out how to do this within the NestJS framework.

how can i create a client interceptor?

Here is how i have set up my client connections in NestJS

const clientOptions: ClientsModuleOptions = [
  {
    name: 'HP_USERS_PACKAGE',
    transport: Transport.GRPC,
    options: {
      url: `${env.USERS_SERVICE}:${config.users.port}`,
      package: config.users.v1.package,
      protoPath: config.users.v1.protoPath,
      loader: {
        includeDirs: [config.protoLoaderDir],
        enums: String,
      },
    },
  },
  {
    name: 'HP_STAGING_PACKAGE',
    transport: Transport.GRPC,
    options: {
      url: `${env.STAGING_SERVICE}:${config.staging.port}`,
      package: config.staging.v1.package,
      protoPath: config.staging.v1.protoPath,
      loader: {
        includeDirs: [config.protoLoaderDir],
        enums: String,
      },
    },
  }]

Initial thread with my disconnection issue:

https://stackoverflow.com/questions/69307780/grpc-intermittent-connection-read-econnrese-error

Thread showing other user having same issue with possible solutions

https://github.com/grpc/grpc-node/issues/1907#issuecomment-927361244

example for creating GRPC client interceptors

https://github.com/grpc/proposal/blob/master/L5-node-client-interceptors.md#examples

Upvotes: 1

Views: 2406

Answers (1)

Fib
Fib

Reputation: 807

You can add interceptor in options of your client instance:

const clientOptions: ClientsModuleOptions = [{
     name: 'HP_USERS_PACKAGE',
     transport: Transport.GRPC,
     options: {
         interceptors: [retryInterceptor],
         url: `${env.USERS_SERVICE}:${config.users.port}`,
         package: config.users.v1.package,
         protoPath: config.users.v1.protoPath,
         loader: {
             includeDirs: [config.protoLoaderDir],
             enums: String,
         },
     },
}]`

The implementation of retry interceptor can the same as here: https://github.com/grpc/grpc-node/issues/284

Upvotes: 1

Related Questions