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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use dashmap::DashMap;
use tokio::sync::broadcast;
use crate::WatchEvent;
/// Special channel key that receives a copy of every event regardless of repo.
const ALL_CHANNEL: &str = "__all__";
/// Shared event bus for broadcasting repo events to watching agents.
///
/// Uses per-repo [`tokio::sync::broadcast`] channels so subscribers
/// only receive events for repos they care about. A special "__all__"
/// channel receives a copy of every published event (used by the
/// platform bridge).
///
/// Events that are not consumed before the channel capacity (256) is
/// exhausted are silently dropped for lagged receivers.
#[derive(Clone)]
pub struct EventBus {
channels: DashMap<String, broadcast::Sender<WatchEvent>>,
}
impl EventBus {
/// Create a new event bus.
pub fn new() -> Self {
let channels = DashMap::new();
// Pre-create the global "__all__" channel.
let (tx, _) = broadcast::channel(256);
channels.insert(ALL_CHANNEL.to_string(), tx);
Self { channels }
}
/// Get or create the broadcast sender for the given key.
fn get_or_create_sender(&self, key: &str) -> broadcast::Sender<WatchEvent> {
self.channels
.entry(key.to_string())
.or_insert_with(|| {
let (tx, _) = broadcast::channel(256);
tx
})
.clone()
}
/// Publish an event to a specific repo channel AND the global "__all__" channel.
///
/// If there are no subscribers the event is silently discarded.
pub fn publish(&self, event: WatchEvent) {
let repo_id = &event.repo_id;
// Publish to repo-specific channel if repo_id is set.
if !repo_id.is_empty() {
let tx = self.get_or_create_sender(repo_id);
let _ = tx.send(event.clone());
}
// Always publish to the global "__all__" channel.
if let Some(tx) = self.channels.get(ALL_CHANNEL) {
let _ = tx.send(event);
}
}
/// Subscribe to events for a specific repo.
///
/// The receiver is created while the DashMap shard lock is held so that
/// `cleanup_idle` cannot race and remove the channel between creation
/// and subscription.
pub fn subscribe(&self, repo_id: &str) -> broadcast::Receiver<WatchEvent> {
self.channels
.entry(repo_id.to_string())
.or_insert_with(|| {
let (tx, _) = broadcast::channel(256);
tx
})
.subscribe()
}
/// Subscribe to ALL events across all repos (for the platform bridge).
pub fn subscribe_all(&self) -> broadcast::Receiver<WatchEvent> {
self.channels
.entry(ALL_CHANNEL.to_string())
.or_insert_with(|| {
let (tx, _) = broadcast::channel(256);
tx
})
.subscribe()
}
/// Remove the channel for a specific repo (e.g. when decommissioned).
pub fn remove_repo(&self, repo_id: &str) {
if repo_id != ALL_CHANNEL {
self.channels.remove(repo_id);
}
}
/// Remove channels that have no active receivers, excluding the global channel.
/// Call periodically (e.g. every few minutes) to prevent unbounded growth.
pub fn cleanup_idle(&self) {
self.channels.retain(|key, sender| {
key == ALL_CHANNEL || sender.receiver_count() > 0
});
}
}