pub struct DefaultCircuitBreaker { /* private fields */ }Expand description
默认断路器实现
提供 failure_threshold(连续失败阈值)+ reset_timeout(重置超时)的经典三态机。
§示例
ⓘ
use sz_orm_pool::circuit_breaker::{CircuitBreaker, CircuitState, DefaultCircuitBreaker};
use std::time::Duration;
let mut cb = DefaultCircuitBreaker::new(3, Duration::from_secs(60));
assert_eq!(cb.state(), CircuitState::Closed);
assert!(cb.can_execute());
// 3 次失败后跳闸
cb.record_failure();
cb.record_failure();
cb.record_failure();
assert_eq!(cb.state(), CircuitState::Open);
assert!(!cb.can_execute());
// 一次成功后恢复
cb.record_success();
assert_eq!(cb.state(), CircuitState::Closed);Implementations§
Source§impl DefaultCircuitBreaker
impl DefaultCircuitBreaker
Sourcepub fn new(
failure_threshold: usize,
reset_timeout: Duration,
) -> DefaultCircuitBreaker
pub fn new( failure_threshold: usize, reset_timeout: Duration, ) -> DefaultCircuitBreaker
创建默认断路器
failure_threshold:连续失败次数阈值(>= 时跳闸)reset_timeout:Open 状态持续时间,到达后进入 HalfOpen
Sourcepub fn consecutive_failures(&self) -> usize
pub fn consecutive_failures(&self) -> usize
获取连续失败次数(主要用于测试与监控)
Sourcepub fn failure_threshold(&self) -> usize
pub fn failure_threshold(&self) -> usize
获取失败阈值
Sourcepub fn reset_timeout(&self) -> Duration
pub fn reset_timeout(&self) -> Duration
获取重置超时
Trait Implementations§
Source§impl Debug for DefaultCircuitBreaker
impl Debug for DefaultCircuitBreaker
Source§impl Default for DefaultCircuitBreaker
impl Default for DefaultCircuitBreaker
Source§fn default() -> DefaultCircuitBreaker
fn default() -> DefaultCircuitBreaker
默认配置:5 次连续失败跳闸,30 秒后进入 HalfOpen
Auto Trait Implementations§
impl Freeze for DefaultCircuitBreaker
impl RefUnwindSafe for DefaultCircuitBreaker
impl Send for DefaultCircuitBreaker
impl Sync for DefaultCircuitBreaker
impl Unpin for DefaultCircuitBreaker
impl UnsafeUnpin for DefaultCircuitBreaker
impl UnwindSafe for DefaultCircuitBreaker
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more