pub struct ExponentialBackoff { /* private fields */ }
Expand description
An exponential backoff strategy.
This backoff strategy provides backoff durations that increase exponentially based on a user-provided error count, with a minimum and maximum bound on the duration. Additionally, jitter can be added to the backoff duration in order to help avoiding multiple callers retrying their requests at the same time.
Implementations§
Source§impl ExponentialBackoff
impl ExponentialBackoff
Sourcepub fn new(min_backoff: Duration, max_backoff: Duration) -> Self
pub fn new(min_backoff: Duration, max_backoff: Duration) -> Self
Creates a new ExponentialBackoff
with the given minimum and maximum backoff durations.
Jitter is not applied to the calculated backoff durations.
Sourcepub fn with_jitter(
min_backoff: Duration,
max_backoff: Duration,
min_backoff_factor: f64,
) -> Self
pub fn with_jitter( min_backoff: Duration, max_backoff: Duration, min_backoff_factor: f64, ) -> Self
Creates a new ExponentialBackoff
with the given minimum and maximum backoff durations, and minimum backoff
factor.
Jitter is applied to the calculated backoff durations based on the minimum backoff factor, such that any given
backoff duration will be between D/min_backoff_factor
and D
, where D
is the calculated backoff duration
for the given external error count. If the minimum backoff factor is set to 1.0 or less, then jitter will be
disabled.
Concretely, this means that with a minimum backoff duration of 10ms, and a minimum backoff factor of 2.0, the duration for an error count of one would be 20ms without jitter, but anywhere between 10ms and 20ms with jitter. For an error count of two, it be 40ms without jitter, but anywhere between 20ms and 40ms with jitter.
Sourcepub fn with_rng<R>(self, rng: R) -> Self
pub fn with_rng<R>(self, rng: R) -> Self
Sets the random number generator to use for calculating jittered backoff durations.
Useful for testing purposes, where the RNG must be overridden to add determinism. The RNG is shared atomically behind a mutex, allowing it to be cloned, so care should be taken to never use this outside of tests.
Defaults to a lazily-initialized, thread-local CSPRNG seeded by the operating system.
Sourcepub fn get_backoff_duration(&mut self, error_count: u32) -> Duration
pub fn get_backoff_duration(&mut self, error_count: u32) -> Duration
Calculates the backoff duration for the given error count.
The error count value is generally user-defined, but should constitute the number of consecutive errors, or attempts, that have been made when retrying an operation or request.
Trait Implementations§
Source§impl Clone for ExponentialBackoff
impl Clone for ExponentialBackoff
Source§fn clone(&self) -> ExponentialBackoff
fn clone(&self) -> ExponentialBackoff
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for ExponentialBackoff
impl RefUnwindSafe for ExponentialBackoff
impl Send for ExponentialBackoff
impl Sync for ExponentialBackoff
impl Unpin for ExponentialBackoff
impl UnwindSafe for ExponentialBackoff
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,
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> 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::Request
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> Track for T
impl<T> Track for T
Source§fn track_allocations(self, token: AllocationGroupToken) -> Tracked<Self>
fn track_allocations(self, token: AllocationGroupToken) -> Tracked<Self>
Tracked
wrapper. Read moreSource§fn in_current_allocation_group(self) -> Tracked<Self>
fn in_current_allocation_group(self) -> Tracked<Self>
Tracked
wrapper. Read more