Railsr.Middleware.CircuitBreaker (Railsr v1.0.0)

Copy Markdown View Source

Simple three-state circuit breaker for the Railsr API.

States:

  • closed — normal operation; failures are counted
  • open — requests are rejected immediately without hitting the network
  • half-open — one probe request is allowed through to test recovery

Thresholds

  • Failure threshold: 5 consecutive failures → open
  • Recovery timeout: 30 seconds → half-open
  • Probe success → closed; probe failure → open (reset timer)

Summary

Functions

Check whether a request should be allowed through.

Returns a specification to start this module under a supervisor.

Record a failed request (increments counter, may trip breaker).

Record a successful request (resets failure count, closes if half-open).

Start the circuit breaker process.

Returns the current circuit state.

Functions

check()

@spec check() :: :ok | {:error, :circuit_open}

Check whether a request should be allowed through.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

record_failure()

@spec record_failure() :: :ok

Record a failed request (increments counter, may trip breaker).

record_success()

@spec record_success() :: :ok

Record a successful request (resets failure count, closes if half-open).

start_link(opts \\ [])

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

Start the circuit breaker process.

state()

@spec state() :: :closed | :open | :half_open

Returns the current circuit state.