$ show channable/opnieuw
Opnieuw is a Python library for retrying function calls with exponential backoff and jitter. It provides a clean, opinionated decorator API that requires only two parameters: maximum total calls and a retry window. The library handles both synchronous and asynchronous functions, supports exception chaining, and offers context managers to override retry behavior (e.g., disable retries or skip waits during testing). The core design uses a `BackoffCalculator` class that computes backoff intervals using a geometric series, and a `Clock` abstraction to allow time manipulation in tests. The library is small, fully typed, and demonstrates strong software craftsmanship with proper use of context variables for thread/async safety.
The LLM identified 3 files worth highlighting for reuse:
Defines an abstract `Clock` base class with `MonotonicClock` (production) and `DummyClock` (test) implementations. The `DummyClock` allows precise time control in tests, making backoff logic testable without real sleeps. This is a clean, reusable pattern for injecting time into any algorithm.
Contains the `BackoffCalculator` class that computes deterministic backoff intervals using a geometric series based on `max_calls_total` and `retry_window`. It also tracks remaining time against a deadline and returns `None` when retries are exhausted. The class is independent of the decorator logic and can be reused in other retry implementations. The `calculate_exponential_multiplier` function is a pure mathematical helper.
Provides `retry_immediately` context manager that replaces the `BackoffCalculator` with a version that returns zero backoff, effectively disabling waits during tests. Uses `replace_backoff_calculator` from `retries.py` and `ContextVar` for thread/task safety. This is a reusable pattern for injecting custom behavior into decorated functions during testing.