#[non_exhaustive]pub struct RetryPolicy {
pub max_retries: u32,
pub backoff_initial: Duration,
pub backoff_max: Duration,
pub backoff_jitter: f64,
pub timeout: Duration,
pub respect_retry_after: bool,
pub max_retry_after: Duration,
pub retry_connection_errors: bool,
pub retry_timeouts: bool,
pub retry_statuses: Vec<u16>,
}Expand description
How failed requests are retried.
The defaults match the official TypeSafe SDKs: 2 retries, a 0.5 s first delay doubling up to
5 s, a quarter of each delay randomly subtracted, a 30 s timeout per attempt, and a server’s
Retry-After honoured up to 60 s. Retried by default are 408, 429, every 5xx (so 529 too),
connection failures and timeouts.
use std::time::Duration;
use jev_client::RetryPolicy;
let patient = RetryPolicy::default().with_max_retries(5).with_timeout(Duration::from_secs(60));
assert_eq!(patient.max_retries, 5);
let never = RetryPolicy::none();
assert_eq!(never.max_retries, 0);Fields (Non-exhaustive)§
This struct is marked as non-exhaustive
Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.max_retries: u32Retries after the first attempt. 0 disables retrying.
backoff_initial: DurationThe delay before the first retry. Each later delay doubles, up to backoff_max.
backoff_max: DurationThe longest backoff delay.
backoff_jitter: f64The fraction of each backoff delay that is randomly subtracted, from 0 to 1. Spreading retries out stops many clients from retrying in step.
timeout: DurationThe time allowed for one attempt, from sending the request to reading the whole response.
respect_retry_after: boolWhether a Retry-After or retry-after-ms response header sets the delay.
max_retry_after: DurationThe longest server-requested delay that is honoured. A longer one falls back to backoff.
retry_connection_errors: boolWhether a failure to connect, or a connection lost mid-response, is retried.
retry_timeouts: boolWhether an attempt that timed out is retried.
retry_statuses: Vec<u16>The HTTP statuses that are retried. 400, 401, 403, 404 and 422 are never retried, even when listed here.
Implementations§
Source§impl RetryPolicy
impl RetryPolicy
Sourcepub fn none() -> Self
pub fn none() -> Self
The default policy with retrying switched off. The per-attempt timeout still applies.
Sourcepub const fn with_max_retries(self, max_retries: u32) -> Self
pub const fn with_max_retries(self, max_retries: u32) -> Self
Sets the number of retries after the first attempt.
Sourcepub const fn with_backoff(self, initial: Duration, max: Duration) -> Self
pub const fn with_backoff(self, initial: Duration, max: Duration) -> Self
Sets the first backoff delay and the longest one.
Sourcepub const fn with_jitter(self, fraction: f64) -> Self
pub const fn with_jitter(self, fraction: f64) -> Self
Sets the fraction of each backoff delay that is randomly subtracted. Values outside 0 to 1, and NaN, are clamped into range when a delay is computed.
Sourcepub const fn with_timeout(self, timeout: Duration) -> Self
pub const fn with_timeout(self, timeout: Duration) -> Self
Sets the time allowed for one attempt.
Sourcepub fn retries_status(&self, status: u16) -> bool
pub fn retries_status(&self, status: u16) -> bool
Returns true when a response with status should be retried under this policy.
Sourcepub fn backoff_delay(&self, retry: u32, random: f64) -> Duration
pub fn backoff_delay(&self, retry: u32, random: f64) -> Duration
The backoff delay before retry number retry (0 for the first retry).
random is a sample from 0 to 1 that decides how much of the jitter is applied; passing it
in keeps this function pure.