Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 🎉 add on_circuit_breaker_drop method for circuit breaker state … #114

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions sentinel-core/src/core/circuitbreaker/breaker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ pub trait StateChangeListener: Sync + Send {
/// Argument rule is copy from circuit breaker's rule, any changes of rule don't take effect for circuit breaker
/// Copying rule has a performance penalty and avoids invalid listeners as much as possible
fn on_transform_to_half_open(&self, prev: State, rule: Arc<Rule>);

/// `on_circuit_breaker_drop` is triggered when circuit breaker is drop.
/// Argument rule is copy from circuit breaker's rule, any changes of rule don't take effect for circuit breaker
/// Copying rule has a performance penalty and avoids invalid listeners as much as possible
fn on_circuit_breaker_drop(&self, prev: State, rule: Arc<Rule>);
Forsworns marked this conversation as resolved.
Show resolved Hide resolved
}

/// `CircuitBreakerTrait` is the basic trait of circuit breaker
Expand Down Expand Up @@ -343,6 +348,15 @@ impl BreakerBase {
}
}

impl Drop for BreakerBase {
fn drop(&mut self) {
let listeners = state_change_listeners().lock().unwrap();
for listener in &*listeners {
listener.on_circuit_breaker_drop(self.current_state(), Arc::clone(&self.rule));
}
}
}

#[cfg(test)]
pub(crate) use test::MockCircuitBreaker;

Expand Down Expand Up @@ -379,6 +393,7 @@ pub(crate) mod test {
fn on_transform_to_closed(&self, prev: State, rule: Arc<Rule>);
fn on_transform_to_open(&self, prev: State, rule: Arc<Rule>, snapshot: Option<Arc<Snapshot>>);
fn on_transform_to_half_open(&self, prev: State, rule: Arc<Rule>);
fn on_circuit_breaker_drop(&self, prev: State, rule: Arc<Rule>);
}
}

Expand Down Expand Up @@ -751,4 +766,37 @@ pub(crate) mod test {
assert!(changed);
assert!(breaker.next_retry_timestamp_ms() > 0);
}

#[test]
#[ignore]
fn on_circuit_breaker_drop() {
// by default, the state of `breaker` is `State::Closed` (see the impl of `Default` trait on `State`)
clear_state_change_listeners();
let mut listener = MockStateListener::new();
listener
.expect_on_circuit_breaker_drop()
.returning(|prev: State, rule: Arc<Rule>| {
logging::debug!(
"circuit breaker drop, strategy: {:?}, previous state: {:?}",
rule.strategy,
prev
);
assert_eq!(prev, State::Closed);
});
register_state_change_listeners(vec![Arc::new(listener)]);
let rule = Arc::new(Rule {
resource: "abc".into(),
strategy: BreakerStrategy::Custom(101),
retry_timeout_ms: 3000,
min_request_amount: 10,
stat_interval_ms: 10000,
max_allowed_rt_ms: 50,
threshold: 0.5,
..Default::default()
});
{
let _breaker = SlowRtBreaker::new(Arc::clone(&rule));
}
clear_state_change_listeners();
}
}
Loading