GitHub

CircuitBreakerStoreService

API reference for CircuitBreakerStoreService.

The state of each key: make_store_memory keeps it in the process, make_store_redis shares it between processes, and CircuitBreakerStoreService(admit=..., record=..., state=...) makes one of three functions (a test double, say).

Functions

__init__

__init__(*, admit, record, state) -> None
ParameterTypeDescription
admitCallable[[str], Effect[Admission, CircuitBreakerError, Scope]]
recordRecord
stateCallable[[str], Effect[State, CircuitBreakerError]]
def __init__(
    self,
    *,
    admit: Callable[[str], Effect[Admission, CircuitBreakerError, Scope]],
    record: Record,
    state: Callable[[str], Effect[State, CircuitBreakerError]],
) -> None:
    self._admit = admit
    self._record = record
    self._state = state

admit

admit(key) -> Effect[Admission, CircuitBreakerError, Scope]

Lets a call on key through, giving its Admission: the generation to pass back to record, and whether it is the half-open probe. An open circuit whose open time has ended turns half-open and admits this call as the probe. Fails with CircuitBreakerError whose reason is CircuitOpen while the circuit is open, CircuitHalfOpen while another probe runs, or CircuitBreakerStoreError when the store cannot be reached. The Scope is the call's: a store may hold the probe's slot with it (the Redis store renews the probe's lease until it closes).

ParameterTypeDescription
keystr

Returns Effect[Admission, CircuitBreakerError, Scope]

def admit(self, key: str) -> Effect[Admission, CircuitBreakerError, Scope]:
    """Lets a call on ``key`` through, giving its ``Admission``: the
    generation to pass back to ``record``, and whether it is the
    half-open probe. An open circuit whose open time has ended turns
    half-open and admits this call as the probe. Fails with
    ``CircuitBreakerError`` whose reason is ``CircuitOpen`` while the
    circuit is open, ``CircuitHalfOpen`` while another probe runs, or
    ``CircuitBreakerStoreError`` when the store cannot be reached. The
    ``Scope`` is the call's: a store may hold the probe's slot with it
    (the Redis store renews the probe's lease until it closes)."""
    return self._admit(key)

record

record(*, key, generation, outcome, trip_policy, open_for) -> Effect[Transition | None, CircuitBreakerError]

Records the outcome of the call on key admitted with generation; one whose generation is no longer current changes nothing. trip_policy decides whether a failure opens a closed circuit, and open_for is how long it stays open if this opens it. Gives the transition the outcome caused, or None. Fails with CircuitBreakerError (reason CircuitBreakerStoreError) when the store cannot be reached.

ParameterTypeDescription
keystr
generationint
outcomeOutcomeKind
trip_policyTripPolicy
open_forDuration

Returns Effect[Transition | None, CircuitBreakerError]

def record(
    self,
    *,
    key: str,
    generation: int,
    outcome: OutcomeKind,
    trip_policy: TripPolicy,
    open_for: Duration,
) -> Effect[Transition | None, CircuitBreakerError]:
    """Records the ``outcome`` of the call on ``key`` admitted with
    ``generation``; one whose generation is no longer current changes
    nothing. ``trip_policy`` decides whether a failure opens a closed
    circuit, and ``open_for`` is how long it stays open if this opens
    it. Gives the transition the outcome caused, or ``None``. Fails
    with ``CircuitBreakerError`` (reason ``CircuitBreakerStoreError``)
    when the store cannot be reached."""
    return self._record(
        key=key,
        generation=generation,
        outcome=outcome,
        trip_policy=trip_policy,
        open_for=open_for,
    )

state

state(key) -> Effect[State, CircuitBreakerError]

key's current state (Closed for a key never seen or long idle). Fails with CircuitBreakerError (reason CircuitBreakerStoreError) when the store cannot be reached.

ParameterTypeDescription
keystr

Returns Effect[State, CircuitBreakerError]

def state(self, key: str) -> Effect[State, CircuitBreakerError]:
    """``key``'s current state (``Closed`` for a key never seen or long
    idle). Fails with ``CircuitBreakerError`` (reason
    ``CircuitBreakerStoreError``) when the store cannot be reached."""
    return self._state(key)