Total Retry Latency with Capped Backoff
Problem Statement
An agent retries a failing call with exponential backoff. Compute the total wall-clock delay spent sleeping between attempts, given a cap on each individual backoff.
Background
With base seconds, factor 2, and attempts total tries, the sleeps happen between attempts: after attempt i (1-indexed) the agent sleeps min(base * 2(i-1), cap)** seconds, for i = 1 .. attempts-1. The final attempt is not followed by a sleep. Return the summed sleep time.
Your Task
Implement:
def total_backoff(base, cap, attempts):
Return the total seconds slept (float) across all inter-attempt waits.
Input Format
- base (float), cap (float), attempts (int).
Output Format
- A float (total sleep seconds).
Sample
print(total_backoff(1.0, 8.0, 4))
Output:
7.0
Example
print(total_backoff(1.0, 8.0, 4))
7.0
- With
attempts = 4, the agent sleeps between attempts 1→2, 2→3, and 3→4, resulting in 3 total sleep intervals. - For the first interval (after attempt 1), the backoff is calculated as second.
- For the second interval (after attempt 2), the backoff doubles to seconds.
- For the third interval (after attempt 3), the backoff doubles again to seconds.
- Summing these individual sleep durations gives the total latency: .
- The final output is 7.0
Constraints
attempts >= 1; with 1 attempt there are no sleeps (return 0.0).- Sleep after attempt
iismin(base * 2**(i-1), cap)fori in 1..attempts-1. - Return a float.