pub struct RetryPolicy { /* private fields */ }Expand description
When a Client tries a GET call again.
Retries are off unless a policy is set with
Client::retry. With one, a GET call
(health, faces,
and request with "GET") is tried again
after a connection failure or timeout (Error::Transport), or an
HTTP 429 or 503 answer, up to max_attempts
attempts in all. A POST is never retried: repeating validate,
generate, check or project_face could submit a payment file
twice.
The wait before retry n (from 1) is exponential with jitter: a random
point in the upper half of min(max_delay, base_delay * 2^(n-1)). A
Retry-After header on a 429 or 503, in seconds or as an HTTP
date, is waited exactly instead; one asking for longer than
max_delay ends the retries and returns that
answer, so the client never comes back earlier than the server asked.
The client’s timeout applies to each attempt.
use pain001_sdk::{Client, RetryPolicy};
use std::time::Duration;
let policy = RetryPolicy::new()
.with_max_attempts(4)
.with_base_delay(Duration::from_millis(100));
let client = Client::new("http://127.0.0.1:8000").retry(policy);
assert_eq!(policy.max_delay(), RetryPolicy::DEFAULT_MAX_DELAY);Implementations§
Source§impl RetryPolicy
impl RetryPolicy
Sourcepub const DEFAULT_MAX_ATTEMPTS: u32 = 3
pub const DEFAULT_MAX_ATTEMPTS: u32 = 3
Attempts in all, the first one included, of RetryPolicy::new.
Sourcepub const DEFAULT_BASE_DELAY: Duration
pub const DEFAULT_BASE_DELAY: Duration
The wait before the first retry, before jitter.
Sourcepub const DEFAULT_MAX_DELAY: Duration
pub const DEFAULT_MAX_DELAY: Duration
The longest wait before any retry, and the longest Retry-After
honoured.
Sourcepub fn with_max_attempts(self, attempts: u32) -> Self
pub fn with_max_attempts(self, attempts: u32) -> Self
Sets the number of attempts in all, the first one included. 1
(or 0, read as 1) makes the policy retry nothing.
Sourcepub fn with_base_delay(self, delay: Duration) -> Self
pub fn with_base_delay(self, delay: Duration) -> Self
Sets the wait before the first retry, before jitter; each later wait doubles it, up to the maximum delay.
Sourcepub fn with_max_delay(self, delay: Duration) -> Self
pub fn with_max_delay(self, delay: Duration) -> Self
Sets the longest wait before any retry, which is also the longest
Retry-After the client waits for.
Sourcepub fn max_attempts(&self) -> u32
pub fn max_attempts(&self) -> u32
Attempts in all, the first one included.
Sourcepub fn base_delay(&self) -> Duration
pub fn base_delay(&self) -> Duration
The wait before the first retry, before jitter.