circuit_breaker/actor
Circuit breaker OTP actor.
Manages per-key circuit breakers with thread-safe actor-based state.
Usage
let config = circuit_breaker.CircuitConfig(
failure_threshold: 5,
recovery_timeout_ms: 30_000,
half_open_max_calls: 2,
)
let assert Ok(cb) = actor.start(config)
case actor.check_and_call(cb, "my_service") {
actor.Allowed -> // proceed
actor.Blocked(_) -> // fail fast
}
actor.record_success(cb, "my_service")
actor.record_failure(cb, "my_service")
Types
pub type CheckResult {
Allowed
Blocked(reason: String)
}
Constructors
-
Allowed -
Blocked(reason: String)
pub type CircuitBreakerActor =
process.Subject(CircuitBreakerCommand)
pub opaque type CircuitBreakerCommand
Values
pub fn check_and_call(
cb: process.Subject(CircuitBreakerCommand),
key: String,
) -> CheckResult
Check whether a call is allowed for the given key.
pub fn get_state(
cb: process.Subject(CircuitBreakerCommand),
key: String,
) -> circuit_breaker.CircuitState
Return the current circuit state for the given key.
pub fn record_failure(
cb: process.Subject(CircuitBreakerCommand),
key: String,
) -> Nil
Record a failed call for the given key.
pub fn record_success(
cb: process.Subject(CircuitBreakerCommand),
key: String,
) -> Nil
Record a successful call for the given key.
pub fn start(
config: circuit_breaker.CircuitConfig,
) -> Result(
process.Subject(CircuitBreakerCommand),
actor.StartError,
)
Start a circuit breaker actor with the given config.
pub fn start_linked(
config: circuit_breaker.CircuitConfig,
) -> Result(
actor.Started(process.Subject(CircuitBreakerCommand)),
actor.StartError,
)
Start and return the full Started record (for use with supervisors).