← All Exercises

Propagate an RPC Deadline Across Hops

intermediatejavascript~25min

Background

Deadlines that are not propagated let upstream services work past a caller timeout. Propagating remaining budget cancels wasted work early.

Problem

Subtract per-hop overhead from a request deadline and report the remaining budget at each hop, marking when the deadline is exceeded.

Input

JSON object: { deadlineMs, hops: [{ name, overheadMs }] }

Output

JSON object: { perHop: [{ name, remainingMs }], exceeded }

Sample tests

Sample 1

Input: {"deadlineMs":500,"hops":[{"name":"gateway","overheadMs":20},{"name":"auth","overheadMs":80},{"name":"db","overheadMs":50}]}
Expected: {"perHop":[{"name":"gateway","remainingMs":480},{"name":"auth","remainingMs":400},{"name":"db","remainingMs":350}],"exceeded":[]}

Sample 2

Input: {"deadlineMs":100,"hops":[{"name":"gateway","overheadMs":60},{"name":"db","overheadMs":60}]}
Expected: {"perHop":[{"name":"gateway","remainingMs":40},{"name":"db","remainingMs":-20}],"exceeded":["db"]}

Your solution

Learning objective

Propagate RPC deadlines so downstream work stops when callers give up.