Tenacity Circuit Breaker Fallback
Guard a flaky dependency with a circuit breaker state machine, falling back to cached defaults and opening after repeated failures.
Code
import random
import time
from tenacity import RetryCallState, retry, stop_after_attempt, wait_fixed
class CircuitBreaker:
def __init__(self, failure_threshold: int = 3, cooldown: float = 5.0):
self.failure_threshold = failure_threshold
self.cooldown = cooldown
self.failures = 0
self.opened_at: float | None = None
def allow(self) -> bool:
if self.opened_at is None:
return True
if time.monotonic() - self.opened_at >= self.cooldown:
self.opened_at = None # 半开:放行试探请求
self.failures = 0
return True
return False
def record(self, success: bool) -> None:
if success:
self.failures = 0
self.opened_at = None
else:
self.failures += 1
if self.failures >= self.failure_threshold:
self.opened_at = time.monotonic()
breaker = CircuitBreaker(failure_threshold=3, cooldown=5)
FALLBACK_RATES = {'usd': 1.0, 'eur': 0.92}
def after_attempts(retry_state: RetryCallState) -> None:
breaker.record(retry_state.outcome.failed)
@retry(stop=stop_after_attempt(2), wait=wait_fixed(0.1),
after=after_attempts, reraise=True)
def call_rate_api() -> dict:
if random.random() < 0.6:
raise ConnectionError('rates service flaky')
return {'usd': 1.0, 'eur': 0.91}
def get_rates() -> tuple[dict, str]:
if not breaker.allow():
return FALLBACK_RATES, 'fallback-open'
try:
return call_rate_api(), 'live'
except ConnectionError:
return FALLBACK_RATES, 'fallback-error'
for _ in range(8):
rates, source = get_rates()
print('source:', source, 'usd:', rates['usd'])How it works
One way to read it: the class CircuitBreaker and the functions __init__, allow, record and after_attempts, doing all the work.
Iteration happens inside for _ in range(8) — everything outside it runs once. Reach for tenacity and what you get is RetryCallState, retry, stop_after_attempt, wait_fixed — that is the whole API contact area. Invalid states are rejected up front: it throws ConnectionError instead of continuing with bad data.
What comes back is a new object, not a reference to anything passed in.
What to watch for
- Plan for an install step:
tenacityis a real dependency, resolved by the package manager. - The guard
random.random() < 0.6short-circuits the flow with an earlyraisewhen its condition holds.
Putting it to use
Entry signature: def allow(self) -> bool
The demo output is labeled (source:), so every printed line maps back to its originating operation.
allow is the public entry point; everything else in the file exists to support it.
Core lines
def after_attempts(retry_state: RetryCallState) -> None:
breaker.record(retry_state.outcome.failed)
def record(self, success: bool) -> None:
if success:
self.failures = 0
self.opened_at = None
else:
self.failures += 1
if self.failures >= self.failure_threshold:
self.opened_at = time.monotonic()
return {'usd': 1.0, 'eur': 0.91}
Interactive Tools for This Topic
FAQ
- Does `allow` modify its input?
- It does not. Inputs are only read; the return value is freshly built.
- Is there a failure path for bad input?
- The code throws `ConnectionError` — the guard `random.random() < 0.6` rejects bad states before the main logic runs. Handle it at the call site or let it propagate deliberately.
- Are there third-party dependencies?
- `tenacity` — install it with the package manager before running.
Report a problem with this exampleOpens a pre-filled issue on GitHub.