Calculating Remaining Time
What you'll learn: How to compute and pass the correct remaining timeout value when forwarding requests to downstream services.
The Time Budget Problem
When Service A receives a request with a 5-second timeout and then calls Service B, it can't simply forward the original 5 seconds. Why? Because time has already elapsed. If Service A spent 200ms processing before making the downstream call, Service B should only get ~4.8 seconds, not the full 5.
If you don't subtract elapsed time, downstream services think they have more time than actually remains. This creates phantom time budgets—services work on requests that have already timed out upstream, wasting resources.
The Calculation
The formula is straightforward:
remaining_timeout = original_timeout - elapsed_time
Before each downstream call:
- Record when the request arrived (
start_time) - Check the current time (
now) - Calculate
elapsed = now - start_time - Compute
remaining = timeout - elapsed - Pass
remainingto the downstream service (via timeout headers)
The Reality Check
If remaining_timeout ≤ 0, don't make the downstream call at all—the deadline has already passed. Fail fast and return a timeout error immediately.
This is especially critical in deep call chains. Each hop consumes time. By recalculating at every level, you ensure the entire chain respects the original deadline set by the client.
Key Takeaway: Always subtract elapsed processing time from the original timeout before forwarding requests downstream—this ensures accurate deadline propagation and prevents wasted work on already-expired requests.