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