{"name":"nice-grpc-client-middleware-retry","version":"2.0.2","author":{"name":"Daniel Lytkin","email":"aikoven@deeplay.io"},"license":"MIT","_id":"nice-grpc-client-middleware-retry@2.0.2","maintainers":[{"name":"aikoven","email":"dan.lytkin@gmail.com"}],"homepage":"https://github.com/deeplay-io/nice-grpc#readme","bugs":{"url":"https://github.com/deeplay-io/nice-grpc/issues"},"dist":{"shasum":"1cfd6d69e512590b5a95a0e3de4a5321e2d60b48","tarball":"http://registry-npm.ywwl.com/nice-grpc-client-middleware-retry/download/nice-grpc-client-middleware-retry-2.0.2.tgz","fileCount":7,"integrity":"sha512-9w8DhDm3nFo51CGBLPfCrqZCbOmdTQffzb8HzCzRhQm6s7vo6yAioH75bqEoriYfkhk2Bm8ndQpSLBpAWPHF8g==","signatures":[{"sig":"MEUCIQDNR6njAcT+ZqMMbY6+dNg5JLao3hyDFvXjvPKhEszzLAIgR0N7HwjIuKzeo3ZyDHU9/BeecgL8k3BSi7ZWuZYU7ZM=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":15581,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjx4hOACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2VmqWPA//ewCfiso7lGUqc5I9Ygv7aen4vFDOfriF7ZFW/wG4wUW3tVxI\r\nf1LkbxbYr1CZ7WT08OFpK5DDrr7euc/cWBKz0bUF6m3nznB3KpDqWYpfwUp/\r\n60pSSFn5Amkr5lg6ryybuvbSGGFrBApE7c652R0aSbXv0U58Kz4WyhgddXQq\r\nQG0t/FV0IEL25d4b7HBRTTKcw0d9vPWKSRY7N0FNXSgpJWbJntk8iPx9VWEj\r\nZ2O03mKSGfiEjvNFYzrK9KAaG7YluEqwrylAhOFG4Dtt0H+gmKaBII87+/iJ\r\nPrPUz3vRRscXy2sWw/bbO9TvUNJTchVRz8pByeDRG59ZzMGAB9n6pHYPlbWK\r\ns6YvB1iVyb0FNiESk06gYmx981gjGAv4vWj46e3c7bLMBY8a+as8KPyZ5z2y\r\nLGHCL2YTfChFH9/cAHdql8FRIoOHBXN/S59vP7Ok9uUyg6TGrJ/jS3eMHxoq\r\nUeks9GzR267OSNJNXAosDzUMulSBFDCdNmLYc+hVT19xU9Jt1ymTao0t40Cc\r\nyAqggd9CS30rJsJRqC3LVlUtgLeUNR7eDge1+QjZAZx3GSU25kz6r5Dhx6kn\r\n0rHgS9+cJscJO/Y3YDYNU9wzyonXWQY/1LGJUkzsxjjUSp4MBJDmoz25ZzZg\r\nl1kvlw49/VcFCPnt2ofaWKnAvn1X6Wrx34g=\r\n=NaU+\r\n-----END PGP SIGNATURE-----\r\n","size":5241,"noattachment":false,"key":"/nice-grpc-client-middleware-retry/-/nice-grpc-client-middleware-retry-2.0.2.tgz"},"main":"lib/index.js","gitHead":"090b1090ea4ac6859d7f240386fdac48415c1969","scripts":{"test":"jest","build":"tsc -P tsconfig.build.json","clean":"rimraf lib","prepare":"npm run build:proto","build:proto":"grpc_tools_node_protoc --ts_proto_out=./fixtures --ts_proto_opt=outputServices=generic-definitions,useExactTypes=false,esModuleInterop=true -I fixtures fixtures/*.proto","prepublishOnly":"npm run clean && npm run build && npm test"},"typings":"lib/index.d.ts","_npmUser":{"name":"aikoven","email":"dan.lytkin@gmail.com"},"repository":{"url":"git+https://github.com/deeplay-io/nice-grpc.git","type":"git"},"_npmVersion":"lerna/6.3.0/node@v18.12.1+x64 (darwin)","description":"Retry client middleware for nice-grpc","directories":{},"_nodeVersion":"18.12.1","dependencies":{"nice-grpc-common":"^2.0.1","abort-controller-x":"^0.4.0"},"_hasShrinkwrap":false,"devDependencies":{"ts-proto":"^1.112.0","nice-grpc":"^2.1.0","grpc-tools":"^1.10.0","jest-mock-random":"^1.1.1","@tsconfig/recommended":"^1.0.1"},"_npmOperationalInternal":{"tmp":"tmp/nice-grpc-client-middleware-retry_2.0.2_1674020942482_0.9515356045646122","host":"s3://npm-registry-packages"},"readme":"# nice-grpc-client-middleware-retry [![npm version][npm-image]][npm-url] <!-- omit in toc -->\n\nClient middleware for [nice-grpc](https://github.com/deeplay-io/nice-grpc) that\nadds automatic retries to unary calls.\n[Exponential backoff](https://aws.amazon.com/ru/blogs/architecture/exponential-backoff-and-jitter/)\nis added between retry attempts.\n\n- [Installation](#installation)\n- [Idempotency](#idempotency)\n- [Usage](#usage)\n  - [Infinite retries](#infinite-retries)\n  - [Deadlines](#deadlines)\n  - [Client configuration](#client-configuration)\n\n## Installation\n\n```\nnpm install nice-grpc-client-middleware-retry\n```\n\n## Idempotency\n\nIt is generally not safe to retry calls in case of errors, because the failed\ncall might have reached the server and had an effect on the system. For example,\nan increment operation is not idempotent, since executing it twice will\nincrement by 2. In contrast, a delete operation can be made idempotent, if the\nserver ignores the delete of an already non-existent entity. Any read-only\noperation is inherently idempotent.\n\nIn this middleware, the retries are disabled by default, unless the method is\nmarked as idempotent:\n\n```protobuf\nservice ExampleService {\n  rpc ExampleMethod(ExampleMethodRequest) returns (ExampleMethodResponse) {\n    option idempotency_level = IDEMPOTENT;\n  }\n}\n```\n\nor read-only:\n\n```protobuf\nservice ExampleService {\n  rpc ExampleMethod(ExampleMethodRequest) returns (ExampleMethodResponse) {\n    option idempotency_level = NO_SIDE_EFFECTS;\n  }\n}\n```\n\n> Note that method options currently work only when\n> [compiling with `ts-proto`](https://github.com/deeplay-io/nice-grpc/tree/master/packages/nice-grpc#using-ts-proto).\n\n## Usage\n\n```ts\nimport {\n  createClientFactory,\n  createChannel,\n  ClientError,\n  Status,\n} from 'nice-grpc';\nimport {retryMiddleware} from 'nice-grpc-client-middleware-retry';\n\nconst clientFactory = createClientFactory().use(retryMiddleware);\n\nconst channel = createChannel(address);\nconst client = clientFactory.create(ExampleService, channel);\n\nconst response = await client.exampleMethod(request, {\n  // not needed if the method is marked as idempotent in Protobuf\n  retry: true,\n  // defaults to 1\n  retryMaxAttempts: 5,\n  // defaults to [UNKNOWN, INTERNAL, UNAVAILABLE, CANCELLED]\n  retryableStatuses: [Status.UNAVAILABLE],\n  onRetryableError(error: ClientError, attempt: number, delayMs: number) {\n    logger.error(error, `Call failed (${attempt}), retrying in ${delayMs}ms`);\n  },\n});\n```\n\n### Infinite retries\n\nYou can also set `retryMaxAttempts` to `Infinity` and use\n[`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to\n[cancel](https://github.com/deeplay-io/nice-grpc/tree/master/packages/nice-grpc#cancelling-calls)\nthe retried call:\n\n```ts\nconst abortController = new AbortController();\n\nsetTimeout(() => {\n  abortController.abort();\n}, 1000);\n\nconst response = await client.exampleMethod(request, {\n  retry: true,\n  retryMaxAttempts: Infinity,\n  signal: abortController.signal,\n});\n```\n\n### Deadlines\n\nWhen using this middleware together with\n[deadline middleware](https://github.com/deeplay-io/nice-grpc/tree/master/packages/nice-grpc-client-middleware-deadline),\nmake sure to have them in correct order. This way, if the retry is currently in\na backoff delay, it will be correctly aborted upon deadline:\n\n```ts\nconst clientFactory = createClientFactory()\n  .use(retryMiddleware)\n  .use(deadlineMiddleware);\n```\n\n### Client configuration\n\nInstead of specifying retry options per call, you can configure retries when\ncreating the client:\n\n```ts\nconst clientFactory = createClientFactory().use(retryMiddleware);\n\nconst channel = createChannel(address);\nconst client = clientFactory.create(ExampleService, channel, {\n  // set defaults for all methods\n  '*': {\n    retryMaxAttempts: 5,\n  },\n  // enable retries for particular method\n  exampleMethod: {\n    retry: true,\n  },\n});\n```\n\n[npm-image]: https://badge.fury.io/js/nice-grpc-client-middleware-retry.svg\n[npm-url]: https://badge.fury.io/js/nice-grpc-client-middleware-retry\n","publish_time":1674020942687,"_cnpm_publish_time":1674020942687,"dist-tags":{"latest":"3.1.17"}}