The `nice-grpc-client-middleware-deadline` package provides client-side middleware for the `nice-grpc` library, enabling developers to easily set and enforce deadlines for outgoing gRPC calls. When a configured deadline is exceeded, the in-flight gRPC call is automatically cancelled, and the client receives a `ClientError` with the `DEADLINE_EXCEEDED` status code. This functionality is crucial for building resilient microservices by preventing requests from running indefinitely, which could otherwise lead to resource exhaustion and cascading failures. The package is currently at version 2.0.18, with a sustainable maintenance record and consistent updates aligning with the `nice-grpc` ecosystem's focus on modern TypeScript, Promises, and Async Iterables. It is a key component in `nice-grpc`'s modular approach to client-side concerns, offering a clear differentiator in its modern API design compared to more traditional gRPC implementations.
npm install nice-grpc-client-middleware-deadlineVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to integrate `createDeadlineClientMiddleware` with a `nice-grpc` client. It shows how to apply the middleware to a client factory, create a client, and then make RPC calls with both per-call and factory-wide default deadlines using the `deadline` option.
Migrate your project to use ES modules (`import`/`export`) or configure Node.js to treat your files as ESM (e.g., via `"type": "module"` in `package.json` or by using `.mjs` file extensions). Dynamic `import()` can also be used in CommonJS contexts to load ESM modules.
Always ensure the `deadline` value is a future timestamp. For example, `Date.now() + 5000` for a 5-second deadline. Avoid passing `0` or negative values unless immediate failure is intended.
Ensure server-side gRPC handlers pass `ServerCallContext.CancellationToken` to any asynchronous operations (e.g., database queries, other service calls) to enable proper propagation of cancellation and prevent resource wastage on the server.
Carefully consider the nature of streaming calls. For long-lived or potentially infinite streams, avoid aggressive deadlines, or apply them only to the initial connection phase. For finite streams, ensure the deadline is generous enough to accommodate expected data transmission and processing times.
Update your project to use ES module `import` statements and ensure your `package.json` includes `"type": "module"`, or rename your file to `.mjs`. Alternatively, use dynamic `import()` within your CJS file: `const { createDeadlineClientMiddleware } = await import('nice-grpc-client-middleware-deadline');`Review the configured `deadline` value. Ensure it provides sufficient time for the server to process the request and send a response. Consider network latency and server processing time when setting deadlines. For debugging, temporarily increase the deadline to confirm if it's a timeout issue versus a different server-side error.
Ensure `createDeadlineClientMiddleware` is applied to your `createClientFactory` instance *before* creating the client. The middleware is responsible for extending the `CallOptions` type to include the `deadline` property.