pub struct RetryPolicy {
pub max_attempts: u32,
pub strategy: BackoffStrategy,
}Expand description
Retry policy combining a maximum-attempt cap with a BackoffStrategy.
§Integration hints
- reqwest-retry (
reqwest-middleware): implementRetryableStrategyand callRetryPolicy::next_delayfromretry_decision. - tower-retry: implement
tower::retry::Policyand delegate toRetryPolicy::next_delayinsideretry.
§Examples
use api_bones::retry::RetryPolicy;
use core::time::Duration;
// Fixed: always wait 500 ms, up to 5 attempts.
let fixed = RetryPolicy::fixed(5, Duration::from_millis(500));
assert_eq!(fixed.next_delay(0), Duration::from_millis(500));
assert_eq!(fixed.next_delay(4), Duration::from_millis(500));
// Exponential: 100 ms → 200 ms → 400 ms … capped at 2 s.
let exp = RetryPolicy::exponential(4, Duration::from_millis(100));
assert_eq!(exp.next_delay(0), Duration::from_millis(100));
assert_eq!(exp.next_delay(1), Duration::from_millis(200));
assert_eq!(exp.next_delay(2), Duration::from_millis(400));Fields§
§max_attempts: u32Maximum number of retry attempts (excluding the original request).
strategy: BackoffStrategyBackoff strategy used to compute delays between attempts.
Implementations§
Source§impl RetryPolicy
impl RetryPolicy
Sourcepub fn fixed(max_attempts: u32, base: Duration) -> Self
pub fn fixed(max_attempts: u32, base: Duration) -> Self
Create a policy with a BackoffStrategy::Fixed delay.
§Examples
use api_bones::retry::RetryPolicy;
use core::time::Duration;
let p = RetryPolicy::fixed(3, Duration::from_secs(1));
assert_eq!(p.max_attempts, 3);
assert_eq!(p.next_delay(99), Duration::from_secs(1));Sourcepub fn exponential(max_attempts: u32, base: Duration) -> Self
pub fn exponential(max_attempts: u32, base: Duration) -> Self
Create a policy with a BackoffStrategy::Exponential delay
capped at base * 2^10 (≈ 1024× the base).
§Examples
use api_bones::retry::RetryPolicy;
use core::time::Duration;
let p = RetryPolicy::exponential(5, Duration::from_millis(50));
assert_eq!(p.next_delay(0), Duration::from_millis(50));
assert_eq!(p.next_delay(1), Duration::from_millis(100));Create a policy with a BackoffStrategy::DecorrelatedJitter delay.
§Examples
use api_bones::retry::RetryPolicy;
use core::time::Duration;
let p = RetryPolicy::decorrelated_jitter(4, Duration::from_millis(100));
let d = p.next_delay(1);
assert!(d >= Duration::from_millis(100));Sourcepub fn next_delay(&self, attempt: u32) -> Duration
pub fn next_delay(&self, attempt: u32) -> Duration
Compute the delay to wait before attempt number attempt.
attempt is 0-indexed: pass 0 before the first retry,
1 before the second, and so on.
For BackoffStrategy::DecorrelatedJitter this function returns the
midpoint of [base, min(base * 3^(attempt+1), max_delay)] as a
deterministic approximation. Callers wanting true randomness should
use the returned value as an upper bound and sample uniformly in
[base, returned_delay].
§Examples
use api_bones::retry::RetryPolicy;
use core::time::Duration;
let policy = RetryPolicy::exponential(5, Duration::from_millis(100));
assert_eq!(policy.next_delay(0), Duration::from_millis(100));
assert_eq!(policy.next_delay(1), Duration::from_millis(200));
assert_eq!(policy.next_delay(2), Duration::from_millis(400));
// Capped at base * 1024 = 102_400 ms
assert_eq!(policy.next_delay(20), Duration::from_millis(102_400));Trait Implementations§
Source§impl Clone for RetryPolicy
impl Clone for RetryPolicy
Source§fn clone(&self) -> RetryPolicy
fn clone(&self) -> RetryPolicy
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more