Skip to main content

A circuit breaker and retry library for handling failures in Python applications

Project description

highway_circutbreaker

Library for handling failures.

Installation

pip install highway_circutbreaker

Usage

Policies

Currently, we support two policies: Retry and CircuitBreaker. Detailed documentation can be found below.

Retry

class RetryPolicy

Retry policy allows you to configure how result of execution of a given callable must be retried.

Parameters:

  • backoff: Optional[Backoff] = None - backoff configuration;
  • max_retries: int = 3 - maximum number of times supplied function will be called;
  • handle: Callable[[Exception], bool] - predicate defining whether an exception can be handled (deemed as failure to be retried). By default, all exceptions are handled.

Raises:

  • RetriesExceeded - when callable cannot be retried due to exceeding number of attempts. Preserves original exception context.

Can be used as a decorator:

from highway_circutbreaker import RetryPolicy

@RetryPolicy(max_retries=2)
def my_function() -> None:
    print("my function")
class Backoff

Defines configuration of backoff to compute delays between calls.

Formula: delay = min_delay * pow(factor, attempt-1)

Parameters:

  • min_delay: timedelta - initial (thus minimal) delay;
  • max_delay: timedelta - computed delay will not exceed this value;
  • factor: int = 2 - factor;
  • jitter: Optional[float] - jitter coefficient. Must be in range [0, 1]. A random portion of delay * jitter will be added to or subtracted from the total delay.

Methods:

  • for_attempt(attempt: int) -> float - compute delay in seconds for a given attempt.
class Delay

Subclass of Backoff that defines constant delay between subsequent calls.

Parameters:

  • delay: timedelta - delay between calls

Circuit Breaker

For more details on ideas behind Circuit Breaker please read this article.

Class CircuitBreakerPolicy

Circuit Breaker allows you to configure how execution of a given callable must be interrupted.

Parameters:

  • cooldown: timedelta = 0 - duration after which breaker transitions OPEN -> HALF_OPEN
  • failure_threshold: Fraction = 1 of 1 - amount of failures over total number of executions before breaker trips CLOSED -> OPEN
  • success_threshold: Fraction = 1 of 1 - amount of successes over total number of executions before breaker closes HALF_OPEN -> CLOSED. If not set failure_threshold is used instead.
  • handle: Callable[[Exception], bool] - predicate defining whether an exception can be handled (deemed as failure to be counted towards failures). By default, all exceptions are handled.
  • on_state_change: Callable[[CircuitBreakerPolicy, State, State], None] - method which will be called every time state of CircuitBreaker is changed.

Raises:

  • CircuitBreakerOpenError - when CircuitBreaker is in OPEN state.

Can be used as a decorator:

from datetime import timedelta
from highway_circutbreaker import CircuitBreakerPolicy

@CircuitBreakerPolicy(cooldown=timedelta(seconds=2))
def my_function() -> None:
    print("my function")

Methods and Properties:

  • (method) on_state_changed(self, current: CircuitBreakerState, new: CircuitBreakerState) -> None - called whenever CircuitBreaker changes its state
  • (property) history -> CircularBuffer - history of recent calls capped at threshold's denominator. When state is OPEN provides history of the previous state
  • (property) state -> CircuitBreakerState - current state of the CircuitBreaker

Enum CircuitBreakerState

  • CLOSED
  • OPEN
  • HALF_OPEN

Failsafe

Provided interface to apply multiple policies at once. It will handle execution results in reverse, with last policy applied first.

Example:

from highway_circutbreaker import Failsafe, RetryPolicy, CircuitBreakerPolicy

@Failsafe(policies=(RetryPolicy(), CircuitBreakerPolicy()))
def my_function() -> None:
    print("my function")

# Equivalent to:

@RetryPolicy()
@CircuitBreakerPolicy()
def my_function() -> None:
    print("my function")

Library development

  1. install dependencies
poetry install
  1. run all checks
poetry run make verify
  1. enter virtualenv managed by poetry
poetry shell

Maintained by the Python Platform team - Slack #p-python-platform

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

highway_circutbreaker-0.1.0.tar.gz (19.5 kB view details)

Uploaded Source

Built Distribution

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

highway_circutbreaker-0.1.0-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file highway_circutbreaker-0.1.0.tar.gz.

File metadata

  • Download URL: highway_circutbreaker-0.1.0.tar.gz
  • Upload date:
  • Size: 19.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for highway_circutbreaker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 06a8dd1a3580dfd7679ad81927bbabe75ddc19f3ef6a927637c7686ccc2fd029
MD5 32364c4e5c4232c0c103e2664b908480
BLAKE2b-256 645e63435e47c5faca12756fe5404b541eebf89795973e57a9a7176ff29467cf

See more details on using hashes here.

File details

Details for the file highway_circutbreaker-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for highway_circutbreaker-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a0102c20790c0dd517bf21b79b68539a42accd6bff121d0764f18e32611087f
MD5 a0d00aca5479d95bb6cf199aa0d57e28
BLAKE2b-256 340d4a0bc39e33a360c714a1a235d3de7efaaa872ea41061adeb8cec2153662c

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