Skip to content

Commit

Permalink
feat: add retry middleware & configure retries in waitForOperation ut…
Browse files Browse the repository at this point in the history
…ility function
  • Loading branch information
nikolaymatrosov committed Feb 13, 2023
1 parent 74e054f commit 8d1d280
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 15 deletions.
61 changes: 53 additions & 8 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"luxon": "^2.2.0",
"nice-grpc": "^1.0.6",
"nice-grpc-client-middleware-deadline": "^1.0.6",
"nice-grpc-client-middleware-retry": "^1.1.2",
"protobufjs": "^6.11.3",
"utility-types": "^3.10.0"
},
Expand Down
14 changes: 10 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
import {
ChannelCredentials, ChannelOptions, Client, ServiceDefinition,
ChannelCredentials,
ChannelOptions,
Client,
ServiceDefinition,
} from '@grpc/grpc-js';
import { RawClient } from 'nice-grpc';
import { NormalizedServiceDefinition } from 'nice-grpc/lib/service-definitions';
import { DeadlineOptions } from 'nice-grpc-client-middleware-deadline';
import { RetryOptions } from 'nice-grpc-client-middleware-retry';
import { NormalizedServiceDefinition } from 'nice-grpc/lib/service-definitions';

export interface TokenService {
getToken: () => Promise<string>;
}

export interface GeneratedServiceClientCtor<T extends ServiceDefinition> {
service: T

new(
address: string,
credentials: ChannelCredentials,
options?: Partial<ChannelOptions>,
): Client;
service: T
}

export interface IIAmCredentials {
Expand Down Expand Up @@ -59,4 +64,5 @@ export type SessionConfig =
| ServiceAccountCredentialsConfig
| GenericCredentialsConfig;

export type WrappedServiceClientType<S extends ServiceDefinition> = RawClient<NormalizedServiceDefinition<S>, DeadlineOptions>;
// eslint-disable-next-line max-len
export type WrappedServiceClientType<S extends ServiceDefinition> = RawClient<NormalizedServiceDefinition<S>, DeadlineOptions & RetryOptions>;
5 changes: 4 additions & 1 deletion src/utils/client-factory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { createClientFactory } from 'nice-grpc';
import { deadlineMiddleware } from 'nice-grpc-client-middleware-deadline';
import { retryMiddleware } from 'nice-grpc-client-middleware-retry';

export const clientFactory = createClientFactory().use(deadlineMiddleware);
export const clientFactory = createClientFactory()
.use(retryMiddleware)
.use(deadlineMiddleware);
11 changes: 9 additions & 2 deletions src/utils/operation/wait-for.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Session } from '../../session';
import {
cloudApi,
serviceClients,
} from '../..';
import { Operation } from '../../generated/yandex/cloud/operation/operation';
import { serviceClients, cloudApi } from '../..';
import { Session } from '../../session';

const { operation: { operation_service: { GetOperationRequest } } } = cloudApi;

Expand All @@ -18,6 +21,10 @@ export const waitForOperation = (op: Operation, session: Session, timeoutMs: num
GetOperationRequest.fromPartial({
operationId: op.id,
}),
{
retry: true,
retryMaxAttempts: 3,
},
);

checksCount++;
Expand Down

0 comments on commit 8d1d280

Please sign in to comment.