Cyclium.CircuitBreaker (Cyclium v0.1.10)

Copy Markdown View Source

ETS-backed per-expectation circuit breaker.

States: :closed:open:half_open:closed.

When consecutive failures exceed the threshold, the circuit opens and rejects new episodes. After half_open_after_ms, the circuit enters :half_open — allowing one probe episode. If it succeeds, the circuit closes; if it fails, the circuit reopens.

Configuration

Set circuit_breaker on an expectation:

expect :check_health,
  circuit_breaker: %{threshold: 5, half_open_after_ms: 60_000}

ETS layout

Key: {actor_id, expectation_id} Value: %{state: atom, consecutive_failures: int, opened_at: DateTime | nil}

Summary

Functions

Check if an episode is allowed to fire.

Idempotent ETS table creation.

Get the current circuit state for an expectation.

Record a failed episode. Increments failure count and may trip the circuit.

Record a successful episode. Resets the circuit to :closed.

Functions

allow?(actor_id, expectation_id, config)

Check if an episode is allowed to fire.

Returns :ok or {:error, :circuit_open}.

ensure_table()

Idempotent ETS table creation.

get_state(actor_id, expectation_id)

Get the current circuit state for an expectation.

record_failure(actor_id, expectation_id, config)

Record a failed episode. Increments failure count and may trip the circuit.

When the circuit trips and cancel_in_flight: true is set in config, running/blocked episodes for this actor+expectation are automatically canceled.

record_success(actor_id, expectation_id)

Record a successful episode. Resets the circuit to :closed.