pub struct CircuitBreaker { /* private fields */ }Expand description
Sliding-window loss breaker.
§Example
use rustrade_risk::{CircuitBreaker, CircuitBreakerConfig};
let mut cb = CircuitBreaker::new(CircuitBreakerConfig {
loss_limit: 3,
window_secs: 3600,
cooldown_secs: 600,
});
cb.record_loss();
cb.record_loss();
assert!(!cb.is_tripped());
cb.record_loss();
assert!(cb.is_tripped());Implementations§
Source§impl CircuitBreaker
impl CircuitBreaker
Sourcepub fn new(config: CircuitBreakerConfig) -> CircuitBreaker
pub fn new(config: CircuitBreakerConfig) -> CircuitBreaker
Create with the default system clock.
Sourcepub fn with_clock(
config: CircuitBreakerConfig,
clock: Arc<dyn Clock>,
) -> CircuitBreaker
pub fn with_clock( config: CircuitBreakerConfig, clock: Arc<dyn Clock>, ) -> CircuitBreaker
Create with an injected clock — typically Arc<ManualClock> from
crate::clock in tests.
Sourcepub fn tick(&mut self)
pub fn tick(&mut self)
Call once per decision tick to auto-reset the breaker after cooldown and evict stale loss timestamps.
Sourcepub fn record_loss(&mut self)
pub fn record_loss(&mut self)
Record a losing trade. Trips the breaker if the rolling count
within window_secs reaches loss_limit.
Sourcepub fn record_win(&mut self)
pub fn record_win(&mut self)
Record a winning trade.
Does NOT clear the tripped state — once tripped, only elapsed cooldown can un-trip the breaker. A single win is not evidence that market conditions have recovered.
Sourcepub fn is_tripped(&self) -> bool
pub fn is_tripped(&self) -> bool
Is the breaker currently tripped and within its cooldown window?
Sourcepub fn snapshot(&self) -> CircuitBreakerSnapshot
pub fn snapshot(&self) -> CircuitBreakerSnapshot
Capture the mutable breaker state for persistence.
Pairs with Self::restore. The configured limits and the clock
are intentionally excluded — only the recorded loss timestamps and
trip time are persisted.
Sourcepub fn restore(&mut self, snap: CircuitBreakerSnapshot)
pub fn restore(&mut self, snap: CircuitBreakerSnapshot)
Restore breaker state from a CircuitBreakerSnapshot.
Overwrites the loss window and trip time; keeps the configured
limits and clock from the live instance. Call Self::tick
afterwards to evict losses now outside the window and auto-reset the
breaker if its cooldown elapsed while the process was down.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Manually clear the breaker. Typically not called in production — the cooldown does this automatically.
Sourcepub fn recent_loss_count(&self) -> usize
pub fn recent_loss_count(&self) -> usize
Number of losses currently in the rolling window.
Sourcepub fn cooldown_remaining(&self) -> Option<Duration>
pub fn cooldown_remaining(&self) -> Option<Duration>
Cooldown time remaining if tripped, else None.
Trait Implementations§
Source§impl Clone for CircuitBreaker
impl Clone for CircuitBreaker
Source§fn clone(&self) -> CircuitBreaker
fn clone(&self) -> CircuitBreaker
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more