forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervisor.rs
More file actions
84 lines (69 loc) · 1.77 KB
/
Copy pathsupervisor.rs
File metadata and controls
84 lines (69 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//! Supervision trees for fault tolerance.
use crate::runtime::actor::Addr;
use std::collections::HashMap;
/// Supervision strategy.
#[derive(Debug, Clone)]
pub enum Strategy {
OneForOne, // Restart only failed child
OneForAll, // Restart all children
RestForOne, // Restart failed and subsequent children
}
/// Child specification.
#[derive(Debug, Clone)]
pub struct ChildSpec {
pub id: String,
pub restart: RestartPolicy,
pub max_restarts: u32,
pub restart_window: std::time::Duration,
}
#[derive(Debug, Clone)]
pub enum RestartPolicy {
Permanent, // Always restart
Temporary, // Never restart
Transient, // Restart only on abnormal exit
}
/// Supervisor node in the tree.
#[derive(Debug)]
pub struct SupervisorNode {
pub addr: Addr,
pub children: Vec<ChildSpec>,
pub strategy: Strategy,
pub restart_counts: HashMap<String, Vec<std::time::Instant>>,
}
/// Root supervisor managing all actors.
pub struct Supervisor {
actors: Vec<Addr>,
}
impl Supervisor {
pub fn new() -> Self {
Supervisor { actors: Vec::new() }
}
pub fn monitor(&mut self, addr: Addr) {
self.actors.push(addr);
}
pub fn demonitor(&mut self, addr: Addr) {
self.actors.retain(|&a| a != addr);
}
pub fn handle_exit(&self, _addr: Addr, _reason: &str) {
// Placeholder: apply restart strategy
}
pub fn actor_count(&self) -> usize {
self.actors.len()
}
}
impl Default for Supervisor {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_supervisor_monitor() {
let mut sup = Supervisor::new();
let addr = Addr::local(1);
sup.monitor(addr);
assert_eq!(sup.actor_count(), 1);
}
}