Skip to main content

RetryPolicy

Struct RetryPolicy 

Source
#[non_exhaustive]
pub struct RetryPolicy { pub max_retries: u32, pub initial_backoff: Duration, pub multiplier: f64, pub max_backoff: Duration, pub jitter: bool, }
Expand description

How a retryable run is re-attempted: a bounded number of retries with exponential backoff, a per-delay cap, and optional full jitter.

Used two ways:

  • Client-wide via CliClient::default_retry — every verb of the client retries on a shared policy + classifier, instead of repeating a per-command Command::retry (which is fixed-backoff and per-call).
  • As the schedule behind Command::retry, whose (max_attempts, backoff) form is the fixed-backoff special case (multiplier 1.0, no growth, no jitter).

The n-th retry (0-based) waits min(initial_backoff × multiplier^n, max_backoff); with jitter the actual wait is a uniform random value in [0, that] (AWS-style full jitter), which decorrelates a fleet of clients all backing off at once. Jitter uses a small per-thread PRNG seeded from system entropy — no extra dependency, and no crypto-grade randomness is needed for backoff.

Distinct from RestartPolicy: a RetryPolicy replays a verb to success (re-running on a classified Error), while a RestartPolicy is the Supervisor’s keep-alive restart schedule for a long-lived process — different subsystems, different intent.

Non-exhaustive: construct via RetryPolicy::new / Default + the builder methods (new knobs can be added without a breaking change). Intentionally not Copy — that would pin every future field to a Copy type (a boxed custom-backoff fn, a list of retryable codes), and shedding Copy later is the breaking change; Clone covers every real need (all reads are by-&).

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§max_retries: u32

Retries after the first attempt — 0 never retries; 3 (the default) means up to 4 total attempts. Counts retries (like Supervisor::max_restarts), not total attempts — in contrast to Command::retry’s max_attempts.

§initial_backoff: Duration

Backoff before the first retry (and the whole delay when multiplier is 1.0). Default 100 ms. Duration::ZERO retries immediately. (The base of Supervisor::backoff, spelled in full here.)

§multiplier: f64

Exponential growth factor per retry. Default 2.0; 1.0 is fixed backoff. Values below 1.0 (or non-finite) are treated as 1.0 (backoff never shrinks). (The factor of Supervisor::backoff.)

§max_backoff: Duration

Upper bound on a single delay, so exponential growth can’t run away. Default 30 s; set Duration::MAX to effectively disable the cap.

§jitter: bool

Apply full jitter — spread the actual wait uniformly over [0, delay], so a retry may fire almost immediately (AWS-style; decorrelates a fleet all backing off at once). This is full jitter, distinct from the multiplicative band the supervisor’s RestartPolicy backoff uses. Default true.

Implementations§

Source§

impl RetryPolicy

Source

pub fn new() -> Self

A policy with the default schedule (3 retries, 100 ms initial, ×2 growth, 30 s cap, jitter on). Tune with the builder methods.

Source

pub fn max_retries(self, retries: u32) -> Self

Set the number of retries after the first attempt (0 disables retrying).

Source

pub fn initial_backoff(self, backoff: Duration) -> Self

Set the backoff before the first retry (the base of the exponential).

Source

pub fn multiplier(self, multiplier: f64) -> Self

Set the exponential growth factor (1.0 = fixed backoff).

Source

pub fn max_backoff(self, max: Duration) -> Self

Cap a single delay at max (after growth, before jitter).

Source

pub fn jitter(self, jitter: bool) -> Self

Toggle full jitter (spread the wait over [0, delay]).

Trait Implementations§

Source§

impl Clone for RetryPolicy

Source§

fn clone(&self) -> RetryPolicy

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 Debug for RetryPolicy

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for RetryPolicy

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl PartialEq for RetryPolicy

Source§

fn eq(&self, other: &RetryPolicy) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for RetryPolicy

Auto Trait Implementations§

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

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Source§

fn type_name(&self) -> &'static str

Source§

impl<T> AnySync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync>

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