pub struct RetryPolicy { /* private fields */ }Expand description
How many times to attempt an operation and how long to wait between tries.
max_attempts counts the total number of attempts, including the first
one, so max_attempts = 1 means “try once, never retry” and
max_attempts = 3 means “the first try plus up to two retries”. A value of
0 is rejected by new.
The Backoff supplies the delay before each retry. It is consulted only
for delay values; the attempt count is governed solely by max_attempts, so
the two limits never fight. If the backoff yields no delay for a given retry
index, Duration::ZERO is used.
Implementations§
Source§impl RetryPolicy
impl RetryPolicy
Sourcepub const fn new(max_attempts: u32, backoff: Backoff) -> Option<Self>
pub const fn new(max_attempts: u32, backoff: Backoff) -> Option<Self>
Creates a policy that makes at most max_attempts attempts, waiting
according to backoff between them.
Returns None if max_attempts is 0, which would never run the
operation at all.
Examples found in repository?
29fn main() {
30 let policy = RetryPolicy::new(4, Backoff::constant(Duration::from_millis(20)))
31 .expect("max_attempts is non-zero");
32
33 let mut attempt = 0;
34 let result: Result<u32, RetryError<&str>> = block_on(retry_async(
35 &policy,
36 || {
37 attempt += 1;
38 let outcome = if attempt < 3 {
39 Err("temporary")
40 } else {
41 Ok(200)
42 };
43 async move { outcome }
44 },
45 |_error| true,
46 |delay| async move {
47 // Your runtime's async sleep goes here. This example resolves
48 // immediately; the sleep future is entirely user-provided.
49 let _ = delay;
50 },
51 ));
52
53 println!("async result after {attempt} attempt(s): {result:?}");
54}More examples
17fn main() {
18 let policy = RetryPolicy::new(
19 5,
20 Backoff::exponential(Duration::from_millis(50), 2).with_max_delay(Duration::from_secs(1)),
21 )
22 .expect("max_attempts is non-zero");
23
24 // An operation that fails twice with a temporary error, then succeeds.
25 let mut attempt = 0;
26 let result: Result<&str, RetryError<ApiError>> = retry_with_sleep(
27 &policy,
28 || {
29 attempt += 1;
30 println!("attempt {attempt}");
31 if attempt < 3 {
32 Err(ApiError::Temporary)
33 } else {
34 Ok("payload")
35 }
36 },
37 |error| matches!(error, ApiError::Temporary), // retry only temporary errors
38 |delay| {
39 // You provide the waiting. In real code, call your platform or
40 // runtime sleep here; this example only reports the delay so it
41 // stays dependency-free and instant.
42 println!(" would wait {delay:?} before the next attempt");
43 },
44 );
45 match result {
46 Ok(body) => println!("succeeded with: {body}"),
47 Err(error) => println!("gave up: {error:?}"),
48 }
49
50 // A fatal error stops immediately, even though attempts remain.
51 let mut attempt = 0;
52 let result: Result<&str, RetryError<ApiError>> = retry_with_sleep(
53 &policy,
54 || {
55 attempt += 1;
56 Err(ApiError::Fatal)
57 },
58 |error| matches!(error, ApiError::Temporary),
59 |_delay| {},
60 );
61 println!(
62 "fatal path: stopped after {} attempt(s)",
63 result.unwrap_err().attempts()
64 );
65}Sourcepub const fn single(backoff: Backoff) -> Self
pub const fn single(backoff: Backoff) -> Self
A policy that tries exactly once and never retries.
Equivalent to RetryPolicy::new(1, _).unwrap(); the backoff is never
consulted because there is no retry.
Sourcepub const fn max_attempts(&self) -> u32
pub const fn max_attempts(&self) -> u32
The maximum number of attempts (always >= 1).
Sourcepub fn delay_before_retry(&self, completed_attempts: u32) -> Duration
pub fn delay_before_retry(&self, completed_attempts: u32) -> Duration
The delay to wait before the next retry, given how many attempts have already completed.
completed_attempts is the 1-based number of attempts already made, so
the delay before the first retry is delay_before_retry(1). The backoff
is indexed zero-based (retry 0 is the first retry); if it yields no
delay, Duration::ZERO is returned.
Trait Implementations§
Source§impl Clone for RetryPolicy
impl Clone for RetryPolicy
Source§fn clone(&self) -> RetryPolicy
fn clone(&self) -> RetryPolicy
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more