proxywhirl.circuit_breaker.sync¶
Circuit breaker pattern implementation for proxy failure management.
IMPORTANT: This module provides SYNCHRONOUS circuit breaker implementation.
Usage Guidelines:¶
CircuitBreaker: Use in synchronous contexts only (threading.Lock)
AsyncCircuitBreaker: Use in async contexts (from circuit_breaker_async module)
Example
- Synchronous usage:
>>> cb = CircuitBreaker(proxy_id="proxy-1") >>> cb.record_failure() # Thread-safe >>> if cb.should_attempt_request(): ... # make request ... cb.record_success()
- Async usage (use AsyncCircuitBreaker instead):
>>> from proxywhirl.circuit_breaker_async import AsyncCircuitBreaker >>> cb = AsyncCircuitBreaker(proxy_id="proxy-1") >>> await cb.record_failure() # Event loop safe >>> if await cb.should_attempt_request(): ... # make async request ... await cb.record_success()
Classes¶
Circuit breaker for a single proxy (SYNCHRONOUS implementation). |
Module Contents¶
- class proxywhirl.circuit_breaker.sync.CircuitBreaker(/, **data)[source]¶
Bases:
proxywhirl.circuit_breaker.base.CircuitBreakerBaseCircuit breaker for a single proxy (SYNCHRONOUS implementation).
WARNING: This class uses threading.Lock and is designed for SYNCHRONOUS contexts only. For async applications, use AsyncCircuitBreaker from circuit_breaker_async module.
- Parameters:
data (Any)
Create a new model by parsing and validating input data from keyword arguments.
Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.
self is explicitly positional-only to allow self as a field name.
- classmethod create(proxy_id, config=None, **kwargs)[source]¶
Factory method to create a circuit breaker with optional config.
- Parameters:
proxy_id (str) – Unique identifier for the proxy
config (proxywhirl.models.CircuitBreakerConfig | None) – CircuitBreakerConfig with settings
**kwargs (Any) – Additional CircuitBreaker field overrides
- Returns:
CircuitBreaker instance
- Return type:
- classmethod from_config(proxy_id, config=None, **kwargs)[source]¶
Backward-compatible alias for create().
- Parameters:
proxy_id (str)
config (proxywhirl.models.CircuitBreakerConfig | None)
kwargs (Any)
- Return type:
- record_failure()[source]¶
Record a failure and update state if threshold reached.
Thread-safe - acquires threading.Lock for the entire operation.
- Return type:
None
- record_success()[source]¶
Record a success and potentially close circuit.
Thread-safe - acquires threading.Lock for the entire operation.
- Return type:
None