I am trying to implement a stream server interceptor in Go, and its job is to reject the RPC (and therefore close the stream) if the client didn’t provide a certain header.
Based on the grpc-go interceptor example, it looks like the way to do this would be for the interceptor function to simply return an error.
I’ve implemented my interceptor nearly identical to the example, except for checking for my desired header of course, and return an error if the header is not present. However, even though my interceptor gets to that return
statement, I don’t see the resulting error/status logged on either the client or the server, and more importantly, the client receives no error as a result of invoking the RPC.
Am I missing something here? I previously had implemented the header check directly in the server handler function, and when the header wasn’t present, the client did indeed see a stream error. I am trying to reproduce that same result with a proper interceptor.
Here is my interceptor implementation:
func ClientMetadataInterceptor(srv any, ss grpc.ServerStream, _ *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
md, ok := metadata.FromIncomingContext(ss.Context())
if !ok {
return status.Errorf(codes.InvalidArgument, "no incoming metadata in rpc context")
}
var headerValue string
if len(md["required-header"]) > 0 {
headerValue = md["required-header"][0]
} else {
return status.Error(codes.InvalidArgument, "missing required header")
}
md.Append("new-header-key", headerValue)
ctx := metadata.NewIncomingContext(ss.Context(), md)
// Call the handler to complete the normal execution of the RPC
err := handler(srv, &wrappedStream{ss, ctx})
return err
}
1