Skip to main content

systemprompt_sync/api_client/
retry.rs

1//! Retry-policy configuration for [`crate::api_client::SyncApiClient`]:
2//! exponential backoff with a configurable cap.
3//!
4//! Copyright (c) systemprompt.io — Business Source License 1.1.
5//! See <https://systemprompt.io> for licensing details.
6
7use std::time::Duration;
8
9#[derive(Debug, Clone, Copy)]
10pub struct RetryConfig {
11    pub max_attempts: u32,
12    pub initial_delay: Duration,
13    pub max_delay: Duration,
14    pub exponential_base: u32,
15}
16
17impl Default for RetryConfig {
18    fn default() -> Self {
19        Self {
20            max_attempts: 5,
21            initial_delay: Duration::from_secs(2),
22            max_delay: Duration::from_secs(30),
23            exponential_base: 2,
24        }
25    }
26}
27
28impl RetryConfig {
29    pub fn next_delay(&self, current: Duration) -> Duration {
30        current
31            .saturating_mul(self.exponential_base)
32            .min(self.max_delay)
33    }
34}