Struct failsafe::StateMachine
source · [−]pub struct StateMachine<POLICY, INSTRUMENT> { /* private fields */ }
Expand description
A circuit breaker implementation backed by state machine.
It is implemented via a finite state machine with three states: Closed
, Open
and HalfOpen
.
The state machine does not know anything about the backend’s state by itself, but uses the
information provided by the method via on_success
and on_error
events. Before communicating
with the backend, the the permission to do so must be obtained via the method is_call_permitted
.
The state of the state machine changes from Closed
to Open
when the FailurePolicy
reports that the failure rate is above a (configurable) threshold. Then, all access to the backend
is blocked for a time duration provided by FailurePolicy
.
After the time duration has elapsed, the state changes from Open
to HalfOpen
and allows
calls to see if the backend is still unavailable or has become available again. If the circuit
breaker receives a failure on the next call, the state will change back to Open
. Otherwise
it changes to Closed
.
Implementations
sourceimpl<POLICY, INSTRUMENT> StateMachine<POLICY, INSTRUMENT> where
POLICY: FailurePolicy,
INSTRUMENT: Instrument,
impl<POLICY, INSTRUMENT> StateMachine<POLICY, INSTRUMENT> where
POLICY: FailurePolicy,
INSTRUMENT: Instrument,
sourcepub fn new(failure_policy: POLICY, instrument: INSTRUMENT) -> Self
pub fn new(failure_policy: POLICY, instrument: INSTRUMENT) -> Self
Creates a new state machine with given failure policy and instrument.
sourcepub fn is_call_permitted(&self) -> bool
pub fn is_call_permitted(&self) -> bool
Requests permission to call.
It returns true
if a call is allowed, or false
if prohibited.
sourcepub fn on_success(&self)
pub fn on_success(&self)
Records a successful call.
This method must be invoked when a call was success.
Trait Implementations
sourceimpl<POLICY, INSTRUMENT> CircuitBreaker for StateMachine<POLICY, INSTRUMENT> where
POLICY: FailurePolicy,
INSTRUMENT: Instrument,
impl<POLICY, INSTRUMENT> CircuitBreaker for StateMachine<POLICY, INSTRUMENT> where
POLICY: FailurePolicy,
INSTRUMENT: Instrument,
sourceimpl<POLICY, INSTRUMENT> CircuitBreaker for StateMachine<POLICY, INSTRUMENT> where
POLICY: FailurePolicy + Send + Sync,
INSTRUMENT: Instrument + Send + Sync,
impl<POLICY, INSTRUMENT> CircuitBreaker for StateMachine<POLICY, INSTRUMENT> where
POLICY: FailurePolicy + Send + Sync,
INSTRUMENT: Instrument + Send + Sync,
sourcefn is_call_permitted(&self) -> bool
fn is_call_permitted(&self) -> bool
Requests permission to call. Read more
sourcefn call_with<F, P>(
&self,
predicate: P,
f: F
) -> ResponseFuture<F, Self::FailurePolicy, Self::Instrument, P>ⓘNotable traits for ResponseFuture<FUTURE, POLICY, INSTRUMENT, PREDICATE>impl<FUTURE, POLICY, INSTRUMENT, PREDICATE> Future for ResponseFuture<FUTURE, POLICY, INSTRUMENT, PREDICATE> where
FUTURE: TryFuture,
POLICY: FailurePolicy,
INSTRUMENT: Instrument,
PREDICATE: FailurePredicate<FUTURE::Error>, type Output = Result<FUTURE::Ok, Error<FUTURE::Error>>;
where
F: TryFuture,
P: FailurePredicate<F::Error>,
fn call_with<F, P>(
&self,
predicate: P,
f: F
) -> ResponseFuture<F, Self::FailurePolicy, Self::Instrument, P>ⓘNotable traits for ResponseFuture<FUTURE, POLICY, INSTRUMENT, PREDICATE>impl<FUTURE, POLICY, INSTRUMENT, PREDICATE> Future for ResponseFuture<FUTURE, POLICY, INSTRUMENT, PREDICATE> where
FUTURE: TryFuture,
POLICY: FailurePolicy,
INSTRUMENT: Instrument,
PREDICATE: FailurePredicate<FUTURE::Error>, type Output = Result<FUTURE::Ok, Error<FUTURE::Error>>;
where
F: TryFuture,
P: FailurePredicate<F::Error>,
FUTURE: TryFuture,
POLICY: FailurePolicy,
INSTRUMENT: Instrument,
PREDICATE: FailurePredicate<FUTURE::Error>, type Output = Result<FUTURE::Ok, Error<FUTURE::Error>>;
Executes a given future within circuit breaker. Read more
sourcefn call<F>(
&self,
f: F
) -> ResponseFuture<F, Self::FailurePolicy, Self::Instrument, Any>ⓘNotable traits for ResponseFuture<FUTURE, POLICY, INSTRUMENT, PREDICATE>impl<FUTURE, POLICY, INSTRUMENT, PREDICATE> Future for ResponseFuture<FUTURE, POLICY, INSTRUMENT, PREDICATE> where
FUTURE: TryFuture,
POLICY: FailurePolicy,
INSTRUMENT: Instrument,
PREDICATE: FailurePredicate<FUTURE::Error>, type Output = Result<FUTURE::Ok, Error<FUTURE::Error>>;
where
F: TryFuture,
fn call<F>(
&self,
f: F
) -> ResponseFuture<F, Self::FailurePolicy, Self::Instrument, Any>ⓘNotable traits for ResponseFuture<FUTURE, POLICY, INSTRUMENT, PREDICATE>impl<FUTURE, POLICY, INSTRUMENT, PREDICATE> Future for ResponseFuture<FUTURE, POLICY, INSTRUMENT, PREDICATE> where
FUTURE: TryFuture,
POLICY: FailurePolicy,
INSTRUMENT: Instrument,
PREDICATE: FailurePredicate<FUTURE::Error>, type Output = Result<FUTURE::Ok, Error<FUTURE::Error>>;
where
F: TryFuture,
FUTURE: TryFuture,
POLICY: FailurePolicy,
INSTRUMENT: Instrument,
PREDICATE: FailurePredicate<FUTURE::Error>, type Output = Result<FUTURE::Ok, Error<FUTURE::Error>>;
Executes a given future within circuit breaker. Read more
sourceimpl<POLICY, INSTRUMENT> Clone for StateMachine<POLICY, INSTRUMENT>
impl<POLICY, INSTRUMENT> Clone for StateMachine<POLICY, INSTRUMENT>
Auto Trait Implementations
impl<POLICY, INSTRUMENT> !RefUnwindSafe for StateMachine<POLICY, INSTRUMENT>
impl<POLICY, INSTRUMENT> Send for StateMachine<POLICY, INSTRUMENT> where
INSTRUMENT: Send + Sync,
POLICY: Send,
impl<POLICY, INSTRUMENT> Sync for StateMachine<POLICY, INSTRUMENT> where
INSTRUMENT: Send + Sync,
POLICY: Send,
impl<POLICY, INSTRUMENT> Unpin for StateMachine<POLICY, INSTRUMENT>
impl<POLICY, INSTRUMENT> !UnwindSafe for StateMachine<POLICY, INSTRUMENT>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more