Skip to main content

atomr_cluster/
heartbeat.rs

1//! Cluster heartbeat state.
2
3use std::collections::HashMap;
4
5use atomr_core::actor::Address;
6
7use atomr_remote::PhiAccrualFailureDetector;
8
9#[derive(Default)]
10pub struct HeartbeatState {
11    pub detectors: HashMap<Address, PhiAccrualFailureDetector>,
12}
13
14impl HeartbeatState {
15    pub fn new() -> Self {
16        Self::default()
17    }
18
19    pub fn heartbeat(&mut self, from: Address) {
20        self.detectors
21            .entry(from)
22            .or_insert_with(|| {
23                PhiAccrualFailureDetector::new(
24                    8.0,
25                    1000,
26                    std::time::Duration::from_millis(100),
27                    std::time::Duration::from_secs(3),
28                    std::time::Duration::from_secs(1),
29                )
30            })
31            .heartbeat_on_proxy();
32    }
33}
34
35// Helper — PhiAccrualFailureDetector has `heartbeat` via trait; use it.
36// We need to call FailureDetector::heartbeat — provide a tiny helper.
37trait _HeartbeatOnProxy {
38    fn heartbeat_on_proxy(&self);
39}
40
41impl _HeartbeatOnProxy for PhiAccrualFailureDetector {
42    fn heartbeat_on_proxy(&self) {
43        use atomr_remote::FailureDetector;
44        FailureDetector::heartbeat(self);
45    }
46}