Fix the backoff bounds in a retry helper
Your task
Your service retries a flaky downstream call with exponential backoff and full jitter: each sleep is a random time from 0 up to a bound, and the bound grows after every attempt. A reviewer asks you to defend the schedule, so you want a function that returns just the bounds.
An assistant wrote backoff_delay_bounds(base_ms, factor, max_delay_ms, attempts, context_cancelled). It is in the editor. It has a bug. Find it and fix it.
Input and output:
base_ms: the first bound, in milliseconds.factor: what each bound is multiplied by to get the next.max_delay_ms: the cap. No bound is ever larger.attempts: how many calls the loop makes. There is one sleep between each pair of calls.context_cancelled:truewhen the request was cancelled before the loop started.- Return the list of bounds, in milliseconds.
Rules the tests check:
- If
context_cancelledis true, return an empty list. Check this first. - If
attemptsis 1 or less, return an empty list. - A negative
base_mscounts as 0, afactorbelow 1 counts as 1, and a negativemax_delay_mscounts as 0. - Return
attempts - 1bounds. The first isbase_msand each next one is the previous bound timesfactor. Every bound, the first included, is capped atmax_delay_ms.
Examples
backoff_delay_bounds(base_ms: int, factor: int, max_delay_ms: int, attempts: int, context_cancelled: bool) -> list[int]Example 1
Input
backoff_delay_bounds(100, 2, 2000, 5, False)Output
[100, 200, 400, 800]Example 2
Input
backoff_delay_bounds(100, 2, 2000, 5, True)Output
[]Example 3
Input
backoff_delay_bounds(100, 2, 300, 4, False)Output
[100, 200, 300]