Skip to main content

RetryExecutor

Struct RetryExecutor 

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

Retry executor bound to an error type.

The generic parameter E is the caller’s application error type. The success type is chosen per call to RetryExecutor::run, RetryExecutor::run_async, or RetryExecutor::run_async_with_timeout. Cloning an executor shares the retry decider and listeners through reference counting.

Implementations§

Source§

impl<E> RetryExecutor<E>

Source

pub fn builder() -> RetryExecutorBuilder<E>

Creates a retry executor builder.

§Parameters

This function has no parameters.

§Returns

A RetryExecutorBuilder configured with default options and the default retry-all decider.

§Errors

This function does not return errors.

Source

pub fn from_options(options: RetryOptions) -> Result<Self, RetryConfigError>

Creates an executor from options with the default decider.

§Parameters
  • options: Validated retry options to use for the executor.
§Returns

A RetryExecutor that retries all application errors unless limits stop execution.

§Errors

Returns RetryConfigError if options fails validation.

Source

pub fn options(&self) -> &RetryOptions

Returns the immutable option snapshot used by this executor.

§Parameters

This method has no parameters.

§Returns

A shared reference to the executor’s retry options.

§Errors

This method does not return errors.

Source

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

Runs a synchronous operation with retry.

Loops under global elapsed-time and per-executor attempt limits. Before each try, exits with an error if the wall-clock budget is already spent (without calling operation again). Success emits listener events and returns Ok(T). Failure goes through handle_failure: either std::thread::sleep then retry, or return a terminal RetryError.

§Parameters
  • operation: Synchronous operation to execute. It is called once per attempt until it returns Ok, the decider aborts, or retry limits are exhausted.
§Returns

Ok(T) with the operation result, or RetryError preserving the last application error or timeout metadata.

§Errors

Returns RetryError::Aborted when the decider aborts, RetryError::AttemptsExceeded when the attempt limit is reached, or RetryError::MaxElapsedExceeded when the total elapsed-time budget is exhausted.

§Panics

Propagates any panic raised by operation or by registered listeners.

§Blocking

This method blocks the current thread with std::thread::sleep between retry attempts when the computed delay is non-zero.

Source

pub async fn run_async<T, F, Fut>( &self, operation: F, ) -> Result<T, RetryError<E>>
where F: FnMut() -> Fut, Fut: Future<Output = Result<T, E>>,

Runs an asynchronous operation with retry.

Same loop structure as Self::run: checks the global elapsed budget before each attempt, increments the attempt counter, then runs operation().await. On failure, handle_failure chooses a backoff sleep (async) or a terminal RetryError; timing uses Tokio’s timer instead of blocking std::thread::sleep.

§Parameters
  • operation: Asynchronous operation factory. It is called once per attempt and must return a fresh future each time.
§Returns

Ok(T) with the operation result, or RetryError preserving the last application error.

§Errors

Returns RetryError::Aborted when the decider aborts, RetryError::AttemptsExceeded when the attempt limit is reached, or RetryError::MaxElapsedExceeded when the total elapsed-time budget is exhausted.

§Panics

Propagates panics raised by operation, the returned future, or registered listeners. Tokio may also panic if the future is polled outside a runtime with a time driver and a non-zero sleep is required.

§Async

Uses tokio::time::sleep between attempts when the computed delay is non-zero.

Source

pub async fn run_async_with_timeout<T, F, Fut>( &self, attempt_timeout: Duration, operation: F, ) -> Result<T, RetryError<E>>
where F: FnMut() -> Fut, Fut: Future<Output = Result<T, E>>,

Runs an asynchronous operation with a timeout for each attempt.

Like Self::run_async, but each attempt is bounded by tokio::time::timeout(attempt_timeout, operation()). Normal completion yields the inner Ok/Err. A timeout becomes RetryAttemptFailure::AttemptTimeout and is fed to handle_failure to decide retry versus a terminal error, subject to the same global limits.

§Parameters
  • attempt_timeout: Maximum duration allowed for each individual attempt.
  • operation: Asynchronous operation factory. It is called once per attempt and must return a fresh future each time.
§Returns

Ok(T) with the operation result, or RetryError preserving the last application error or timeout metadata.

§Errors

Returns RetryError::Aborted when the decider aborts an application error, RetryError::AttemptsExceeded when the attempt limit is reached, or RetryError::MaxElapsedExceeded when the total elapsed-time budget is exhausted. Attempt timeouts are represented as RetryAttemptFailure::AttemptTimeout and are considered retryable until limits stop execution.

§Panics

Propagates panics raised by operation, the returned future, or registered listeners. Tokio may also panic if the future is polled outside a runtime with a time driver.

§Async

Uses tokio::time::timeout for each attempt and tokio::time::sleep between retries when the computed delay is non-zero.

Trait Implementations§

Source§

impl<E: Clone> Clone for RetryExecutor<E>

Source§

fn clone(&self) -> RetryExecutor<E>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E> Debug for RetryExecutor<E>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the executor for debug output without exposing callbacks.

§Parameters
  • f: Formatter provided by the standard formatting machinery.
§Returns

fmt::Result from the formatter.

§Errors

Returns a formatting error if the underlying formatter fails.

Auto Trait Implementations§

§

impl<E> Freeze for RetryExecutor<E>

§

impl<E = Box<dyn Error + Sync + Send>> !RefUnwindSafe for RetryExecutor<E>

§

impl<E> Send for RetryExecutor<E>

§

impl<E> Sync for RetryExecutor<E>

§

impl<E> Unpin for RetryExecutor<E>

§

impl<E> UnsafeUnpin for RetryExecutor<E>

§

impl<E = Box<dyn Error + Sync + Send>> !UnwindSafe 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.