Exponential backoff with a ceiling
Retries should space out exponentially — double the delay each attempt — but never exceed a sixty-second cap.
- The delay is baseMs multiplied by 2 raised to the power of the attempt number.
- The result is capped at 60000 milliseconds.
- An attempt number of zero or less returns baseMs (still capped).
- Compute 2^attempt by repeated doubling, not a library power function.
backoff_fixed(attempt: int, base_ms: int) → int
Where you start
def backoff_fixed(attempt: int, base_ms: int) -> int:
Worked examples
| Call | Result |
|---|---|
backoff_fixed(0, 100) | 100 |
backoff_fixed(1, 100) | 200 |
backoff_fixed(2, 100) | 400 |
backoff_fixed(3, 100) | 800 |
Hint
Start at baseMs and double in a loop; clamp on each step.
Reference solution in Python
def backoff_fixed(attempt: int, base_ms: int) -> int:
if attempt <= 0:
return min(base_ms, 60000)
value = base_ms
for _ in range(attempt):
value = value * 2
if value > 60000:
value = 60000
return value