Skip to content

How to add a single retry for a specific scenario in interceptor? #5856

Description

@dshylov

What 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);
      }
    };
  }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions