PhoenixMicro.Middleware.CircuitBreaker.Store (PhoenixMicro v1.0.0)

Copy Markdown View Source

ETS-backed store for circuit breaker state.

One entry per fuse name:

{fuse_name, state, failure_window, opened_at}

Where:

  • state is :closed | {:open, monotonic_ms} | :half_open

  • failure_window is a list of {monotonic_ms, :failure} tuples used to count failures within the sliding time window.

The process owns the table so it survives crashes of consumers.

Summary

Functions

Returns a snapshot of all fuse states (useful for health checks).

Returns a specification to start this module under a supervisor.

Records a failure within the sliding window. Returns the current failure count within the window.

Records a success — clears the failure window.

Resets all fuses to CLOSED. Useful in tests.

Resets the breaker to CLOSED.

Transitions to HALF_OPEN for probe.

Trips the breaker to OPEN.

Returns the current state for the given fuse: :closed | {:open, ms} | :half_open

Functions

all_states()

@spec all_states() :: [{term(), :closed | {:open, integer()} | :half_open}]

Returns a snapshot of all fuse states (useful for health checks).

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

record_failure(fuse, window_ms)

@spec record_failure(term(), pos_integer()) :: non_neg_integer()

Records a failure within the sliding window. Returns the current failure count within the window.

record_success(fuse)

@spec record_success(term()) :: :ok

Records a success — clears the failure window.

reset_all()

@spec reset_all() :: :ok

Resets all fuses to CLOSED. Useful in tests.

set_closed(fuse)

@spec set_closed(term()) :: :ok

Resets the breaker to CLOSED.

set_half_open(fuse)

@spec set_half_open(term()) :: :ok

Transitions to HALF_OPEN for probe.

set_open(fuse)

@spec set_open(term()) :: :ok

Trips the breaker to OPEN.

start_link(opts \\ [])

@spec start_link(keyword()) :: GenServer.on_start()

state(fuse)

@spec state(term()) :: :closed | {:open, integer()} | :half_open

Returns the current state for the given fuse: :closed | {:open, ms} | :half_open