pub trait CircuitBreaker: Send + Sync {
// Required methods
fn state(&self) -> CircuitState;
fn record_success(&mut self);
fn record_failure(&mut self);
fn can_execute(&mut self) -> bool;
fn reset(&mut self) -> bool;
}Expand description
断路器抽象 trait
P1-4:核心层定义的断路器接口,消除对 sz-orm-health 的反向依赖。
实现方需保证:
can_execute()在Open状态下根据reset_timeout自动迁移到HalfOpenrecord_success()将状态重置为Closed并清空失败计数record_failure()累计失败次数,达阈值时迁移到Open
Required Methods§
Sourcefn state(&self) -> CircuitState
fn state(&self) -> CircuitState
获取当前断路器状态
Sourcefn record_success(&mut self)
fn record_success(&mut self)
记录一次成功请求
通常将状态重置为 Closed 并清空连续失败计数。
Sourcefn record_failure(&mut self)
fn record_failure(&mut self)
记录一次失败请求
累计连续失败次数,达到阈值时将状态迁移到 Open。
Sourcefn can_execute(&mut self) -> bool
fn can_execute(&mut self) -> bool
判断当前是否允许请求通过
在 Open 状态下,若 reset_timeout 已到达,应迁移到 HalfOpen 并返回 true。
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".