pub trait CircuitBreaker: Send + Sync {
// Required methods
fn state(&self) -> CircuitState;
fn record_success(&self);
fn record_failure(&self);
fn attempt_reset(&self) -> bool;
}Expand description
Circuit breaker port for resilience
Implements the circuit breaker pattern to prevent cascading failures when external services are unavailable. Uses interior mutability for state management.
§Example Implementation
use stygian_graph::ports::{CircuitBreaker, CircuitState};
use stygian_graph::domain::error::Result;
use parking_lot::RwLock;
use std::sync::Arc;
struct MyCircuitBreaker {
state: Arc<RwLock<CircuitState>>,
}
impl CircuitBreaker for MyCircuitBreaker {
fn state(&self) -> CircuitState {
*self.state.read()
}
fn record_success(&self) {
let mut state = self.state.write();
*state = CircuitState::Closed;
}
fn record_failure(&self) {
let mut state = self.state.write();
*state = CircuitState::Open;
}
fn attempt_reset(&self) -> bool {
let mut state = self.state.write();
if matches!(*state, CircuitState::Open) {
*state = CircuitState::HalfOpen;
true
} else {
false
}
}
}Required Methods§
Sourcefn state(&self) -> CircuitState
fn state(&self) -> CircuitState
Sourcefn record_success(&self)
fn record_success(&self)
Record successful operation
Transitions HalfOpen -> Closed, maintains Closed state.
§Example
// After successful API call
cb.record_success();Sourcefn record_failure(&self)
fn record_failure(&self)
Record failed operation
May transition Closed -> Open or HalfOpen -> Open depending on
failure threshold configuration.
§Example
// After failed API call
cb.record_failure();