Skip to main content

causa_runtime/retry/
policy.rs

1//! Retry policy for the optional gateway wrapper.
2
3use std::time::Duration;
4
5/// Bounded retry policy for [`RetryingGateway`](super::RetryingGateway).
6///
7/// There is no `Default` and no recommended attempt count: retry cost is
8/// host policy, so the host states every value. The durations a fresh policy
9/// starts from are the ones the compared references use — a 500 ms base
10/// delay, an 8 s backoff cap and a 60 s server-delay ceiling (`pi-ai`).
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12#[non_exhaustive]
13pub struct RetryPolicy {
14    /// Total calls including the first; at least 1.
15    pub max_attempts: u32,
16    /// Backoff before the second attempt; it doubles per failed attempt.
17    pub base_delay: Duration,
18    /// Ceiling on the wrapper's own backoff. A server-requested delay is not
19    /// capped by this, only by [`RetryPolicy::max_server_delay`].
20    pub max_delay: Duration,
21    /// Ceiling on a server-requested delay. A longer request fails the call
22    /// at once instead of waiting.
23    pub max_server_delay: Duration,
24}
25
26impl RetryPolicy {
27    /// A policy allowing `max_attempts` calls in total — the first included,
28    /// so `new(1)` never retries — starting from a 500 ms base delay, an 8 s
29    /// backoff cap and a 60 s server-delay ceiling. Values below 1 are raised
30    /// to 1.
31    pub fn new(max_attempts: u32) -> Self {
32        Self {
33            max_attempts: max_attempts.max(1),
34            base_delay: Duration::from_millis(500),
35            max_delay: Duration::from_secs(8),
36            max_server_delay: Duration::from_secs(60),
37        }
38    }
39
40    /// Sets [`RetryPolicy::base_delay`].
41    pub fn with_base_delay(mut self, delay: Duration) -> Self {
42        self.base_delay = delay;
43        self
44    }
45
46    /// Sets [`RetryPolicy::max_delay`].
47    pub fn with_max_delay(mut self, delay: Duration) -> Self {
48        self.max_delay = delay;
49        self
50    }
51
52    /// Sets [`RetryPolicy::max_server_delay`].
53    pub fn with_max_server_delay(mut self, delay: Duration) -> Self {
54        self.max_server_delay = delay;
55        self
56    }
57
58    /// The wrapper's own backoff after the `attempt`-th failure (`attempt`
59    /// from 1): `min(base_delay * 2^(attempt - 1), max_delay)` shortened by a
60    /// uniform random factor in `[0.75, 1.0]`. Jitter is fixed, not
61    /// configurable, and never applies to a server-requested delay.
62    pub(super) fn backoff(&self, attempt: u32) -> Duration {
63        let doubling = 1u32 << attempt.saturating_sub(1).min(31);
64        let capped = self.base_delay.saturating_mul(doubling).min(self.max_delay);
65        jittered(capped, 0.75 + 0.25 * fastrand::f64())
66    }
67}
68
69/// Shorten `delay` by `factor` without a panic on extreme host-set caps.
70fn jittered(delay: Duration, factor: f64) -> Duration {
71    Duration::from_nanos(((delay.as_nanos() as f64) * factor) as u64)
72}