Skip to main content

rightkit_sync/
backoff.rs

1/// Deterministic exponential backoff merged with server hints.
2#[derive(Debug, Clone, PartialEq)]
3pub struct BackoffPolicy {
4    pub base_secs: f64,
5    pub max_secs: f64,
6    /// Minimum delay after a pass that finished with pending work
7    /// (iOS: 30s).
8    pub pending_floor_secs: f64,
9}
10
11impl Default for BackoffPolicy {
12    fn default() -> Self {
13        Self {
14            base_secs: 15.0,
15            max_secs: 900.0,
16            pending_floor_secs: 30.0,
17        }
18    }
19}
20
21impl BackoffPolicy {
22    /// Delay after the `failures`-th consecutive failure (>= 1): never less
23    /// than the server hint, capped at `max_secs` unless the hint is larger.
24    pub fn delay_secs(&self, failures: u32, hint: Option<f64>) -> f64 {
25        let exp = self.base_secs * 2f64.powi(failures.saturating_sub(1).min(20) as i32);
26        let policy = exp.min(self.max_secs);
27        match hint {
28            Some(h) if h.is_finite() && h > policy => h,
29            _ => policy,
30        }
31    }
32}