pub struct RetryExecutor<E> { /* private fields */ }Expand description
Executor for retry operations
Implementations§
Source§impl<E> RetryExecutor<E>where
E: Error + 'static,
impl<E> RetryExecutor<E>where
E: Error + 'static,
Sourcepub fn new_exponential() -> Self
pub fn new_exponential() -> Self
Create a new retry executor with an exponential backoff strategy.
Delays start at 100 ms and double each attempt, up to 30 s. With
the jitter cargo feature on, each delay is jittered by ±20%
(see ExponentialBackoff::with_jitter) so retriers across a
fleet do not wake in step; use with_jitter
or with_backoff to choose explicitly.
Sourcepub fn new_linear() -> Self
pub fn new_linear() -> Self
Create a new retry executor with a linear backoff strategy
Sourcepub fn new_fixed(delay_ms: u64) -> Self
pub fn new_fixed(delay_ms: u64) -> Self
Create a new retry executor with a fixed backoff strategy
Sourcepub fn with_max_retries(self, max_retries: usize) -> Self
pub fn with_max_retries(self, max_retries: usize) -> Self
Set the maximum number of retries
Sourcepub fn with_backoff(self, backoff: impl Backoff) -> Self
pub fn with_backoff(self, backoff: impl Backoff) -> Self
Use backoff for the delay before each retry, replacing the
built-in schedule. Any Backoff works, including a configured
ExponentialBackoff (initial delay, factor, cap, jitter).
§Example
use error_forge::recovery::{ExponentialBackoff, RetryExecutor};
let executor = RetryExecutor::<std::io::Error>::new_exponential().with_backoff(
ExponentialBackoff::new()
.with_initial_delay(1)
.with_max_delay(5)
.with_jitter(true),
);
let result = executor.retry(|| Ok::<_, std::io::Error>(7));
assert_eq!(result.unwrap(), 7);Sourcepub fn with_jitter(self, jitter: bool) -> Self
pub fn with_jitter(self, jitter: bool) -> Self
Turn ±20% jitter on or off for an exponential executor (see
ExponentialBackoff::with_jitter). Has no effect on linear,
fixed, or custom backoff.
§Example
use error_forge::recovery::RetryExecutor;
let executor = RetryExecutor::<std::io::Error>::new_exponential().with_jitter(true);
assert!(executor.retry(|| Ok::<_, std::io::Error>(())).is_ok());Sourcepub fn with_retry_if<F>(self, predicate: F) -> Self
pub fn with_retry_if<F>(self, predicate: F) -> Self
Set a predicate to determine if an error should be retried