Class GrpcStreamDeadline

java.lang.Object
org.mockserver.grpc.GrpcStreamDeadline

public class GrpcStreamDeadline extends Object
Enforces the client's grpc-timeout for a streaming RPC, terminating it mid-stream with DEADLINE_EXCEEDED when the deadline elapses.

One instance per RPC invocation, created by the handler that begins the stream and threaded through its emission recursion. It is deliberately not stored on a channel attribute: on the HTTP/2 connection-adapter pipeline a single channel is shared by every multiplexed stream, so a channel-scoped guard would be replaced by the next overlapping RPC and the first stream would then consult the wrong one -- the same class of error as the single-slot service/method attribute that this change set already had to fix twice.

The interleaving guarantee. A streaming RPC emits frames asynchronously (per message delays, write-completion callbacks, breakpoint resumes), so the deadline can fire while a write is in flight. tryTerminate() is a compare-and-set, so exactly one of "the deadline fired" and "the stream completed normally" wins, and the loser writes nothing. Emission points additionally check isTerminated() before writing, so no message can be emitted after the terminal trailer. Both the timer and the emission callbacks run on the stream's own event loop, so the CAS resolves an ordering rather than true parallelism -- the CAS is what makes that ordering explicit instead of assumed.

  • Constructor Details

    • GrpcStreamDeadline

      public GrpcStreamDeadline()
  • Method Details

    • schedule

      public GrpcStreamDeadline schedule(io.netty.channel.ChannelHandlerContext ctx, HttpRequest request, Runnable onDeadline)
      Schedules deadline termination for a streaming RPC, if the client sent a grpc-timeout.
      Parameters:
      ctx - the stream's channel context
      request - the inbound request, read for grpc-timeout
      onDeadline - invoked on the event loop when the deadline elapses and this guard wins the CAS; it should write the DEADLINE_EXCEEDED trailer and stop the stream
      Returns:
      this guard, for chaining
    • tryTerminate

      public boolean tryTerminate()
      Claims the stream for termination. Returns true only for the first caller, so a trailer is never written twice.
    • isTerminated

      public boolean isTerminated()
      Whether this stream has already been terminated (by the deadline or by normal completion). Emission points check this so no message follows the terminal trailer.
    • cancel

      public void cancel()
      Cancels the deadline timer. Called on normal completion and when the channel goes inactive, so a timer cannot outlive its stream.
    • deadlineExceededMessage

      public String deadlineExceededMessage()
      The grpc-message to report when this deadline elapses.