pub struct CircuitBreaker { /* private fields */ }Expand description
Circuit breaker implementation for provider failure management.
This struct provides the main circuit breaker functionality, automatically detecting failures and managing state transitions to prevent cascading failures.
§Examples
use ultrafast_models_sdk::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig};
use std::time::Duration;
let config = CircuitBreakerConfig::default();
let circuit_breaker = CircuitBreaker::new("openai".to_string(), config);
// Execute operation with circuit breaker protection
let result = circuit_breaker.call(|| async {
// Your provider operation here
Ok("success")
}).await;Implementations§
Source§impl CircuitBreaker
impl CircuitBreaker
Sourcepub fn new(name: String, config: CircuitBreakerConfig) -> Self
pub fn new(name: String, config: CircuitBreakerConfig) -> Self
Create a new circuit breaker with the specified configuration.
§Arguments
name- Unique identifier for this circuit breakerconfig- Configuration parameters for circuit breaker behavior
§Examples
use ultrafast_models_sdk::circuit_breaker::{CircuitBreaker, CircuitBreakerConfig};
let config = CircuitBreakerConfig::default();
let circuit_breaker = CircuitBreaker::new("anthropic".to_string(), config);Sourcepub async fn call<F, Fut, T, E>(
&self,
operation: F,
) -> Result<T, CircuitBreakerError>
pub async fn call<F, Fut, T, E>( &self, operation: F, ) -> Result<T, CircuitBreakerError>
Execute an operation with circuit breaker protection.
This method automatically manages the circuit breaker state based on the success or failure of the provided operation.
§Arguments
operation- The operation to execute with circuit breaker protection
§Returns
Returns the result of the operation, or a circuit breaker error if the operation should be blocked.
§Examples
use ultrafast_models_sdk::circuit_breaker::{CircuitBreaker, CircuitBreakerError};
let circuit_breaker = CircuitBreaker::new("provider".to_string(), config);
let result = circuit_breaker.call(|| async {
// Your provider call here
provider.chat_completion(request).await
}).await;
match result {
Ok(response) => println!("Success"),
Err(CircuitBreakerError::Open) => println!("Circuit is open"),
Err(CircuitBreakerError::Timeout) => println!("Request timed out"),
}Sourcepub async fn get_state(&self) -> CircuitState
pub async fn get_state(&self) -> CircuitState
Get the current circuit breaker state.
Returns the current operational state of the circuit breaker.
Sourcepub async fn get_metrics(&self) -> CircuitBreakerMetrics
pub async fn get_metrics(&self) -> CircuitBreakerMetrics
Get comprehensive metrics for this circuit breaker.
Returns detailed metrics including state, failure/success counts, and timing information.
Sourcepub async fn force_open(&self)
pub async fn force_open(&self)
Force the circuit breaker to open state.
This method manually opens the circuit breaker, useful for testing or emergency situations.
Sourcepub async fn force_closed(&self)
pub async fn force_closed(&self)
Force the circuit breaker to closed state.
This method manually closes the circuit breaker, useful for testing or when you want to reset the circuit breaker state.