Retry with Capped Exponential Backoff and Jitter Cap
Problem Statement
Generate the backoff delay schedule for a retrying pipeline step: exponential growth, capped at a maximum, for a given number of retries.
Background
For retry attempt i (0-indexed), the base delay is base * (2 ** i), capped at cap. Return the list of delays for attempts 0 .. retries-1. This is the deterministic (pre-jitter) schedule used to reason about worst-case total wait.
Your Task
def backoff_schedule(base, cap, retries):
Return the list of capped delays.
Input Format
- base (number), cap (number), retries (int).
Output Format
- A list of numbers.
Sample
print(backoff_schedule(1, 10, 5))
Output:
[1, 2, 4, 8, 10]
Example
print(backoff_schedule(1, 10, 5))
[1, 2, 4, 8, 10]
- Initialize the parameters from the input: base delay , maximum cap , and total retries .
- For the first two attempts (), calculate the exponential delay and compare it to the cap. Since and are both less than , the delays remain and .
- For the next two attempts (), the exponential growth continues: and . Both values are still below the cap of , so the delays are and .
- For the final attempt (), the calculated exponential delay is . Because this exceeds the cap, the delay is constrained to the maximum value of .
- The final output is [1, 2, 4, 8, 10]
Constraints
- delay[i] = min(base * 2**i, cap).
- Return
retriesentries (empty if retries == 0).