Tinkex.CircuitBreaker.Registry (Tinkex v0.3.4)

View Source

ETS-based registry for circuit breaker state.

Circuit breakers are identified by endpoint names and can be shared across processes in the same node. Updates are versioned to avoid lost-update races under concurrency.

Usage

# Initialize the registry (typically in Application.start/2)
CircuitBreaker.Registry.init()

# Execute a call through a circuit breaker
case CircuitBreaker.Registry.call("sampling-endpoint", fn ->
  Tinkex.API.Sampling.sample_async(request, opts)
end) do
  {:ok, result} -> handle_success(result)
  {:error, :circuit_open} -> {:error, "Service temporarily unavailable"}
  {:error, reason} -> {:error, reason}
end

# Check circuit state
CircuitBreaker.Registry.state("sampling-endpoint")
# => :closed | :open | :half_open

# Reset a specific circuit
CircuitBreaker.Registry.reset("sampling-endpoint")

Summary

Functions

Execute a function through a named circuit breaker.

Delete a circuit breaker from the registry.

Initialize the circuit breaker registry.

List all circuit breakers and their states.

Reset a circuit breaker to closed state.

Get the current state of a circuit breaker.

Functions

call(name, fun, opts \\ [])

@spec call(String.t(), (-> result), keyword()) :: result | {:error, :circuit_open}
when result: term()

Execute a function through a named circuit breaker.

Creates the circuit breaker if it doesn't exist.

Options

  • :failure_threshold - Failures before opening (default: 5)
  • :reset_timeout_ms - Open duration before half-open (default: 30,000)
  • :half_open_max_calls - Calls allowed in half-open (default: 1)
  • :success? - Custom success classifier function

delete(name)

@spec delete(String.t()) :: :ok

Delete a circuit breaker from the registry.

init()

@spec init() :: :ok

Initialize the circuit breaker registry.

Creates the ETS table if it doesn't exist. Safe to call multiple times.

list()

@spec list() :: [{String.t(), Tinkex.CircuitBreaker.state()}]

List all circuit breakers and their states.

reset(name)

@spec reset(String.t()) :: :ok

Reset a circuit breaker to closed state.

state(name)

Get the current state of a circuit breaker.

Returns :closed if the circuit breaker doesn't exist.