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:
jgrusewski
2026-02-22 22:45:08 +01:00
parent 43f0fa90fc
commit bb4d4159d8

View File

@@ -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 {