Skip to main content

harmos_protocol/
service.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10// Resident Service Stop Deadlines and Restart Credit
11
12/* === Modules === */
13
14/* === Re-Exports === */
15
16/* === Imports === */
17
18// Standard Library Imports
19use std::time::Duration;
20
21// Third Party Imports
22use serde::{Deserialize, Serialize};
23
24// Local Imports
25use crate::policy::{Backoff, PolicyError};
26
27/* === Constants === */
28
29/* === Macros === */
30
31/* === Type Aliases === */
32
33/* === Structs === */
34
35#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
36#[serde(deny_unknown_fields)]
37pub struct ServicePolicy {
38    pub stop_timeout: Duration,
39    pub restart: RestartPolicy,
40}
41
42/* === Enums === */
43
44// Failed Residents Spend Restart Credit; Sustained Health Replenishes It.
45#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
46#[serde(rename_all = "snake_case", deny_unknown_fields)]
47pub enum RestartPolicy {
48    Never,
49    OnFailure { credit: usize, sustained_health: Duration, backoff: Backoff },
50}
51
52/* === Traits === */
53
54/* === Implementations === */
55
56impl ServicePolicy {
57    pub fn validate(self) -> Result<(), PolicyError> {
58        if self.stop_timeout.is_zero() {
59            return Err(PolicyError::Deadline {
60                timeout: self.stop_timeout,
61                attempt_timeout: self.stop_timeout,
62            });
63        }
64        self.restart.validate()
65    }
66}
67
68impl RestartPolicy {
69    pub fn validate(self) -> Result<(), PolicyError> {
70        match self {
71            Self::Never => Ok(()),
72            Self::OnFailure { credit, sustained_health, .. } if credit == 0 || sustained_health.is_zero() => {
73                Err(PolicyError::EmptyRestarts)
74            }
75            Self::OnFailure { backoff, .. } => backoff.validate(),
76        }
77    }
78
79    pub const fn credit(self) -> usize {
80        match self {
81            Self::Never => 0,
82            Self::OnFailure { credit, .. } => credit,
83        }
84    }
85}
86
87/* === Public Functions === */
88
89/* === Private Functions === */
90
91/* === Tests === */
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96    use crate::policy::{Backoff, Jitter};
97
98    #[test]
99    fn service_credit_and_backoff() {
100        let delay = Duration::from_millis(10);
101        let backoff = Backoff::Exponential {
102            initial: delay,
103            maximum: delay * 4,
104            jitter: Jitter::None,
105        };
106        let restart = RestartPolicy::OnFailure {
107            credit: 2,
108            sustained_health: Duration::from_secs(1),
109            backoff,
110        };
111        assert_eq!(restart.credit(), 2);
112        assert!(ServicePolicy { stop_timeout: delay, restart }.validate().is_ok());
113        assert!(ServicePolicy { stop_timeout: Duration::ZERO, restart }.validate().is_err());
114        assert!(
115            RestartPolicy::OnFailure {
116                credit: 0,
117                sustained_health: delay,
118                backoff
119            }
120            .validate()
121            .is_err()
122        );
123        assert_eq!(backoff.delay(0, 0).unwrap(), delay);
124        assert_eq!(backoff.delay(usize::MAX, 0).unwrap(), delay * 4);
125    }
126}