{"library":"nice-grpc-client-middleware-retry","title":"nice-grpc Client Retry Middleware","description":"nice-grpc-client-middleware-retry is a client-side middleware for the `nice-grpc` TypeScript gRPC library, enabling automatic retries for unary gRPC calls. It integrates seamlessly with the `nice-grpc` client factory to add robust fault tolerance with features like exponential backoff. The current stable version is 3.1.14, and the `nice-grpc` ecosystem, which this package is part of, maintains an active and frequent release cadence, often with updates every few weeks for core packages. A key differentiator is its strong TypeScript support and modern API leveraging Promises and Async Iterables, providing a more ergonomic experience compared to traditional gRPC implementations. It also emphasizes the importance of idempotency, requiring explicit configuration for retries on non-idempotent operations to prevent unintended side effects.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install nice-grpc-client-middleware-retry"],"cli":null},"imports":["import { retryMiddleware } from 'nice-grpc-client-middleware-retry';","import { createClientFactory } from 'nice-grpc';","import { Status } from 'nice-grpc';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { createChannel, createClientFactory, ClientError, Status } from 'nice-grpc';\nimport { retryMiddleware } from 'nice-grpc-client-middleware-retry';\n\n// Imagine you have a generated Protobuf service definition (e.g., from ts-proto)\n// interface ExampleService extends Client<typeof ExampleServiceDefinition> {}\n// const ExampleServiceDefinition = { ... }; // Your service definition\n\n// Mock a service definition for demonstration\nconst ExampleServiceDefinition = {\n  name: 'ExampleService',\n  methods: {\n    exampleMethod: {\n      path: '/ExampleService/ExampleMethod',\n      requestType: {}, // replace with your actual request type\n      responseType: {}, // replace with your actual response type\n      options: {},\n    },\n  },\n};\n\nasync function runClientWithRetries() {\n  const address = process.env.GRPC_SERVER_ADDRESS ?? 'localhost:50051';\n  const channel = createChannel(address);\n\n  const clientFactory = createClientFactory().use(retryMiddleware);\n\n  const client = clientFactory.create(ExampleServiceDefinition, channel);\n\n  let attempt = 0;\n  try {\n    console.log('Attempting to call exampleMethod with retries...');\n    const response = await client.exampleMethod({}, {\n      // Enable retry if method is not marked as idempotent in Protobuf options\n      retry: true, \n      retryMaxAttempts: 3, \n      retryableStatuses: [Status.UNAVAILABLE, Status.INTERNAL, Status.UNKNOWN],\n      onRetry(error: ClientError, attemptNumber: number) {\n        console.warn(`Retry attempt ${attemptNumber} due to ${Status[error.code]}: ${error.details}`);\n      },\n      // Optional: Exponential backoff configuration\n      retryDelay: 100, // initial delay in ms\n      retryDelayMax: 1000, // max delay in ms\n      retryDelayMultiplier: 2, // multiplier for each successive delay\n    });\n    console.log('Successfully received response:', response);\n  } catch (error) {\n    if (error instanceof ClientError) {\n      console.error(`Client error after all retries: ${Status[error.code]} - ${error.details}`);\n    } else {\n      console.error('An unexpected error occurred:', error);\n    }\n  } finally {\n    channel.close();\n  }\n}\n\nrunClientWithRetries();","lang":"typescript","description":"This quickstart demonstrates how to apply the `retryMiddleware` to a `nice-grpc` client, configure retry parameters like max attempts and retryable statuses, and handle potential errors. It also illustrates the `onRetry` callback for logging retry attempts and highlights the configuration options for exponential backoff.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}