Configuring the storage backend¶
Purgatory can share the state of its circuits between many instances using a redis server.
With this strategy, when a service has been restarted, it starts with the circuit breaker state.
To choose a storage backend, the unit of work has to be configured.
Using asynchronous API¶
Important
Extra dependency redis
is required. can be installed using
pip install "purgatory[redis]"
import httpx
from purgatory import AsyncCircuitBreakerFactory, AsyncRedisUnitOfWork
circuit_breaker = AsyncCircuitBreakerFactory(
default_threshold=5,
default_ttl=30,
exclude=[
(httpx.HTTPStatusError, lambda exc: exc.response.is_client_error),
],
uow=AsyncRedisUnitOfWork("redis://localhost/0"),
)
await circuit_breaker.initialize()
Using synchronous API¶
Important
Extra dependency redis
is required. can be installed using
pip install "purgatory[redis]"
import requests
from purgatory import SyncCircuitBreakerFactory, SyncRedisUnitOfWork
circuit_breaker = SyncCircuitBreakerFactory(
default_threshold=5,
default_ttl=30,
exclude=[
(requests.HTTPError, lambda exc: 400 <= exc.response.status_code < 500),
],
uow=SyncRedisUnitOfWork("redis://localhost/0"),
)
Important
When using the AsyncRedisUnitOfWork, the coroutine initialize
must
be called to initialize the redis connection.
The synchronous client has this method because it is autogenerated, but it cannot not be used.