ig_client/model/
retry.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 20/10/25
5******************************************************************************/
6use crate::utils::config::get_env_or_none;
7
8/// Configuration for HTTP request retry behavior
9#[derive(Debug, Clone)]
10pub struct RetryConfig {
11    /// Maximum number of retries on rate limit (None = infinite retries)
12    pub max_retry_count: Option<u32>,
13    /// Delay in seconds between retries (None = use default 10 seconds)
14    pub retry_delay_secs: Option<u64>,
15}
16
17impl RetryConfig {
18    /// Creates a new retry configuration with infinite retries and 10 second delay
19    #[must_use]
20    pub fn new() -> Self {
21        Self::default()
22    }
23
24    /// Creates a new retry configuration with infinite retries and 10 second delay
25    #[must_use]
26    pub fn infinite() -> Self {
27        Self {
28            max_retry_count: None,  // infinite retries
29            retry_delay_secs: None, // use default 10 seconds
30        }
31    }
32
33    /// Creates a new retry configuration with a maximum number of retries
34    #[must_use]
35    pub fn with_max_retries(max_retries: u32) -> Self {
36        Self {
37            max_retry_count: Some(max_retries),
38            retry_delay_secs: None, // use default 10 seconds
39        }
40    }
41
42    /// Creates a new retry configuration with custom delay
43    #[must_use]
44    pub fn with_delay(delay_secs: u64) -> Self {
45        Self {
46            max_retry_count: None, // infinite retries
47            retry_delay_secs: Some(delay_secs),
48        }
49    }
50
51    /// Creates a new retry configuration with both max retries and custom delay
52    #[must_use]
53    pub fn with_max_retries_and_delay(max_retries: u32, delay_secs: u64) -> Self {
54        Self {
55            max_retry_count: Some(max_retries),
56            retry_delay_secs: Some(delay_secs),
57        }
58    }
59
60    /// Gets the maximum retry count (0 = infinite)
61    #[must_use]
62    pub fn max_retries(&self) -> u32 {
63        self.max_retry_count.unwrap_or(0)
64    }
65
66    /// Gets the retry delay in seconds (default: 10)
67    #[must_use]
68    pub fn delay_secs(&self) -> u64 {
69        self.retry_delay_secs.unwrap_or(10)
70    }
71}
72
73impl Default for RetryConfig {
74    fn default() -> Self {
75        let max_retry_count: Option<u32> = get_env_or_none("MAX_RETRY_COUNT");
76        let retry_delay_secs: Option<u64> = get_env_or_none("RETRY_DELAY_SECS");
77
78        Self {
79            max_retry_count,
80            retry_delay_secs,
81        }
82    }
83}