use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
pub type ReconnectCallback = Arc<dyn Fn(u32) + Send + Sync>;
pub type SubscriberId = u64;
#[derive(Default)]
struct NodeState {
subscribers: HashMap<SubscriberId, ReconnectCallback>,
}
pub struct NodeSupervisor {
nodes: Mutex<HashMap<u32, NodeState>>,
next_id: AtomicU64,
}
impl NodeSupervisor {
pub fn global() -> &'static NodeSupervisor {
static SUPERVISOR: OnceLock<NodeSupervisor> = OnceLock::new();
SUPERVISOR.get_or_init(|| NodeSupervisor {
nodes: Mutex::new(HashMap::new()),
next_id: AtomicU64::new(1),
})
}
pub fn subscribe(&self, node_id: u32, cb: ReconnectCallback) -> Subscription {
let subscriber_id = self.next_id.fetch_add(1, Ordering::Relaxed);
self.nodes
.lock()
.expect("node supervisor lock poisoning")
.entry(node_id)
.or_default()
.subscribers
.insert(subscriber_id, cb);
Subscription {
node_id,
subscriber_id,
}
}
fn unsubscribe(&self, node_id: u32, subscriber_id: SubscriberId) {
let mut nodes = self.nodes.lock().expect("node supervisor lock poisoning");
let mut remove_node = false;
if let Some(state) = nodes.get_mut(&node_id) {
state.subscribers.remove(&subscriber_id);
remove_node = state.subscribers.is_empty();
}
if remove_node {
nodes.remove(&node_id);
}
}
pub fn notify_node_dead(&self, node_id: u32) {
let cbs: Vec<ReconnectCallback> = {
let nodes = self.nodes.lock().expect("node supervisor lock poisoning");
nodes
.get(&node_id)
.map(|state| state.subscribers.values().cloned().collect())
.unwrap_or_default()
};
for cb in cbs {
cb(node_id);
}
}
}
pub struct Subscription {
node_id: u32,
subscriber_id: SubscriberId,
}
impl Subscription {
pub fn node_id(&self) -> u32 {
self.node_id
}
}
impl Drop for Subscription {
fn drop(&mut self) {
NodeSupervisor::global().unsubscribe(self.node_id, self.subscriber_id);
}
}
pub fn fire(node_id: u32) {
NodeSupervisor::global().notify_node_dead(node_id);
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::atomic::AtomicU32;
const NODE_MULTI_SVC: u32 = 0x0FEE_1001;
const NODE_MULTI_INST: u32 = 0x0FEE_1002;
const NODE_OTHER: u32 = 0x0FEE_1003;
const NODE_DROP: u32 = 0x0FEE_1004;
fn counting_cb(counter: Arc<AtomicU32>) -> ReconnectCallback {
Arc::new(move |_| {
counter.fetch_add(1, Ordering::Relaxed);
})
}
#[test]
fn fire_notifies_all_services_of_the_node() {
let fired_a = Arc::new(AtomicU32::new(0));
let fired_b = Arc::new(AtomicU32::new(0));
let _sub_a = NodeSupervisor::global().subscribe(NODE_MULTI_SVC, counting_cb(fired_a.clone()));
let _sub_b = NodeSupervisor::global().subscribe(NODE_MULTI_SVC, counting_cb(fired_b.clone()));
fire(NODE_MULTI_SVC);
assert_eq!(fired_a.load(Ordering::Relaxed), 1);
assert_eq!(fired_b.load(Ordering::Relaxed), 1);
}
#[test]
fn fire_notifies_multiple_instances_of_the_node() {
let fired_a = Arc::new(AtomicU32::new(0));
let fired_b = Arc::new(AtomicU32::new(0));
let _sub_a = NodeSupervisor::global().subscribe(NODE_MULTI_INST, counting_cb(fired_a.clone()));
let _sub_b = NodeSupervisor::global().subscribe(NODE_MULTI_INST, counting_cb(fired_b.clone()));
fire(NODE_MULTI_INST);
assert_eq!(fired_a.load(Ordering::Relaxed), 1);
assert_eq!(fired_b.load(Ordering::Relaxed), 1);
}
#[test]
fn fire_does_not_cross_nodes() {
let fired = Arc::new(AtomicU32::new(0));
let _sub = NodeSupervisor::global().subscribe(NODE_MULTI_SVC, counting_cb(fired.clone()));
fire(NODE_OTHER);
assert_eq!(fired.load(Ordering::Relaxed), 0);
}
#[test]
fn drop_unsubscribes() {
let fired = Arc::new(AtomicU32::new(0));
let sub = NodeSupervisor::global().subscribe(NODE_DROP, counting_cb(fired.clone()));
drop(sub);
fire(NODE_DROP);
assert_eq!(fired.load(Ordering::Relaxed), 0);
}
}