How to add a single retry for a specific scenario in interceptor?
See original GitHub issueWhat version of gRPC are you using?
1.20.0
I am working on auth interceptor which responsible for auth tokens on every single call. Sometimes, when a token expired, I have to change auth metadata on the client side and make a retry for this call.
I have already implemented auth interceptor on start and not I’m trying to find out how to make recall when the interceptor found that token expired on onClose(…){…}
Could anybody give advice or other help with this issue?
Interceptor code:
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(final MethodDescriptor<ReqT, RespT> method,
final CallOptions callOptions, final Channel next) {
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
// add a token into the headers
@Override
public void start(final Listener<RespT> responseListener, final Metadata headers) {
if (jwtAuthHeader != null) {
headers.put(ProtoConstants.REQ_HEADER_AUTH, jwtAuthHeader);
} else if (basicAuthHeader != null) {
headers.put(ProtoConstants.REQ_HEADER_AUTH, basicAuthHeader);
}
super.start(new SimpleForwardingClientCallListener<RespT>(responseListener) {
// get jwt token
@Override
public void onHeaders(final Metadata headers) {
final String jwtToken = headers.get(ProtoConstants.RSP_HEADER_JWT_TOKEN);
if (jwtToken != null) {
jwtAuthHeader = getAuthHeader(AUTH_SCHEME_JWT, jwtToken);
}
super.onHeaders(headers);
}
@Override
public void onClose(final Status status, final Metadata trailers) {
if (!status.isOk()) {
switch (status.getCode()) {
case UNAUTHENTICATED:
if (ProtoConstants.STATUS_TOKEN_EXPIRED.equalsIgnoreCase(status.getDescription())) {
// TODO:
// here I am trying to do it
// TODO
}
break;
default:
break;
}
}
super.onClose(status, trailers);
}
}, headers);
}
};
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:13 (7 by maintainers)
Top Results From Across the Web
How To Write Retry Interceptor For Data Stream In OkHttp?
Call call = client.newCall(new Request.Builder().url(url).get().build()); Response response = call.execute(); // PROBLEM DOES NOT OCCUR THERE // ...
Read more >Implementing auto retry in Java EE applications
Define a simple annotation which represents the 'retry policy metadata' e.g. number of retries · Define an interceptor with implementation to ...
Read more >How to Customize Feign's Retry Mechanism - Medium
In this post I will talk about enabling retry mechanism for feign Client. By instinct what we can do is update our business...
Read more >Using the Spring RestTemplate Interceptor - Baeldung
Retrying the requests with a configurable back off strategy; Request denial based on certain request parameters; Altering the request URL ...
Read more >Retrying HTTP Requests a Fixed Number of Times With a ...
When the request fails, the handleRetryError() in the RetryService is triggered. We are passing the “delay between the retries” as an argument for...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@dshylov My point is client-interceptor may not be the best place for this kind of logic given the constraints. Can you check the JWT “exp” field and if the token has expired or about to expire in X milliseconds can you use basic auth in the 1st attempt itself? This logic will be in your
start()
method.Hi @dshylov , I’m providing an interceptor that buffers the message and retries, you can add your logic in
needToRetry(status, trailers)
. Hope this could solve your problem.