Skip to main content

syslog_server_mcp/
notifier.rs

1use std::sync::Arc;
2
3use tokio::sync::Mutex;
4
5/// Abstraction over the rmcp peer notification primitive.
6#[async_trait::async_trait]
7pub trait PeerNotifier: Send + Sync {
8    async fn notify_tool_list_changed(&self) -> Result<(), String>;
9}
10
11pub struct ListChangedNotifier {
12    peers: Mutex<Vec<Arc<dyn PeerNotifier>>>,
13}
14
15impl ListChangedNotifier {
16    pub fn new() -> Self {
17        Self {
18            peers: Mutex::new(Vec::new()),
19        }
20    }
21
22    pub async fn register(&self, peer: Arc<dyn PeerNotifier>) {
23        self.peers.lock().await.push(peer);
24    }
25
26    pub async fn broadcast_list_changed(&self) {
27        let mut peers = self.peers.lock().await;
28        let mut alive = Vec::with_capacity(peers.len());
29        for peer in peers.drain(..) {
30            match peer.notify_tool_list_changed().await {
31                Ok(()) => alive.push(peer),
32                Err(e) => tracing::debug!("dropping dead peer from list_changed notifier: {e}"),
33            }
34        }
35        *peers = alive;
36    }
37
38    pub async fn peer_count(&self) -> usize {
39        self.peers.lock().await.len()
40    }
41}
42
43impl Default for ListChangedNotifier {
44    fn default() -> Self {
45        Self::new()
46    }
47}