pub struct CircuitBreaker<S, P> { /* private fields */ }circuit-breaker only.Expand description
Tower Service implementing the circuit-breaker pattern.
The open/probe/close criteria are driven by a CircuitPolicy, making
the triggering logic independently customisable. The built-in policy is
ConsecutiveFailures; supply any type implementing CircuitPolicy
via CircuitBreaker::new or CircuitBreakerLayer::with_policy for
custom strategies.
§Thread safety
CircuitBreaker<S, P> is Send when both S and P are Send.
This is enforced structurally: the policy is held behind
Arc<Mutex<P>>, so Arc<Mutex<P>>: Send requires P: Send.
No explicit bound is placed on P in the Service impl, so
!Send policies can still be used in single-threaded contexts without
a compile error. P: Sync is never required.
See CircuitPolicy for more detail.
See the module documentation for a full example.
Implementations§
Source§impl<S, P: CircuitPolicy> CircuitBreaker<S, P>
impl<S, P: CircuitPolicy> CircuitBreaker<S, P>
Sourcepub fn new(inner: S, policy: P) -> Self
pub fn new(inner: S, policy: P) -> Self
Wrap inner with the given CircuitPolicy.
Sourcepub fn status(&self) -> CircuitStatus
pub fn status(&self) -> CircuitStatus
Return the current CircuitStatus.
Sourcepub fn reset(&self)
pub fn reset(&self)
Manually close the circuit (e.g. after operator confirmation that the backend is healthy).
Calls CircuitPolicy::on_half_open to reset any per-window counters
in the policy, then sets the status to Closed.
Trait Implementations§
Source§impl<S: Clone, P: Clone> Clone for CircuitBreaker<S, P>
impl<S: Clone, P: Clone> Clone for CircuitBreaker<S, P>
Source§fn clone(&self) -> CircuitBreaker<S, P>
fn clone(&self) -> CircuitBreaker<S, P>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<S, P, Request> Service<Request> for CircuitBreaker<S, P>where
S: Service<Request>,
P: CircuitPolicy,
impl<S, P, Request> Service<Request> for CircuitBreaker<S, P>where
S: Service<Request>,
P: CircuitPolicy,
Source§type Future = ResponseFuture<<S as Service<Request>>::Future, <S as Service<Request>>::Response, <S as Service<Request>>::Error, P>
type Future = ResponseFuture<<S as Service<Request>>::Future, <S as Service<Request>>::Response, <S as Service<Request>>::Error, P>
Auto Trait Implementations§
impl<S, P> Freeze for CircuitBreaker<S, P>where
S: Freeze,
impl<S, P> RefUnwindSafe for CircuitBreaker<S, P>where
S: RefUnwindSafe,
impl<S, P> Send for CircuitBreaker<S, P>
impl<S, P> Sync for CircuitBreaker<S, P>
impl<S, P> Unpin for CircuitBreaker<S, P>where
S: Unpin,
impl<S, P> UnsafeUnpin for CircuitBreaker<S, P>where
S: UnsafeUnpin,
impl<S, P> UnwindSafe for CircuitBreaker<S, P>where
S: UnwindSafe,
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<M, S, Target, Request> MakeService<Target, Request> for M
impl<M, S, Target, Request> MakeService<Target, Request> for M
Source§type Response = <S as Service<Request>>::Response
type Response = <S as Service<Request>>::Response
make only.Source§type Error = <S as Service<Request>>::Error
type Error = <S as Service<Request>>::Error
make only.Source§type Service = S
type Service = S
make only.Service value created by this factorySource§type MakeError = <M as Service<Target>>::Error
type MakeError = <M as Service<Target>>::Error
make only.Source§type Future = <M as Service<Target>>::Future
type Future = <M as Service<Target>>::Future
make only.Service instance.Source§fn poll_ready(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>
fn poll_ready( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<(), <M as MakeService<Target, Request>>::MakeError>>
make only.Poll::Ready when the factory is able to create more services. Read moreSource§fn make_service(
&mut self,
target: Target,
) -> <M as MakeService<Target, Request>>::Future
fn make_service( &mut self, target: Target, ) -> <M as MakeService<Target, Request>>::Future
make only.Source§fn into_service(self) -> IntoService<Self, Request>where
Self: Sized,
fn into_service(self) -> IntoService<Self, Request>where
Self: Sized,
make only.Source§fn as_service(&mut self) -> AsService<'_, Self, Request>where
Self: Sized,
fn as_service(&mut self) -> AsService<'_, Self, Request>where
Self: Sized,
make only.Source§impl<T, Request> ServiceExt<Request> for T
impl<T, Request> ServiceExt<Request> for T
Source§fn ready(&mut self) -> Ready<'_, Self, Request> ⓘwhere
Self: Sized,
fn ready(&mut self) -> Ready<'_, Self, Request> ⓘwhere
Self: Sized,
util only.Source§fn ready_oneshot(self) -> ReadyOneshot<Self, Request> ⓘwhere
Self: Sized,
fn ready_oneshot(self) -> ReadyOneshot<Self, Request> ⓘwhere
Self: Sized,
util only.Source§fn oneshot(self, req: Request) -> Oneshot<Self, Request> ⓘwhere
Self: Sized,
fn oneshot(self, req: Request) -> Oneshot<Self, Request> ⓘwhere
Self: Sized,
util only.Service, calling it with the provided request once it is ready.Source§fn call_all<S>(self, reqs: S) -> CallAll<Self, S>where
Self: Sized,
S: Stream<Item = Request>,
fn call_all<S>(self, reqs: S) -> CallAll<Self, S>where
Self: Sized,
S: Stream<Item = Request>,
util only.Source§fn and_then<F>(self, f: F) -> AndThen<Self, F>
fn and_then<F>(self, f: F) -> AndThen<Self, F>
util only.poll_ready method. Read moreSource§fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
fn map_response<F, Response>(self, f: F) -> MapResponse<Self, F>
util only.poll_ready method. Read moreSource§fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
fn map_err<F, Error>(self, f: F) -> MapErr<Self, F>
util only.poll_ready method. Read moreSource§fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
fn map_result<F, Response, Error>(self, f: F) -> MapResult<Self, F>
util only.Result<Self::Response, Self::Error>)
to a different value, regardless of whether the future succeeds or
fails. Read moreSource§fn map_request<F, NewRequest>(self, f: F) -> MapRequest<Self, F>
fn map_request<F, NewRequest>(self, f: F) -> MapRequest<Self, F>
util only.Source§fn filter<F, NewRequest>(self, filter: F) -> Filter<Self, F>
fn filter<F, NewRequest>(self, filter: F) -> Filter<Self, F>
util and filter only.Source§fn filter_async<F, NewRequest>(self, filter: F) -> AsyncFilter<Self, F>where
Self: Sized,
F: AsyncPredicate<NewRequest>,
fn filter_async<F, NewRequest>(self, filter: F) -> AsyncFilter<Self, F>where
Self: Sized,
F: AsyncPredicate<NewRequest>,
util and filter only.AsyncFilter that conditionally accepts or
rejects requests based on an [async predicate]. Read moreSource§fn then<F, Response, Error, Fut>(self, f: F) -> Then<Self, F>
fn then<F, Response, Error, Fut>(self, f: F) -> Then<Self, F>
util only.Source§fn map_future<F, Fut, Response, Error>(self, f: F) -> MapFuture<Self, F>
fn map_future<F, Fut, Response, Error>(self, f: F) -> MapFuture<Self, F>
util only.Source§fn buffered(self, bound: usize) -> Buffer<Request, Self::Future>
fn buffered(self, bound: usize) -> Buffer<Request, Self::Future>
util and buffer only.Source§fn retry<P>(self, policy: P) -> Retry<P, Self>where
Self: Sized,
fn retry<P>(self, policy: P) -> Retry<P, Self>where
Self: Sized,
util and retry only.