1use std::sync::atomic::{AtomicBool, Ordering};
2use std::sync::Arc;
3use tokio::sync::broadcast;
4
5#[derive(Clone)]
6pub struct ShutdownCoordinator {
7 shutdown_tx: broadcast::Sender<()>,
8 is_shutting_down: Arc<AtomicBool>,
9}
10
11impl ShutdownCoordinator {
12 pub fn new() -> Self {
13 let (shutdown_tx, _) = broadcast::channel(1);
14 Self {
15 shutdown_tx,
16 is_shutting_down: Arc::new(AtomicBool::new(false)),
17 }
18 }
19
20 pub fn initiate(&self) {
21 self.is_shutting_down.store(true, Ordering::SeqCst);
22 let _ = self.shutdown_tx.send(());
23 }
24
25 pub fn subscribe(&self) -> broadcast::Receiver<()> {
26 self.shutdown_tx.subscribe()
27 }
28
29 pub fn is_shutting_down(&self) -> bool {
30 self.is_shutting_down.load(Ordering::SeqCst)
31 }
32}
33
34impl Default for ShutdownCoordinator {
35 fn default() -> Self {
36 Self::new()
37 }
38}