yfinance_rs/core/client/retry.rs
1/// Specifies the backoff strategy for retrying failed requests.
2#[derive(Clone, Debug)]
3pub enum Backoff {
4 /// Uses a fixed delay between retries.
5 Fixed(std::time::Duration),
6 /// Uses an exponential delay between retries.
7 /// The delay is calculated as `base * (factor ^ attempt)`.
8 Exponential {
9 /// The initial backoff duration.
10 base: std::time::Duration,
11 /// The multiplicative factor for each subsequent retry.
12 factor: f64,
13 /// The maximum duration to wait between retries.
14 max: std::time::Duration,
15 /// Whether to apply random jitter (+/- 50%) to the delay.
16 jitter: bool,
17 },
18}
19
20/// Configuration for the automatic retry mechanism.
21#[derive(Clone, Debug)]
22pub struct RetryConfig {
23 /// Enables or disables the retry mechanism.
24 pub enabled: bool,
25 /// The maximum number of retries to attempt. The total number of attempts will be `max_retries + 1`.
26 pub max_retries: u32,
27 /// The backoff strategy to use between retries.
28 pub backoff: Backoff,
29 /// A list of HTTP status codes that should trigger a retry.
30 pub retry_on_status: Vec<u16>,
31 /// Whether to retry on request timeouts.
32 pub retry_on_timeout: bool,
33 /// Whether to retry on connection errors.
34 pub retry_on_connect: bool,
35}
36
37impl Default for RetryConfig {
38 fn default() -> Self {
39 Self {
40 enabled: true,
41 max_retries: 4,
42 backoff: Backoff::Exponential {
43 base: std::time::Duration::from_millis(200),
44 factor: 2.0,
45 max: std::time::Duration::from_secs(3),
46 jitter: true,
47 },
48 retry_on_status: vec![408, 429, 500, 502, 503, 504],
49 retry_on_timeout: true,
50 retry_on_connect: true,
51 }
52 }
53}
54
55/// Defines the behavior of the in-memory cache for an API call.
56#[derive(Clone, Copy, Debug, PartialEq, Eq)]
57pub enum CacheMode {
58 /// Read from the cache if a non-expired entry is present; otherwise, fetch from the network
59 /// and write the response to the cache. (Default)
60 Use,
61 /// Always fetch from the network, bypassing any cached entry, and write the new response to the cache.
62 Refresh,
63 /// Always fetch from the network and do not read from or write to the cache.
64 Bypass,
65}