core.circuit_breaker module
Generic async circuit breaker.
CircuitBreaker trips to OPEN after failure_threshold
consecutive failures, short-circuiting further calls with
CircuitBreakerOpenException until reset_timeout elapses,
then probes recovery via a HALF_OPEN trial call. Used to protect
calls to fragile dependencies (e.g. Redis during ingress routing).
- class core.circuit_breaker.CircuitBreaker(failure_threshold=3, reset_timeout=60.0)[source]
Bases:
object- __init__(failure_threshold=3, reset_timeout=60.0)[source]
Initialize a closed circuit breaker with failure/reset tuning.
Sets up the in-memory state machine used to short-circuit calls to a fragile dependency. The breaker starts in the
CLOSEDstate with a zeroed failure counter and no recorded last-failure time, so the first protected call always executes.This is a pure in-memory initializer: it performs no I/O, touches no Redis/KG/LLM/HTTP collaborators, and only stores configuration and counters on
self. State transitions happen later insidecall(),_record_failure(), and_record_success(). It is constructed bycore.strangler_router(and the top-levelstrangler_routershim) when wiring ingress routing, and directly intests/core/migration/test_circuit_breaker.py.
- async call(func, *args, **kwargs)[source]
Invoke an async callable through the breaker, enforcing the circuit state.
This is the single entry point that protects a fragile dependency. When the breaker is
OPENit checks whetherreset_timeouthas elapsed since the last failure: if so it transitions toHALF_OPEN(loggingcircuit_breaker_half_open) and lets one probe call through; otherwise it short-circuits immediately by raisingCircuitBreakerOpenExceptionwithout ever awaiting func.When a call is allowed, it awaits
func(*args, **kwargs). Any exception is routed through_record_failure()(which may trip or re-open the breaker) and then re-raised unchanged; a successful return is routed through_record_success()(which may close the breaker and clear the failure count) before the result is returned. The method reads the wall clock viatime.time()and emits structured log lines on thestargazer.circuit_breakerlogger; it performs no Redis, KG, LLM, or HTTP I/O of its own beyond whatever func does.It is called by
core.strangler_router/ the top-levelstrangler_routershim to guard ingress route resolution, and by the circuit-breaker unit tests intests/core/migration/test_circuit_breaker.py.- Parameters:
- Returns:
Whatever func returns when the call is permitted and succeeds.
- Return type:
- Raises:
CircuitBreakerOpenException – If the breaker is
OPENand the reset timeout has not yet elapsed, so func is never invoked.Exception – Re-raises, unchanged, any exception thrown by func after recording the failure.