nat.middleware.circuit_breaker#
Circuit breaker middleware package.
Submodules#
Exceptions#
Raised when a call is short-circuited because the circuit breaker is |
Classes#
Middleware implementing the Circuit Breaker pattern with per-target isolation. |
|
Possible states for the circuit breaker. |
|
Configuration for circuit breaker middleware. |
Package Contents#
- class CircuitBreakerMiddleware(
- config: nat.middleware.circuit_breaker.circuit_breaker_middleware_config.CircuitBreakerMiddlewareConfig,
- builder: nat.builder.builder.Builder,
Bases:
nat.middleware.dynamic.dynamic_function_middleware.DynamicFunctionMiddlewareMiddleware implementing the Circuit Breaker pattern with per-target isolation.
Monitors execution failures and short-circuits calls when downstream functions fail repeatedly, transitioning through
CLOSED,OPEN, andHALF_OPENstates per target.Initialize CircuitBreakerMiddleware.
- Args:
config: Circuit breaker configuration parameters. builder: Workflow builder instance.
- _cb_config: nat.middleware.circuit_breaker.circuit_breaker_middleware_config.CircuitBreakerMiddlewareConfig#
- _lock: asyncio.Lock#
- _get_tool_state(target: str) _ToolState#
Get or initialize state record for a given target name under lock.
- Args:
target: Identifier for the target function or tool.
- Returns:
_ToolState: State record for the target.
- async get_state(target: str) CircuitBreakerState#
Return the current circuit breaker state for a specific target.
- Args:
target: Identifier for the target function or tool.
- Returns:
CircuitBreakerState: Current state of the target.
- async get_failure_count(target: str) int#
Return the consecutive failure count for a specific target.
- Args:
target: Identifier for the target function or tool.
- Returns:
int: Number of consecutive failures.
- async get_success_count(target: str) int#
Return consecutive successes in
HALF_OPENstate towards recovery for a target.- Args:
target: Identifier for the target function or tool.
- Returns:
int: Number of consecutive probe successes.
- async get_last_state_change(target: str) float#
Return the monotonic timestamp of the last state change for a specific target.
- Args:
target: Identifier for the target function or tool.
- Returns:
float: Timestamp in seconds.
- async is_half_open_probing(target: str) bool#
Return whether a probe execution is actively in-flight for a target in
HALF_OPENstate.- Args:
target: Identifier for the target function or tool.
- Returns:
bool: True if a probe call is in-flight in
HALF_OPENstate.
- _get_short_circuit_message(target: str) str#
Format the short-circuit message when
OPENor busy probing.- Args:
target: Identifier for the target function or tool.
- Returns:
str: Explanatory error message.
- async _before_invocation(target: str) bool#
Evaluate circuit breaker state and determine whether invocation can proceed.
- Args:
target: Identifier for the target function or tool.
- Returns:
bool: True if execution is admitted as a
HALF_OPENprobe, False if normalCLOSED.- Raises:
CircuitBreakerOpenError: If the circuit breaker is
OPENor busy probing inHALF_OPEN.
- async _after_invocation_success( ) None#
Update circuit breaker state upon a successful execution.
- Args:
target: Identifier for the target function or tool. is_probe: Whether this execution was an admitted probe call in
HALF_OPENstate.
- async _after_invocation_failure( ) None#
Update circuit breaker state upon a failed execution.
- Args:
target: Identifier for the target function or tool. is_probe: Whether this execution was an admitted probe call in
HALF_OPENstate.
- async _after_invocation_cancellation(target: str) None#
Reset probing flag on cancellation without counting as failure.
- Args:
target: Identifier for the target function or tool.
- async function_middleware_invoke(
- *args: Any,
- call_next: nat.middleware.middleware.CallNext,
- context: nat.middleware.middleware.FunctionMiddlewareContext,
- \*\*kwargs: Any,
Wrap downstream invocation with circuit breaker monitoring and short-circuiting.
- Args:
args: Positional arguments for the intercepted function. call_next: Callable to invoke next middleware or target function. context: Static function metadata describing the tool being invoked. kwargs: Keyword arguments for the intercepted function.
- Returns:
Any: The tool result.
- Raises:
CircuitBreakerOpenError: If the circuit breaker is
OPENor busy probing. Exception: Any exception raised by the downstream callable.
- async function_middleware_stream(
- *args: Any,
- call_next: nat.middleware.middleware.CallNextStream,
- context: nat.middleware.middleware.FunctionMiddlewareContext,
- \*\*kwargs: Any,
Wrap downstream streaming call with circuit breaker monitoring and short-circuiting.
- Args:
args: Positional arguments for the intercepted function. call_next: Callable to invoke next middleware or target stream. context: Static function metadata describing the tool being invoked. kwargs: Keyword arguments for the intercepted function.
- Yields:
Any: Stream chunks from downstream execution.
- Raises:
CircuitBreakerOpenError: If the circuit breaker is
OPENor busy probing. Exception: Any exception raised by the downstream stream.
- exception CircuitBreakerOpenError#
Bases:
RuntimeErrorRaised when a call is short-circuited because the circuit breaker is
OPEN.Initialize self. See help(type(self)) for accurate signature.
- class CircuitBreakerState#
Bases:
enum.StrEnumPossible states for the circuit breaker.
Initialize self. See help(type(self)) for accurate signature.
- CLOSED = 'CLOSED'#
- OPEN = 'OPEN'#
- HALF_OPEN = 'HALF_OPEN'#
- class CircuitBreakerMiddlewareConfig#
Bases:
nat.middleware.dynamic.dynamic_middleware_config.DynamicMiddlewareConfigConfiguration for circuit breaker middleware.
- Attributes:
failure_threshold: Number of consecutive failures required to trip the circuit breaker. cooldown_period: Time in seconds to wait in
OPENstate before probing. half_open_success_threshold: Consecutive successful probes required to recover toCLOSED. probe_timeout: Optional timeout in seconds for probe calls inHALF_OPENstate. circuit_breaker_message: Optional custom message returned when short-circuited.