Skip to content

Commit

Permalink
feat(grpc-client): Add retry mechanism to grpc-client
Browse files Browse the repository at this point in the history
  • Loading branch information
Arun-KumarH committed Mar 18, 2024
1 parent ffd61e7 commit b3d7320
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/grpc-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"lodash": "^4.17.21",
"nice-grpc": "^2.1.7",
"nice-grpc-client-middleware-deadline": "^2.0.10",
"nice-grpc-client-middleware-retry": "^3.1.7",
"protobufjs": "^7.2.6",
"retry": "^0.13.1",
"rxjs": "^7.8.1",
Expand Down
17 changes: 16 additions & 1 deletion packages/grpc-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createClientFactory as createClientFactoryInternal,
DefaultCallOptions,
createChannel,
ClientError,
} from 'nice-grpc';
import { CompatServiceDefinition, NormalizedServiceDefinition } from 'nice-grpc/lib/service-definitions';
import {
Expand All @@ -15,6 +16,7 @@ import {
} from './middleware';
import { createLogger } from '@restorecommerce/logger';
import { deadlineMiddleware } from 'nice-grpc-client-middleware-deadline';
import { retryMiddleware } from 'nice-grpc-client-middleware-retry';

export interface GrpcClientConfig {
logger: ReturnType<typeof createLogger>;
Expand All @@ -31,13 +33,26 @@ export function createClient<Service extends CompatServiceDefinition>(
let factory = createClientFactoryInternal()
.use<WithRequestID>(loggingMiddleware(config.logger, config.omittedFields))
.use<WithRequestID>(tracingMiddleware)
.use<WithRequestID>(metaMiddleware);
.use<WithRequestID>(metaMiddleware)
.use<WithRequestID>(retryMiddleware);

if (config.timeout) {
factory = factory.use(deadlineMiddleware);
factory = factory.use(internalDeadlineMiddleware(config.timeout));
}

if (!defaultCallOptions) {
defaultCallOptions = {};
}
defaultCallOptions = Object.assign(defaultCallOptions, {
'*': {
retryMaxAttempts: 5,
retry: true,
},
onRetryableError(error: ClientError, attempt: number, delayMs: number) {
config.logger.error(`Call failed (${attempt}), retrying in ${delayMs}ms`, { code: error.code, message: error.message, stack: error.stack });
}
});
return factory.create(definition, channel, defaultCallOptions);
}

Expand Down

0 comments on commit b3d7320

Please sign in to comment.