Skip to main content

RetryExecutor

Struct RetryExecutor 

Source
pub struct RetryExecutor<E> { /* private fields */ }
Expand description

Executor for retry operations

Implementations§

Source§

impl<E> RetryExecutor<E>
where E: Error + 'static,

Source

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.

Source

pub fn new_linear() -> Self

Create a new retry executor with a linear backoff strategy

Source

pub fn new_fixed(delay_ms: u64) -> Self

Create a new retry executor with a fixed backoff strategy

Source

pub fn with_max_retries(self, max_retries: usize) -> Self

Set the maximum number of retries

Source

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);
Source

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());
Source

pub fn with_retry_if<F>(self, predicate: F) -> Self
where F: Fn(&E) -> bool + Send + Sync + 'static,

Set a predicate to determine if an error should be retried

Source

pub fn retry<F, T>(&self, operation: F) -> Result<T, E>
where F: FnMut() -> Result<T, E>,

Execute a fallible operation with retries

Source

pub fn retry_with_handler<F, H, T>( &self, operation: F, on_error: H, ) -> Result<T, E>
where F: FnMut() -> Result<T, E>, H: FnMut(&E, usize, Duration),

Execute a fallible operation with retries using a custom error handler

Auto Trait Implementations§

§

impl<E> !RefUnwindSafe for RetryExecutor<E>

§

impl<E> !UnwindSafe for RetryExecutor<E>

§

impl<E> Forget for RetryExecutor<E>

§

impl<E> Freeze for RetryExecutor<E>
where Option<Box<dyn Fn(&E) -> bool + Send + Sync>>: Freeze, PhantomData<E>: Freeze,

§

impl<E> Move for RetryExecutor<E>

§

impl<E> Send for RetryExecutor<E>
where Option<Box<dyn Fn(&E) -> bool + Send + Sync>>: Send, PhantomData<E>: Send,

§

impl<E> Sync for RetryExecutor<E>
where Option<Box<dyn Fn(&E) -> bool + Send + Sync>>: Sync, PhantomData<E>: Sync,

§

impl<E> Unpin for RetryExecutor<E>
where Option<Box<dyn Fn(&E) -> bool + Send + Sync>>: Unpin, PhantomData<E>: Unpin,

§

impl<E> UnsafeUnpin for RetryExecutor<E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<E> WithErrorCode<E> for E

Source§

fn with_code(self, code: impl Into<String>) -> CodedError<E>

Attach an error code to an error
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more