Help with retry interceptor #485
Unanswered
yiningzhao
asked this question in
Q&A
Replies: 3 comments 4 replies
-
@timostamm excuse me for the direct ping. I'd really appreciate it if you could give me some advice on this. |
Beta Was this translation helpful? Give feedback.
0 replies
-
I believe this will require a bit more complexity as you'll need to manage the import {
Deferred,
RpcError,
RpcInterceptor,
RpcMetadata,
RpcStatus,
UnaryCall,
} from '@protobuf-ts/runtime-rpc';
export const getNewRetryInterceptor = (): RpcInterceptor => ({
interceptUnary(next, method, input, options) {
const maxRetries = 5,
defHeader = new Deferred<RpcMetadata>(),
defMessage = new Deferred<object>(),
defStatus = new Deferred<RpcStatus>(),
defTrailer = new Deferred<RpcMetadata>();
let retries = 0,
result = next(method, input, options);
void (async () => {
while (true) {
try {
await result;
defHeader.resolve(result.headers);
defMessage.resolve(result.response);
defStatus.resolve(result.status);
defTrailer.resolve(result.trailers);
} catch (err) {
if (
err instanceof RpcError &&
err.code == GrpcStatus[GrpcStatus.UNAVAILABLE] &&
++retries <= maxRetries
) {
console.warn(
`gRPC connection unavailable, retrying after 1s... (Retry ${retries} of ${maxRetries})`
);
await sleep(1000);
result = next(method, input, options);
continue;
}
defHeader.rejectPending(err);
defMessage.rejectPending(err);
defStatus.rejectPending(err);
defTrailer.rejectPending(err);
break;
}
}
})();
return new UnaryCall(
method,
options.meta ?? {},
input,
defHeader.promise,
defMessage.promise,
defStatus.promise,
defTrailer.promise
);
},
}); |
Beta Was this translation helpful? Give feedback.
3 replies
-
@jcready , sorry for the mention. But how to do this for also streaming. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hope someone can help me figure out why my interceptor implementation is not working.
I'm trying to add a retry interceptor for our gPRC client. Here's the implementation:
This is the only interceptor for our gPRC client. When I tested it, I saw the retries happening in
handleResponse
but the gPRC error was returned before all retries are finished:Beta Was this translation helpful? Give feedback.
All reactions