Skip to main content

Retry

Struct Retry 

Source
pub struct Retry { /* private fields */ }
Available on crate feature std only.
Expand description

A retry policy: a Backoff, an attempt ceiling, and whether to honor a server’s Retry-After.

The policy is independent of the limiters — retry any fallible async operation, or wrap a limiter’s acquire call. The error is classified per attempt by a closure you supply, so retry works with any error type; for errors that implement error_forge::ForgeError, the retry_if_retryable helper classifies by the error’s own retryability.

§Examples

use throttle_net::{Backoff, Retry, RetryAction};

let retry = Retry::new(Backoff::default()).max_attempts(4);

let result: Result<u32, &str> = retry
    .run(
        || async { Err("transient") },
        |_err| RetryAction::Retry,
    )
    .await;
assert_eq!(result, Err("transient")); // gave up after 4 attempts

Implementations§

Source§

impl Retry

Source

pub fn new(backoff: Backoff) -> Self

Creates a retry policy with the given backoff, a default of five attempts, and Retry-After honored.

Source

pub fn max_attempts(self, attempts: u32) -> Self

Sets the maximum number of attempts (including the first). A value of 1 disables retrying; 0 is treated as 1.

Source

pub fn respect_retry_after(self, yes: bool) -> Self

Sets whether a RetryAction::RetryAfter delay overrides the computed backoff. On by default.

Source

pub const fn attempts(&self) -> u32

The configured attempt ceiling.

Source

pub const fn backoff(&self) -> &Backoff

The configured backoff policy.

Source§

impl Retry

Source

pub async fn run<F, Fut, T, E, C>( &self, operation: F, classify: C, ) -> Result<T, E>
where F: FnMut() -> Fut, Fut: Future<Output = Result<T, E>>, C: Fn(&E) -> RetryAction,

Available on crate feature runtime only.

Runs operation, retrying on failure per the policy until it succeeds, the classifier says to stop, or the attempt ceiling is reached.

operation is called once per attempt. classify inspects each error and returns a RetryAction: retry with the backoff delay, retry honoring a Retry-After, or give up. The last error is returned when attempts run out or the classifier gives up.

§Examples

Retry on a Retry-After the server sent, parsed with parse_retry_after:

use std::time::Duration;
use throttle_net::{Backoff, Retry, RetryAction};

struct Rejected { retry_after: Option<Duration> }

let retry = Retry::new(Backoff::default()).respect_retry_after(true);
let result: Result<(), &str> = retry
    .run(
        || async { Err::<(), _>(Rejected { retry_after: Some(Duration::from_millis(10)) }) },
        |err: &Rejected| match err.retry_after {
            Some(after) => RetryAction::RetryAfter(after),
            None => RetryAction::Retry,
        },
    )
    .await
    .map(|_| ())
    .map_err(|_| "exhausted");
assert_eq!(result, Err("exhausted"));

Trait Implementations§

Source§

impl Clone for Retry

Source§

fn clone(&self) -> Retry

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for Retry

Source§

impl Debug for Retry

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Retry

§

impl RefUnwindSafe for Retry

§

impl Send for Retry

§

impl Sync for Retry

§

impl Unpin for Retry

§

impl UnsafeUnpin for Retry

§

impl UnwindSafe for Retry

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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.
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