refactor(ml): implement CircuitBreakerTrait for ML circuit breaker
Bridge the ML crate's parking_lot::RwLock-based CircuitBreaker to the shared CircuitBreakerTrait from common. Maps synchronous methods (allow_request, record_success, etc.) to the async trait interface and converts CircuitState variants to common::CircuitBreakerState. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -8,6 +8,9 @@ use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use ::common::resilience::circuit_breaker::{
|
||||
CircuitBreakerState as CommonCBState, CircuitBreakerTrait,
|
||||
};
|
||||
use parking_lot::RwLock;
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
@@ -253,6 +256,33 @@ impl CircuitBreaker {
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl CircuitBreakerTrait for CircuitBreaker {
|
||||
async fn can_execute(&self) -> bool {
|
||||
self.allow_request()
|
||||
}
|
||||
|
||||
async fn record_success(&self) {
|
||||
CircuitBreaker::record_success(self);
|
||||
}
|
||||
|
||||
async fn record_failure(&self) {
|
||||
CircuitBreaker::record_failure(self);
|
||||
}
|
||||
|
||||
async fn state(&self) -> CommonCBState {
|
||||
match self.current_state() {
|
||||
CircuitState::Closed => CommonCBState::Closed,
|
||||
CircuitState::Open => CommonCBState::Open,
|
||||
CircuitState::HalfOpen => CommonCBState::HalfOpen,
|
||||
}
|
||||
}
|
||||
|
||||
async fn reset(&self) {
|
||||
CircuitBreaker::reset(self);
|
||||
}
|
||||
}
|
||||
|
||||
/// Circuit breaker statistics
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CircuitBreakerStats {
|
||||
|
||||
Reference in New Issue
Block a user