Skip to main content

appcore_contracts/policy/
health.rs

1// =============================================================================
2//        #######
3//     ###       ###     F: health.rs
4//    ##   ## ##   ##    P: AppCore-Runtime
5//         ## ##
6//                       C: 2026/07/22 15:41:18 by dnettoRaw
7//    ##   ## ##   ##    U: 2026/07/22 15:41:18 by dnettoRaw
8//      ###########      S: 1.0.1-rc.8
9// =============================================================================
10
11use super::*;
12
13/// Health timing required by an application.
14#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
15pub struct HealthRequirements {
16    startup_grace_ms: u64,
17    heartbeat_interval_ms: u64,
18    failure_threshold: u32,
19}
20
21impl HealthRequirements {
22    /// Creates health requirements.
23    pub fn new(
24        startup_grace_ms: u64,
25        heartbeat_interval_ms: u64,
26        failure_threshold: u32,
27    ) -> ContractResult<Self> {
28        if heartbeat_interval_ms == 0 || failure_threshold == 0 {
29            return Err(ContractError::InvalidValue {
30                field: "health",
31                reason: "heartbeat interval and failure threshold must be greater than zero",
32            });
33        }
34        Ok(Self {
35            startup_grace_ms,
36            heartbeat_interval_ms,
37            failure_threshold,
38        })
39    }
40
41    /// Returns startup grace time in milliseconds.
42    pub fn startup_grace_ms(&self) -> u64 {
43        self.startup_grace_ms
44    }
45
46    /// Returns heartbeat interval in milliseconds.
47    pub fn heartbeat_interval_ms(&self) -> u64 {
48        self.heartbeat_interval_ms
49    }
50
51    /// Returns consecutive failures tolerated before unhealthy state.
52    pub fn failure_threshold(&self) -> u32 {
53        self.failure_threshold
54    }
55
56    pub(crate) fn validate(&self) -> ContractResult<()> {
57        if self.heartbeat_interval_ms == 0 || self.failure_threshold == 0 {
58            return Err(ContractError::InvalidValue {
59                field: "health",
60                reason: "heartbeat interval and failure threshold must be greater than zero",
61            });
62        }
63        Ok(())
64    }
65}