ExLLM.Infrastructure.CircuitBreaker (ex_llm v0.8.1)

View Source

High-performance circuit breaker implementation using ETS for concurrent access.

Implements the classic three-state pattern:

  • :closed - Normal operation, requests pass through
  • :open - Service failing, requests blocked with fail-fast
  • :half_open - Testing service recovery with limited requests

Summary

Functions

Apply a configuration profile to a circuit using ConfigManager.

Batch update configuration for multiple circuits.

Execute a function with circuit breaker protection.

Execute a function with both circuit breaker and bulkhead protection.

Get circuit configuration from ConfigManager.

Get current circuit breaker state and statistics.

Initialize the circuit breaker system. Called during ExLLM application startup.

Manually reset a circuit breaker to closed state.

Rollback circuit to previous configuration.

Update circuit configuration using ConfigManager with validation and history.

Update circuit breaker configuration at runtime.

Functions

apply_profile(circuit_name, profile_name)

Apply a configuration profile to a circuit using ConfigManager.

batch_update_config(circuit_names, opts)

Batch update configuration for multiple circuits.

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

Execute a function with circuit breaker protection.

Options

  • :failure_threshold - Number of failures before opening circuit (default: 5)
  • :success_threshold - Number of successes to close from half-open (default: 3)
  • :reset_timeout - Milliseconds before attempting half-open (default: 30_000)
  • :timeout - Function execution timeout (default: 30_000)
  • :name - Circuit name (auto-generated if not provided)

Examples

iex> ExLLM.CircuitBreaker.call("api_service", fn -> 
...>   HTTPClient.get("/api/data")
...> end)
{:ok, response}

iex> ExLLM.CircuitBreaker.call("failing_service", fn -> 
...>   raise "Service down"
...> end)
{:error, :circuit_open}

call_with_bulkhead(circuit_name, fun, opts \\ [])

Execute a function with both circuit breaker and bulkhead protection.

This combines circuit breaker fault tolerance with bulkhead concurrency limiting.

get_circuit_config(circuit_name)

Get circuit configuration from ConfigManager.

get_stats(circuit_name)

Get current circuit breaker state and statistics.

init()

Initialize the circuit breaker system. Called during ExLLM application startup.

reset(circuit_name)

Manually reset a circuit breaker to closed state.

rollback_config(circuit_name)

Rollback circuit to previous configuration.

update_circuit_config(circuit_name, config_changes)

Update circuit configuration using ConfigManager with validation and history.

update_config(circuit_name, opts)

Update circuit breaker configuration at runtime.