| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | | use tokio::sync::broadcast; |
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | | #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)] |
| 12 | | pub struct LiveEvent { |
| 13 | | |
| 14 | | |
| 15 | | pub id: u64, |
| 16 | | pub topic: String, |
| 17 | | |
| 18 | | pub version: u64, |
| 19 | | |
| 20 | | pub kind: String, |
| 21 | | } |
| 22 | |
|
| 23 | |
|
| 24 | | pub mod topic { |
| 25 | 1277× | pub fn repo(id: i64) -> String { |
| 26 | 1277× | format!("repo:{id}") |
| 27 | 1277× | } |
| 28 | 148× | pub fn proposal(repo: i64, n: i64) -> String { |
| 29 | 148× | format!("repo:{repo}/proposal:{n}") |
| 30 | 148× | } |
| 31 | 69× | pub fn issue(repo: i64, n: i64) -> String { |
| 32 | 69× | format!("repo:{repo}/issue:{n}") |
| 33 | 69× | } |
| 34 | 134× | pub fn run(id: i64) -> String { |
| 35 | 134× | format!("run:{id}") |
| 36 | 134× | } |
| 37 | 100× | pub fn user(id: i64) -> String { |
| 38 | 100× | format!("user:{id}") |
| 39 | 100× | } |
| 40 | 23× | pub fn diffusion(repo: i64) -> String { |
| 41 | 23× | format!("diffusion:{repo}") |
| 42 | 23× | } |
| 43 | | } |
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | | pub const REPLAY: usize = 1024; |
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | | pub struct LiveBus { |
| 52 | | tx: broadcast::Sender<LiveEvent>, |
| 53 | | state: std::sync::Mutex<BusState>, |
| 54 | | } |
| 55 | |
|
| 56 | | #[derive(Default)] |
| 57 | | struct BusState { |
| 58 | | next_id: u64, |
| 59 | | versions: std::collections::HashMap<String, u64>, |
| 60 | | recent: std::collections::VecDeque<LiveEvent>, |
| 61 | | } |
| 62 | |
|
| 63 | | impl Default for LiveBus { |
| 64 | 196× | fn default() -> Self { |
| 65 | 196× | LiveBus { tx: broadcast::channel(1024).0, state: std::sync::Mutex::new(BusState { next_id: 1, ..Default::default() }) } |
| 66 | 196× | } |
| 67 | | } |
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | | #[derive(Debug, PartialEq, Eq)] |
| 72 | | pub enum Replay { |
| 73 | | Events(Vec<LiveEvent>), |
| 74 | | Reload, |
| 75 | | } |
| 76 | |
|
| 77 | | impl LiveBus { |
| 78 | | |
| 79 | | |
| 80 | | |
| 81 | 3495× | pub fn publish(&self, topic: &str, kind: &str) -> LiveEvent { |
| 82 | 3495× | let event = { |
| 83 | 3495× | let mut s = self.state.lock().unwrap_or_else(|p| p.into_inner()); |
| 84 | 3495× | let id = s.next_id; |
| 85 | 3495× | s.next_id += 1; |
| 86 | 3495× | let version = { |
| 87 | 3495× | let v = s.versions.entry(topic.to_string()).or_insert(0); |
| 88 | 3495× | *v += 1; |
| 89 | 3495× | *v |
| 90 | | }; |
| 91 | 3495× | let e = LiveEvent { id, topic: topic.into(), version, kind: kind.into() }; |
| 92 | 3495× | if s.recent.len() == REPLAY { |
| 93 | 6× | s.recent.pop_front(); |
| 94 | 3489× | } |
| 95 | 3495× | s.recent.push_back(e.clone()); |
| 96 | 3495× | e |
| 97 | | }; |
| 98 | 3495× | let _ = self.tx.send(event.clone()); |
| 99 | 3495× | event |
| 100 | 3495× | } |
| 101 | |
|
| 102 | 4× | pub fn subscribe(&self) -> broadcast::Receiver<LiveEvent> { |
| 103 | 4× | self.tx.subscribe() |
| 104 | 4× | } |
| 105 | |
|
| 106 | | |
| 107 | 4× | pub fn last_id(&self) -> u64 { |
| 108 | 4× | self.state.lock().unwrap_or_else(|p| p.into_inner()).next_id - 1 |
| 109 | 4× | } |
| 110 | |
|
| 111 | | |
| 112 | | |
| 113 | 10× | pub fn since(&self, last_id: u64, topics: &[String]) -> Replay { |
| 114 | 10× | let s = self.state.lock().unwrap_or_else(|p| p.into_inner()); |
| 115 | | |
| 116 | | |
| 117 | | |
| 118 | 10× | if last_id >= s.next_id { |
| 119 | 1× | return Replay::Reload; |
| 120 | 9× | } |
| 121 | 9× | let oldest = s.recent.front().map(|e| e.id).unwrap_or(s.next_id); |
| 122 | 9× | if last_id + 1 < oldest { |
| 123 | 2× | return Replay::Reload; |
| 124 | 7× | } |
| 125 | 29× | Replay::Events(s.recent.iter().filter(|e| e.id > last_id && topics.contains(&e.topic)).cloned().collect()) |
| 126 | 10× | } |
| 127 | | } |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | 1080× | pub fn hook_topics(repo_id: i64, event: &str, data: &serde_json::Value) -> Vec<(String, String)> { |
| 132 | 1080× | let kind = event.split('.').next().unwrap_or(event).to_string(); |
| 133 | 1080× | let mut topics = vec![(topic::repo(repo_id), kind.clone())]; |
| 134 | 1080× | let number = data["number"].as_i64().or_else(|| data["issue"]["number"].as_i64()); |
| 135 | 1080× | let is_issue = event.starts_with("issue") || event == "comment.issue"; |
| 136 | 1080× | let is_proposal = event.starts_with("proposal") || matches!(event, "review" | "comment" | "comment.proposal"); |
| 137 | 67× | match number { |
| 138 | 39× | Some(n) if is_issue => topics.push((topic::issue(repo_id, n), kind)), |
| 139 | 67× | Some(n) if is_proposal => topics.push((topic::proposal(repo_id, n), kind)), |
| 140 | 974× | _ => {} |
| 141 | | } |
| 142 | 1080× | topics |
| 143 | 1080× | } |
| 144 | |
|
| 145 | | #[cfg(test)] |
| 146 | | mod tests { |
| 147 | | use super::*; |
| 148 | |
|
| 149 | | #[test] |
| 150 | 1× | fn events_have_ids_and_versions_and_replay_or_say_reload() { |
| 151 | 1× | let bus = LiveBus::default(); |
| 152 | 1× | let mut rx = bus.subscribe(); |
| 153 | 1× | let a = bus.publish("repo:1", "push"); |
| 154 | 1× | let b = bus.publish("repo:2", "push"); |
| 155 | 1× | let c = bus.publish("repo:1", "check"); |
| 156 | 1× | assert_eq!((a.id, b.id, c.id), (1, 2, 3)); |
| 157 | 1× | assert_eq!((a.version, b.version, c.version), (1, 1, 2), "versions count per topic"); |
| 158 | 1× | assert_eq!(rx.try_recv().unwrap(), a, "subscribers hear every event"); |
| 159 | 1× | assert_eq!(bus.since(1, &["repo:1".into()]), Replay::Events(vec![c.clone()]), "only the topics asked for"); |
| 160 | 1× | assert_eq!(bus.since(3, &["repo:1".into()]), Replay::Events(vec![])); |
| 161 | 1× | assert_eq!(bus.since(4, &["repo:1".into()]), Replay::Reload, "an id never given here: another process's, or before a restart"); |
| 162 | 1024× | for _ in 0..REPLAY { |
| 163 | 1024× | bus.publish("repo:3", "push"); |
| 164 | 1024× | } |
| 165 | 1× | assert_eq!(bus.since(1, &["repo:1".into()]), Replay::Reload, "too old to replay"); |
| 166 | 1× | assert_eq!(bus.last_id(), 3 + REPLAY as u64); |
| 167 | 1× | } |
| 168 | |
|
| 169 | | #[test] |
| 170 | 1× | fn a_webhook_event_names_its_repository_and_its_proposal_or_issue() { |
| 171 | 1× | let mut seen = vec![]; |
| 172 | 1× | seen.extend(hook_topics(7, "review", &serde_json::json!({ "number": 4, "verdict": "approve" }))); |
| 173 | 1× | seen.extend(hook_topics(7, "issue.opened", &serde_json::json!({ "issue": { "number": 9 } }))); |
| 174 | 1× | seen.extend(hook_topics(7, "comment.issue", &serde_json::json!({ "number": 9 }))); |
| 175 | 1× | seen.extend(hook_topics(7, "push", &serde_json::json!({ "updates": [] }))); |
| 176 | 1× | assert_eq!( |
| 177 | | seen, |
| 178 | 1× | [ |
| 179 | 1× | ("repo:7", "review"), |
| 180 | 1× | ("repo:7/proposal:4", "review"), |
| 181 | 1× | ("repo:7", "issue"), |
| 182 | 1× | ("repo:7/issue:9", "issue"), |
| 183 | 1× | ("repo:7", "comment"), |
| 184 | 1× | ("repo:7/issue:9", "comment"), |
| 185 | 1× | ("repo:7", "push"), |
| 186 | 1× | ] |
| 187 | 7× | .map(|(t, k)| (t.to_string(), k.to_string())) |
| 188 | | ); |
| 189 | 1× | } |
| 190 | | } |