pub struct Retry {
initial_backoff: Duration,
factor: f64,
clamp_backoff: Duration,
max_duration: Duration,
max_tries: usize,
}
async
only.Expand description
Configures a retry operation.
See the module documentation for usage examples.
Fields§
§initial_backoff: Duration
§factor: f64
§clamp_backoff: Duration
§max_duration: Duration
§max_tries: usize
Implementations§
Source§impl Retry
impl Retry
Sourcepub fn initial_backoff(self, initial_backoff: Duration) -> Self
pub fn initial_backoff(self, initial_backoff: Duration) -> Self
Sets the initial backoff for the retry operation.
The initial backoff is the amount of time to wait if the first try fails.
Sourcepub fn clamp_backoff(self, clamp_backoff: Duration) -> Self
pub fn clamp_backoff(self, clamp_backoff: Duration) -> Self
Clamps the maximum backoff for the retry operation.
The maximum backoff is the maximum amount of time to wait between tries.
Sourcepub fn factor(self, factor: f64) -> Self
pub fn factor(self, factor: f64) -> Self
Sets the exponential backoff factor for the retry operation.
The time to wait is multiplied by this factor after each failed try. The default factor is two.
Sourcepub fn max_duration(self, duration: Duration) -> Self
pub fn max_duration(self, duration: Duration) -> Self
Sets the maximum duration.
If the operation is still failing after the specified duration
, then
the operation will be retried once more and retry
will
return the last error.
Calls to max_duration
will override any previous calls to
max_duration
.
Sourcepub fn retry<F, R, T, E>(self, f: F) -> Result<T, E>
pub fn retry<F, R, T, E>(self, f: F) -> Result<T, E>
Retries the fallible operation f
according to the configured policy.
The retry
method invokes f
repeatedly until it returns either
RetryResult::Ok
or RetryResult::FatalErr
, or until the maximum
duration or tries have been reached, as configured via
max_duration
or
max_tries
. If the last invocation of f
returns
RetryResult::Ok(t)
, then retry
returns Ok(t)
. If the last
invocation of f
returns RetryResult::RetriableErr(e)
or
RetryResult::FatalErr(e)
, then retry
returns Err(e)
.
As a convenience, f
can return any type that is convertible to a
RetryResult
. The conversion from Result
to RetryResult
converts
Err(e)
to RetryResult::RetryableErr(e)
, that is, it considers all
errors retryable.
After the first failure, retry
sleeps for the initial backoff
configured via initial_backoff
. After each
successive failure, retry
sleeps for twice the last backoff. If the
backoff would ever exceed the maximum backoff configured viq
Retry::clamp_backoff
, then the backoff is clamped to the specified
maximum.
The operation does not attempt to forcibly time out f
, even if there
is a maximum duration. If there is the possibility of f
blocking
forever, consider adding a timeout internally.
Sourcepub async fn retry_async<F, U, R, T, E>(self, f: F) -> Result<T, E>
pub async fn retry_async<F, U, R, T, E>(self, f: F) -> Result<T, E>
Like Retry::retry
but for asynchronous operations.
Sourcepub async fn retry_async_canceling<F, U, T, E>(self, f: F) -> Result<T, E>
pub async fn retry_async_canceling<F, U, T, E>(self, f: F) -> Result<T, E>
Like Retry::retry_async
but the operation will be canceled if the
maximum duration is reached.
Specifically, if the maximum duration is reached, the operation f
will
be forcibly canceled by dropping it. Canceling f
can be surprising if
the operation is not programmed to expect the possibility of not
resuming from an await
point; if you wish to always run f
to
completion, use Retry::retry_async
instead.
If f
is forcibly canceled, the error returned will be the error
returned by the prior invocation of f
. If there is no prior invocation
of f
, then an Elapsed
error is returned. The idea is that if f
fails three times in a row with a useful error message, and then the
fourth attempt is canceled because the timeout is reached, the caller
would rather see the useful error message from the third attempt, rather
than the “deadline exceeded” message from the fourth attempt.
Sourcepub async fn retry_async_with_state<F, S, U, R, T, E>(
self,
user_state: S,
f: F,
) -> (S, Result<T, E>)
pub async fn retry_async_with_state<F, S, U, R, T, E>( self, user_state: S, f: F, ) -> (S, Result<T, E>)
Like Retry::retry_async
but can pass around user specified state.
Sourcepub async fn retry_async_with_state_canceling<F, S, U, R, T, E>(
self,
user_state: S,
f: F,
) -> Result<T, E>where
F: FnMut(RetryState, S) -> U,
U: Future<Output = (S, R)>,
R: Into<RetryResult<T, E>>,
E: From<Elapsed> + Debug,
pub async fn retry_async_with_state_canceling<F, S, U, R, T, E>(
self,
user_state: S,
f: F,
) -> Result<T, E>where
F: FnMut(RetryState, S) -> U,
U: Future<Output = (S, R)>,
R: Into<RetryResult<T, E>>,
E: From<Elapsed> + Debug,
Combines Retry::retry_async_canceling
and Retry::retry_async_with_state
, so that
both timeouts are respected and user state can be passed in (bot not be read out).
Sourcepub fn into_retry_stream(self) -> RetryStream
pub fn into_retry_stream(self) -> RetryStream
Convert into RetryStream
Trait Implementations§
impl<'pin> Unpin for Retrywhere
PinnedFieldsOf<__Retry<'pin>>: Unpin,
Auto Trait Implementations§
impl Freeze for Retry
impl RefUnwindSafe for Retry
impl Send for Retry
impl Sync for Retry
impl UnwindSafe for Retry
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> 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::Request
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);
Source§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<'a, S, T> Semigroup<&'a S> for Twhere
T: Semigroup<S>,
impl<'a, S, T> Semigroup<&'a S> for Twhere
T: Semigroup<S>,
Source§fn plus_equals(&mut self, rhs: &&'a S)
fn plus_equals(&mut self, rhs: &&'a S)
std::ops::AddAssign
, for types that do not implement AddAssign
.