Class GrpcServerContextPropagationInterceptor

java.lang.Object
io.micronaut.grpc.server.propagation.GrpcServerContextPropagationInterceptor
All Implemented Interfaces:
io.grpc.ServerInterceptor, io.micronaut.core.order.Ordered

@Singleton @Requires(property="grpc.server.context-propagation.enabled", value="true", defaultValue="true") public class GrpcServerContextPropagationInterceptor extends Object implements io.grpc.ServerInterceptor, io.micronaut.core.order.Ordered
Carries the Micronaut PropagatedContext that outer server interceptors make current when they start a call into every later event of that call, so that the service method sees it.

gRPC delivers a call's events (onMessage, onHalfClose, onCancel, onComplete and onReady) as separate tasks on the server executor, possibly on different threads. A unary service method runs in onHalfClose, so a context an interceptor propagates around next.startCall(..), for example with context.plus(element).propagate(() -> next.startCall(call, headers)), would otherwise be lost before the method runs.

The interceptor runs last, closest to the service, so it captures the context of interceptors registered as beans. Interceptors applied to a single service with ServerInterceptors.intercept(..) run inside it and are not captured. The snapshot is only a fallback: an event that already runs inside a propagated context, for example one an outer interceptor set around that event, keeps it. The interceptor does nothing when no context is propagated. Enabled by default; disable with grpc.server.context-propagation.enabled=false.

Since:
5.3.0
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    The property that enables server context propagation.

    Fields inherited from interface io.micronaut.core.order.Ordered

    HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    Runs last, so that the context set by every other interceptor is captured.
    <ReqT,RespT>
    io.grpc.ServerCall.Listener<ReqT>
    interceptCall(io.grpc.ServerCall<ReqT,RespT> call, io.grpc.Metadata headers, io.grpc.ServerCallHandler<ReqT,RespT> next)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • GrpcServerContextPropagationInterceptor

      public GrpcServerContextPropagationInterceptor()
  • Method Details

    • interceptCall

      public <ReqT,RespT> io.grpc.ServerCall.Listener<ReqT> interceptCall(io.grpc.ServerCall<ReqT,RespT> call, io.grpc.Metadata headers, io.grpc.ServerCallHandler<ReqT,RespT> next)
      Specified by:
      interceptCall in interface io.grpc.ServerInterceptor
    • getOrder

      public int getOrder()
      Runs last, so that the context set by every other interceptor is captured.
      Specified by:
      getOrder in interface io.micronaut.core.order.Ordered
      Returns:
      The order