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.
backoffFixed(attempt: int, baseMs: int) → int
Go needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.
Where you start
func backoffFixed(attempt int, baseMs int) int {
}
Worked examples
| Call | Result |
|---|---|
backoffFixed(0, 100) | 100 |
backoffFixed(1, 100) | 200 |
backoffFixed(2, 100) | 400 |
backoffFixed(3, 100) | 800 |
Hint
Start at baseMs and double in a loop; clamp on each step.
Reference solution in Go
func backoffFixed(attempt int, baseMs int) int {
if attempt <= 0 {
if baseMs > 60000 {
return 60000
}
return baseMs
}
value := baseMs
for i := 0; i < attempt; i++ {
value = value * 2
if value > 60000 {
value = 60000
}
}
return value
}