AlgoMaster Logo
AlgoMasterPropagate a gRPC Deadlinemedium

Propagate a gRPC Deadline

medium

An end-to-end gRPC deadline is an absolute point in time, not a fresh timeout for every service. Passing a new generous timeout at each hop can let downstream work continue after the original caller has already given up.

Design a GrpcDeadlinePropagator class:

  • GrpcDeadlinePropagator() creates a stateless planner.
  • int[] propagate(int startTime, int clientTimeout, int[] processingTimes, int[] localTimeouts) returns the absolute deadline forwarded at every downstream hop.

The original deadline is startTime + clientTimeout. Let now initially equal startTime. For hop i:

  1. Add processingTimes[i] to now; this is time already spent before making the downstream call.
  2. If now >= inheritedDeadline, append -1 for this hop and every remaining hop.
  3. Otherwise, the child deadline is min(inheritedDeadline, now + localTimeouts[i]).
  4. Append that absolute deadline and use it as the inherited deadline for the next hop.

A local policy may shorten a deadline but may never extend its parent. Do not mutate either input array, and keep repeated calls independent.

Example 1:

Input:

Output:

Explanation: Time already spent affects each local bound, and no child deadline exceeds its parent.

Example 2:

Input:

Output:

Explanation: The second hop reaches the inherited deadline exactly, so it and every deeper call are cancelled.

Constraints

  • 0 <= startTime, clientTimeout <= 10^9
  • 0 <= processingTimes.length == localTimeouts.length <= 10^4
  • 0 <= processingTimes[i] <= 10^9
  • 1 <= localTimeouts[i] <= 10^9
  • All intermediate sums fit a signed 32-bit integer.
  • At most 100 calls are made to propagate.
Hints

Loading...
CallReturns
new GrpcDeadlinePropagator()null
propagate(0, 100, [10,20,5], [80,100,10])[90,90,45]

The first hop shortens the deadline to 90, the second inherits 90, and the third imposes a tighter deadline of 45.

Run checks these cases. Submit also runs a larger hidden set.