appcore_supervisor/
policy.rs1use crate::{SupervisorError, SupervisorResult};
14use std::time::Duration;
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum RestartMode {
19 Never,
21 OnFailure,
23 Always,
25}
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
29pub struct RestartPolicy {
30 pub mode: RestartMode,
32 pub restart_budget: u32,
34 pub restart_window: Duration,
36 pub backoff: Duration,
38 pub jitter: Duration,
40 pub shutdown_timeout: Duration,
42}
43
44impl RestartPolicy {
45 pub fn never() -> Self {
47 Self {
48 mode: RestartMode::Never,
49 restart_budget: 0,
50 restart_window: Duration::from_secs(600),
51 backoff: Duration::ZERO,
52 jitter: Duration::ZERO,
53 shutdown_timeout: Duration::from_secs(10),
54 }
55 }
56
57 pub fn bounded(restart_budget: u32, restart_window: Duration) -> SupervisorResult<Self> {
59 let policy = Self {
60 mode: RestartMode::OnFailure,
61 restart_budget,
62 restart_window,
63 backoff: Duration::from_millis(100),
64 jitter: Duration::from_millis(50),
65 shutdown_timeout: Duration::from_secs(10),
66 };
67 policy.validate()?;
68 Ok(policy)
69 }
70
71 pub fn with_backoff(mut self, backoff: Duration, jitter: Duration) -> Self {
73 self.backoff = backoff;
74 self.jitter = jitter;
75 self
76 }
77
78 pub fn with_shutdown_timeout(mut self, shutdown_timeout: Duration) -> Self {
80 self.shutdown_timeout = shutdown_timeout;
81 self
82 }
83
84 pub fn validate(&self) -> SupervisorResult<()> {
86 if self.mode != RestartMode::Never
87 && (self.restart_budget == 0 || self.restart_window.is_zero())
88 {
89 return Err(SupervisorError::InvalidConfiguration(
90 "automatic restart requires a non-zero budget and window".to_string(),
91 ));
92 }
93 if self.shutdown_timeout.is_zero() {
94 return Err(SupervisorError::InvalidConfiguration(
95 "shutdown_timeout must be greater than zero".to_string(),
96 ));
97 }
98 Ok(())
99 }
100}
101
102impl Default for RestartPolicy {
103 fn default() -> Self {
104 Self {
105 mode: RestartMode::OnFailure,
106 restart_budget: 5,
107 restart_window: Duration::from_secs(600),
108 backoff: Duration::from_millis(100),
109 jitter: Duration::from_millis(50),
110 shutdown_timeout: Duration::from_secs(10),
111 }
112 }
113}