Skip to main content

vlo_types/
retry.rs

1//! Retry policy and the plan the kernel derives from it.
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6/// How the delay grows between attempts.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
8#[serde(rename_all = "snake_case")]
9pub enum Backoff {
10    /// Every retry waits `initial_ms`.
11    Fixed,
12    /// The delay is `initial_ms * factor^(attempt - 1)`, capped at `max_ms`.
13    Exponential,
14}
15
16/// Randomization applied to the computed delay.
17#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
18#[serde(rename_all = "snake_case")]
19pub enum Jitter {
20    /// Use the computed delay as-is.
21    None,
22    /// Full jitter: scale the delay by a uniform fraction taken from `random`, so the
23    /// result lies in `[0, delay]`.
24    Full,
25}
26
27/// Declared per activity in the schema registry (see `docs/workflows.md`). Integers only
28/// so the policy can live inside the hashed `KernelState`.
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
30pub struct RetryPolicy {
31    /// Total attempts including the first. `1` means never retry.
32    pub max_attempts: u32,
33    /// Backoff shape.
34    pub backoff: Backoff,
35    /// Base delay in milliseconds. Default `1_000`.
36    #[serde(default = "default_initial_ms")]
37    pub initial_ms: u64,
38    /// Delay cap in milliseconds. Default `600_000`.
39    #[serde(default = "default_max_ms")]
40    pub max_ms: u64,
41    /// Multiplier for exponential backoff, e.g. `2`.
42    #[serde(default = "default_factor")]
43    pub factor: u32,
44    /// Jitter mode. Default `full`.
45    #[serde(default = "default_jitter")]
46    pub jitter: Jitter,
47}
48
49fn default_initial_ms() -> u64 {
50    1_000
51}
52fn default_max_ms() -> u64 {
53    600_000
54}
55fn default_factor() -> u32 {
56    2
57}
58fn default_jitter() -> Jitter {
59    Jitter::Full
60}
61
62impl Default for RetryPolicy {
63    fn default() -> Self {
64        RetryPolicy {
65            max_attempts: 3,
66            backoff: Backoff::Exponential,
67            initial_ms: default_initial_ms(),
68            max_ms: default_max_ms(),
69            factor: default_factor(),
70            jitter: default_jitter(),
71        }
72    }
73}
74
75/// Input to `plan_retry`.
76#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
77pub struct PlanRetryInput {
78    /// Policy of the step that failed.
79    pub policy: RetryPolicy,
80    /// The attempt that just failed (1-based).
81    pub attempt: u32,
82    /// Why it failed. Non-retryable causes always yield `retry: false`.
83    pub cause: crate::Cause,
84    /// Journaled randomness (32 hex chars); the only entropy source for jitter.
85    pub random: String,
86}
87
88/// `retry: false` means the step fails for good; `next_attempt` / `delay_ms` are set iff `retry`.
89#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, JsonSchema)]
90pub struct RetryPlan {
91    /// Whether another attempt is scheduled.
92    pub retry: bool,
93    /// The attempt to schedule (`attempt + 1`).
94    #[serde(default, skip_serializing_if = "Option::is_none")]
95    pub next_attempt: Option<u32>,
96    /// Delay before that attempt, in milliseconds.
97    #[serde(default, skip_serializing_if = "Option::is_none")]
98    pub delay_ms: Option<u64>,
99}
100
101impl RetryPlan {
102    /// The plan that gives up.
103    pub fn stop() -> Self {
104        RetryPlan {
105            retry: false,
106            next_attempt: None,
107            delay_ms: None,
108        }
109    }
110    /// The plan that schedules `next_attempt` after `delay_ms`.
111    pub fn again(next_attempt: u32, delay_ms: u64) -> Self {
112        RetryPlan {
113            retry: true,
114            next_attempt: Some(next_attempt),
115            delay_ms: Some(delay_ms),
116        }
117    }
118}