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
27    ///
28    /// Uses `RETRY_DELAY_SECS` environment variable if set, otherwise defaults to 10 seconds.
29    #[must_use]
30    pub fn infinite() -> Self {
31        Self {
32            max_retry_count: None, // infinite retries
33            retry_delay_secs: get_env_or_none("RETRY_DELAY_SECS"),
34        }
35    }
36
37    /// Creates a new retry configuration with a maximum number of retries
38    ///
39    /// Uses `RETRY_DELAY_SECS` environment variable if set, otherwise defaults to 10 seconds.
40    #[must_use]
41    pub fn with_max_retries(max_retries: u32) -> Self {
42        Self {
43            max_retry_count: Some(max_retries),
44            retry_delay_secs: get_env_or_none("RETRY_DELAY_SECS"),
45        }
46    }
47
48    /// Creates a new retry configuration with custom delay
49    #[must_use]
50    pub fn with_delay(delay_secs: u64) -> Self {
51        Self {
52            max_retry_count: None, // infinite retries
53            retry_delay_secs: Some(delay_secs),
54        }
55    }
56
57    /// Creates a new retry configuration with both max retries and custom delay
58    #[must_use]
59    pub fn with_max_retries_and_delay(max_retries: u32, delay_secs: u64) -> Self {
60        Self {
61            max_retry_count: Some(max_retries),
62            retry_delay_secs: Some(delay_secs),
63        }
64    }
65
66    /// Gets the maximum retry count (0 = infinite)
67    #[must_use]
68    pub fn max_retries(&self) -> u32 {
69        self.max_retry_count.unwrap_or(0)
70    }
71
72    /// Gets the retry delay in seconds (default: 10)
73    #[must_use]
74    pub fn delay_secs(&self) -> u64 {
75        self.retry_delay_secs.unwrap_or(10)
76    }
77}
78
79impl Default for RetryConfig {
80    fn default() -> Self {
81        let max_retry_count: Option<u32> = get_env_or_none("MAX_RETRY_COUNT");
82        let retry_delay_secs: Option<u64> = get_env_or_none("RETRY_DELAY_SECS");
83
84        Self {
85            max_retry_count,
86            retry_delay_secs,
87        }
88    }
89}