Skip to content

Commit

Permalink
add grpc mappings for blocking status code (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
shashank11p authored Jan 19, 2024
1 parent 4570eef commit ae81663
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
FilterResult filterResult =
FilterRegistry.getFilter().evaluateRequestHeaders(currentSpan, mapHeaders);
if (filterResult.shouldBlock()) {
// map http codes with grpc codes
// We cannot send custom message in grpc calls
// TODO: map http codes with grpc codes. filterResult.getBlockingStatusCode()
call.close(Status.PERMISSION_DENIED, new Metadata());
call.close(mapHttpToGrpcStatus(filterResult.getBlockingStatusCode()), new Metadata());
@SuppressWarnings("unchecked")
ServerCall.Listener<ReqT> noop = NoopServerCallListener.INSTANCE;
return noop;
Expand All @@ -78,6 +78,32 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(
}
}

/**
* Mapping according to https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md
*/
private static Status mapHttpToGrpcStatus(int httpStatus) {
switch (httpStatus) {
case 400:
return Status.INTERNAL;
case 401:
return Status.UNAUTHENTICATED;
case 403:
return Status.PERMISSION_DENIED;
case 404:
return Status.UNIMPLEMENTED;
case 429:
return Status.UNAVAILABLE;
case 502:
return Status.UNAVAILABLE;
case 503:
return Status.UNAVAILABLE;
case 504:
return Status.UNAVAILABLE;
default:
return Status.UNKNOWN;
}
}

static final class TracingServerCall<ReqT, RespT>
extends ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT> {

Expand Down

0 comments on commit ae81663

Please sign in to comment.