pub struct KeyedCircuitBreaker<K: Hash + Eq + Clone + Send + Sync + 'static> { /* private fields */ }resilience only.Expand description
Generic keyed circuit breaker for per-resource isolation.
Unlike CircuitBreakerManager which uses String keys, this supports any
hashable key type. Each key maintains independent circuit breaker state,
allowing failures in one resource (e.g., one exchange, one endpoint) to
not affect others.
§Example
use allframe_core::resilience::{KeyedCircuitBreaker, CircuitBreakerConfig};
// Per-exchange circuit breakers for a trading system
let cb = KeyedCircuitBreaker::<String>::new(CircuitBreakerConfig::default());
// Check if we can make a request to Kraken
if cb.check(&"kraken".to_string()).is_ok() {
// Make request...
cb.record_success(&"kraken".to_string());
}
// Failures on Kraken don't affect Binance
cb.record_failure(&"kraken".to_string());
assert!(cb.check(&"binance".to_string()).is_ok());Implementations§
Source§impl<K: Hash + Eq + Clone + Send + Sync + 'static> KeyedCircuitBreaker<K>
impl<K: Hash + Eq + Clone + Send + Sync + 'static> KeyedCircuitBreaker<K>
Sourcepub fn new(config: CircuitBreakerConfig) -> Self
pub fn new(config: CircuitBreakerConfig) -> Self
Create a new keyed circuit breaker with the given configuration.
All keys will use the same circuit breaker configuration.
Sourcepub fn check(&self, key: &K) -> Result<(), CircuitOpenError>
pub fn check(&self, key: &K) -> Result<(), CircuitOpenError>
Check if a request for the given key is allowed.
Returns Ok(()) if allowed, Err(CircuitOpenError) if the circuit is
open.
Sourcepub fn record_success(&self, key: &K)
pub fn record_success(&self, key: &K)
Record a successful request for the given key.
Sourcepub fn record_failure(&self, key: &K)
pub fn record_failure(&self, key: &K)
Record a failed request for the given key.
Sourcepub async fn call<F, Fut, T, E>(
&self,
key: &K,
f: F,
) -> Result<T, CircuitBreakerError<E>>
pub async fn call<F, Fut, T, E>( &self, key: &K, f: F, ) -> Result<T, CircuitBreakerError<E>>
Execute an async operation through the circuit breaker for the given key.
Sourcepub fn get_state(&self, key: &K) -> Option<CircuitState>
pub fn get_state(&self, key: &K) -> Option<CircuitState>
Get the current state for a key.
Sourcepub fn get_stats(&self, key: &K) -> Option<CircuitBreakerStats>
pub fn get_stats(&self, key: &K) -> Option<CircuitBreakerStats>
Get statistics for a specific key.
Sourcepub fn get_all_stats(&self) -> Vec<(K, CircuitBreakerStats)>
pub fn get_all_stats(&self) -> Vec<(K, CircuitBreakerStats)>
Get statistics for all keys.
Trait Implementations§
Auto Trait Implementations§
impl<K> !Freeze for KeyedCircuitBreaker<K>
impl<K> !RefUnwindSafe for KeyedCircuitBreaker<K>
impl<K> Send for KeyedCircuitBreaker<K>
impl<K> Sync for KeyedCircuitBreaker<K>
impl<K> Unpin for KeyedCircuitBreaker<K>
impl<K> UnsafeUnpin for KeyedCircuitBreaker<K>
impl<K> !UnwindSafe for KeyedCircuitBreaker<K>
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
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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>
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>
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 moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().