Skip to main content

made_core/value_objects/ceremony/
retry_policy.rs

1use serde::{Deserialize, Serialize};
2
3use crate::value_objects::DurationMs;
4
5use super::StepAttempt;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
8pub struct RetryPolicy {
9    max_attempts: StepAttempt,
10    backoff: DurationMs,
11}
12
13impl RetryPolicy {
14    #[must_use]
15    pub fn new(max_attempts: StepAttempt, backoff: DurationMs) -> Self {
16        Self {
17            max_attempts,
18            backoff,
19        }
20    }
21
22    #[must_use]
23    pub fn single_attempt() -> Self {
24        Self::new(StepAttempt::FIRST, DurationMs::ZERO)
25    }
26
27    #[must_use]
28    pub fn max_attempts(self) -> StepAttempt {
29        self.max_attempts
30    }
31
32    #[must_use]
33    pub fn backoff(self) -> DurationMs {
34        self.backoff
35    }
36
37    #[must_use]
38    pub fn allows_attempt(self, attempt: StepAttempt) -> bool {
39        attempt <= self.max_attempts
40    }
41}
42
43impl Default for RetryPolicy {
44    fn default() -> Self {
45        Self::single_attempt()
46    }
47}