Skip to main content

Async retry with exponential backoff, circuit breaker, and cancellation for Python

Project description

philiprehberger-retry-kit

Retry with exponential backoff, circuit breaker, and presets for Python.

Installation

pip install philiprehberger-retry-kit

Usage

Basic Retry

from philiprehberger_retry_kit import retry

result = retry(lambda: fetch_data(), max_attempts=3)

With Options

result = retry(
    lambda: fetch_data(),
    max_attempts=5,
    backoff="exponential",      # "exponential" | "linear" | "fixed"
    initial_delay=1.0,
    max_delay=30.0,
    jitter=True,
    retry_on=lambda e: isinstance(e, ConnectionError),
    on_retry=lambda e, attempt: print(f"Retry {attempt}..."),
)

Async Retry

from philiprehberger_retry_kit import async_retry

result = await async_retry(
    lambda: async_fetch_data(),
    max_attempts=3,
    backoff="exponential",
)

Presets

from philiprehberger_retry_kit import retry, presets

result = retry(lambda: fetch_data(), **presets["network_request"])
result = retry(lambda: db_query(), **presets["database_query"])
result = retry(lambda: critical_op(), **presets["aggressive"])
result = retry(lambda: gentle_op(), **presets["gentle"])

Circuit Breaker

from philiprehberger_retry_kit import CircuitBreaker, CircuitOpenError

breaker = CircuitBreaker(
    failure_threshold=5,
    reset_timeout=30.0,
    on_state_change=lambda from_s, to_s: print(f"Circuit: {from_s} -> {to_s}"),
)

try:
    result = breaker.call(lambda: fetch_data())
except CircuitOpenError:
    print("Circuit is open, failing fast")

Async Circuit Breaker

result = await breaker.async_call(lambda: async_fetch_data())

API Reference

Function / Class Description
retry(fn, *, max_attempts=3, backoff="exponential", initial_delay=1.0, max_delay=30.0, jitter=True, retry_on=None, on_retry=None, on_success=None, on_failure=None) Retry a callable with configurable backoff
async_retry(fn, *, ...) Async version of retry() with the same parameters
CircuitBreaker(failure_threshold=5, reset_timeout=30.0, half_open_max_attempts=1, on_state_change=None, on_circuit_open=None) Circuit breaker that fails fast after repeated failures
CircuitBreaker.call(fn) Execute function through the circuit breaker
CircuitBreaker.async_call(fn) Async version of call()
RetryError Raised when all retry attempts fail (.attempts, .last_error)
CircuitOpenError Raised when circuit breaker is open
presets Dict of preset configs: "aggressive", "gentle", "network_request", "database_query"

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_retry_kit-0.2.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

philiprehberger_retry_kit-0.2.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_retry_kit-0.2.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_retry_kit-0.2.0.tar.gz
Algorithm Hash digest
SHA256 ac070c980c0b96a6a53d245c4f5e34402332e0a3a66d66c2088cd4eb08dc1372
MD5 d1af58cb4f01e28545ce4e4cf9d779d9
BLAKE2b-256 202fa95b51ebe483d70642aa2cb1260497a88798608e34d63af3a74a5e24c65d

See more details on using hashes here.

File details

Details for the file philiprehberger_retry_kit-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_retry_kit-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a886a9a19a3edaabe41d5644bdc7e179725ed7c684f5592a3965bf8980d1e101
MD5 e60925620fcc9455d4f75fc39dd4cadd
BLAKE2b-256 09a6b95ec712804b3c26a222f6715f18797ae9a3286fbe9fbad99f3a2ef48a79

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page