tako_rs_core/queue/
retry.rs1use std::time::Duration;
4
5#[derive(Debug, Clone, Default)]
7pub enum RetryPolicy {
8 #[default]
10 None,
11 Fixed {
13 max_retries: u32,
15 delay: Duration,
17 },
18 Exponential {
20 max_retries: u32,
22 base_delay: Duration,
24 },
25}
26
27impl RetryPolicy {
28 pub fn fixed(max_retries: u32, delay: Duration) -> Self {
30 Self::Fixed { max_retries, delay }
31 }
32
33 pub fn exponential(max_retries: u32, base_delay: Duration) -> Self {
35 Self::Exponential {
36 max_retries,
37 base_delay,
38 }
39 }
40
41 pub(crate) fn max_retries(&self) -> u32 {
42 match self {
43 Self::None => 0,
44 Self::Fixed { max_retries, .. } | Self::Exponential { max_retries, .. } => *max_retries,
45 }
46 }
47
48 pub(crate) fn delay_for_attempt(&self, attempt: u32) -> Duration {
49 match self {
50 Self::None => Duration::ZERO,
51 Self::Fixed { delay, .. } => *delay,
52 Self::Exponential { base_delay, .. } => {
53 base_delay
58 .checked_mul(2u32.saturating_pow(attempt))
59 .unwrap_or(Duration::from_secs(86_400))
60 }
61 }
62 }
63}