Practice ยท 1 of 2
Backoff schedule, computed not slept
Implement RetryPolicy(max_attempts=5, base=0.5, cap=8.0) with two methods:
- schedule(attempt, rnd=None) โ the delay before retry number attempt (0-based): the exponential ceiling min(cap, base * 2**attempt) multiplied by a jitter factor from rnd (a zero-arg callable returning floats in [0, 1); default random.random) โ i.e. full jitter: rnd() * ceiling
- run(fn, rnd=None, clock=None) โ call fn(attempt) up to max_attempts times; stop at the first truthy result and return (result, attempts_used); if all attempts fail return (None, max_attempts)
Deterministic requirement: run computes the schedule but must not actually sleep โ tests pass their own rnd.
Difficulty: advanced
Back to lesson: Practice: Retry & Redelivery Drills