pub struct Attempt<F> { /* private fields */ }
Expand description
This type provides an API for retrying failable functions.
See the documentation for this type’s methods for detailed examples and the module documentation for an overview example.
Implementations§
Source§impl<F> Attempt<F>
impl<F> Attempt<F>
Sourcepub fn to(func: F) -> Attempt<F>
pub fn to(func: F) -> Attempt<F>
Constructs a new Attempt
which, when executed with either Attempt::run
or
[Attempt::run_async
], will run the provided function func
until it returns Ok
or
one of the limits are exceeded.
The Attempt
is constructed with the default configuration:
- No time delay between attempts (thread will not sleep)
- A default delay growth magnitude of 1.25 (25% increase each attempt)
- A cap on maximum tries of 10 These defaults are in place to hopefully prevent any accidental infinite loops.
Sourcepub fn infinitely<T, E>(func: F) -> T
pub fn infinitely<T, E>(func: F) -> T
Shortcut function to construct and run an Attempt
that will retry a function infinitely
until an Ok
value is produced.
Other than removing the limit on maximum attempts, this function uses the default
configuration outlined in the documentation for Attempt::to
. Using this function is
honestly a terrible idea, especially for production code, but it may be useful for
prototyping, idk.
Sourcepub fn no_max_tries(self) -> Self
pub fn no_max_tries(self) -> Self
Removes the limit on the maximum number of calls to the function that will be made before
propagating an Err
.
Please keep in mind that this setting can result in infinite loops and/or getting banned from a third-party API.
Sourcepub fn max_tries(self, max_tries: usize) -> Self
pub fn max_tries(self, max_tries: usize) -> Self
Sets the maximum bound for the maximum number of calls to the function that will be made
before propagating an Err
.
Must be greater than 0 (checked by assertion).
Sourcepub fn delay(self, delay: Duration) -> Self
pub fn delay(self, delay: Duration) -> Self
Sets the duration of the delay between each call to the function.
For synchronous functions, the delay is implemented using std::thread::sleep
. For
async functions, the delay uses [tokio::time::sleep
].
Sourcepub fn delay_growth_magnitude(self, magnitude: f32) -> Self
pub fn delay_growth_magnitude(self, magnitude: f32) -> Self
Sets the magnitude which the delay will be multiplied by after each epoch following the
second try. For example, say our magnitude is 2.0, and our delay is 1 second. If the first
call to the function fails, Attempt
will wait 1 second before executing the function
again. If that call also fails, Attempt
will wait 2 seconds before executing the
function a third time, and so on.