### Basic Circuit Breaker Usage Source: https://github.com/fabfuel/circuitbreaker/blob/develop/README.rst Decorate a function with @circuit for basic circuit breaker functionality with default settings. This monitors failures, resets on success, opens after 5 failures, and attempts a test execution after 30 seconds. ```python from circuitbreaker import circuit @circuit def external_call(): ... ``` -------------------------------- ### Configuring Failure Threshold and Expected Exception Source: https://github.com/fabfuel/circuitbreaker/blob/develop/README.rst Customize the circuit breaker by setting a custom failure threshold and specifying the exact exception types that should trigger a failure. This helps in protecting against cascading failures by only reacting to integration-specific errors. ```python from circuitbreaker import circuit @circuit(failure_threshold=10, expected_exception=ConnectionError) def external_call(): ... ``` -------------------------------- ### Apply Custom Circuit Breaker as Object Source: https://github.com/fabfuel/circuitbreaker/blob/develop/README.rst Use an instance of a custom circuit breaker class as a decorator for a function. ```python @MyCircuitBreaker() def external_call(): ... ``` -------------------------------- ### Apply Custom Circuit Breaker via Decorator Proxy Source: https://github.com/fabfuel/circuitbreaker/blob/develop/README.rst Apply a custom circuit breaker class to a function using the 'circuit' decorator with the 'cls' parameter. ```python @circuit(cls=MyCircuitBreaker) def external_call(): ... ``` -------------------------------- ### Custom Circuit Breaker Subclass Source: https://github.com/fabfuel/circuitbreaker/blob/develop/README.rst Extend the CircuitBreaker class to define custom failure thresholds, recovery timeouts, and expected exceptions. ```python from circuitbreaker import CircuitBreaker class MyCircuitBreaker(CircuitBreaker): FAILURE_THRESHOLD = 10 RECOVERY_TIMEOUT = 60 EXPECTED_EXCEPTION = RequestException ``` -------------------------------- ### Custom Failure Logic with Callable Source: https://github.com/fabfuel/circuitbreaker/blob/develop/README.rst Use a callable function to define complex logic for identifying failures. This allows for fine-grained control over which exceptions trigger the circuit breaker, such as distinguishing between general request errors and specific HTTP status codes. ```python # Assume we are using the requests library def is_not_http_error(thrown_type, thrown_value): return issubclass(thrown_type, RequestException) and not issubclass(thrown_type, HTTPError) def is_rate_limited(thrown_type, thrown_value): return issubclass(thrown_type, HTTPError) and thrown_value.status_code == 429 @circuit(expected_exception=is_not_http_error) def call_flaky_api(...): rsp = requests.get(...) rsp.raise_for_status() return rsp @circuit(expected_exception=is_rate_limited) def call_slow_server(...): rsp = requests.get(...) rsp.raise_for_status() return rsp ``` -------------------------------- ### Async Function Circuit Breaker Source: https://github.com/fabfuel/circuitbreaker/blob/develop/README.rst Supports asynchronous functions by decorating them with @circuit. The behavior and default settings remain the same as for synchronous functions. ```python @circuit async def external_call(): ... ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.