| 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 |
use chrono::{DateTime, Utc}; |
| 30 |
use serde::{Deserialize, Serialize}; |
| 31 |
use tokio::sync::broadcast; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
pub const STATUS_CAPACITY: usize = 256; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
pub const LOG_CAPACITY: usize = 1024; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
pub trait BusEvent: Clone { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
fn is_high_rate(&self) -> bool; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
#[derive(Clone, Debug, Serialize, Deserialize)] |
| 54 |
pub struct EventEnvelope<E> { |
| 55 |
pub at: DateTime<Utc>, |
| 56 |
#[serde(flatten)] |
| 57 |
pub event: E, |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
#[derive(Clone)] |
| 67 |
pub struct EventBus<E> { |
| 68 |
status: broadcast::Sender<EventEnvelope<E>>, |
| 69 |
logs: broadcast::Sender<EventEnvelope<E>>, |
| 70 |
} |
| 71 |
|
| 72 |
impl<E: Clone> EventBus<E> { |
| 73 |
|
| 74 |
|
| 75 |
pub fn subscribe_status(&self) -> broadcast::Receiver<EventEnvelope<E>> { |
| 76 |
self.status.subscribe() |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
pub fn subscribe_logs(&self) -> broadcast::Receiver<EventEnvelope<E>> { |
| 81 |
self.logs.subscribe() |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
pub type EventTx<E> = EventBus<E>; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
pub fn channel<E: Clone>() -> EventTx<E> { |
| 92 |
EventBus { |
| 93 |
status: broadcast::channel(STATUS_CAPACITY).0, |
| 94 |
logs: broadcast::channel(LOG_CAPACITY).0, |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
pub fn emit<E: BusEvent>(tx: &EventTx<E>, event: E) { |
| 103 |
let envelope = EventEnvelope { |
| 104 |
at: Utc::now(), |
| 105 |
event, |
| 106 |
}; |
| 107 |
let sender = if envelope.event.is_high_rate() { |
| 108 |
&tx.logs |
| 109 |
} else { |
| 110 |
&tx.status |
| 111 |
}; |
| 112 |
let _ = sender.send(envelope); |
| 113 |
} |
| 114 |
|
| 115 |
#[cfg(test)] |
| 116 |
mod tests { |
| 117 |
use super::*; |
| 118 |
use serde::{Deserialize, Serialize}; |
| 119 |
|
| 120 |
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] |
| 121 |
#[serde(tag = "kind", rename_all = "snake_case")] |
| 122 |
enum TestEvent { |
| 123 |
Started { name: String }, |
| 124 |
Done { code: i32 }, |
| 125 |
LogChunk { seq: u32 }, |
| 126 |
} |
| 127 |
|
| 128 |
impl BusEvent for TestEvent { |
| 129 |
fn is_high_rate(&self) -> bool { |
| 130 |
matches!(self, TestEvent::LogChunk { .. }) |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
#[test] |
| 135 |
fn emit_with_zero_subscribers_does_not_panic() { |
| 136 |
let tx = channel::<TestEvent>(); |
| 137 |
emit(&tx, TestEvent::Started { name: "x".into() }); |
| 138 |
} |
| 139 |
|
| 140 |
#[tokio::test] |
| 141 |
async fn emit_reaches_a_subscriber() { |
| 142 |
let tx = channel::<TestEvent>(); |
| 143 |
let mut rx = tx.subscribe_status(); |
| 144 |
emit(&tx, TestEvent::Done { code: 7 }); |
| 145 |
let env = rx.recv().await.expect("envelope"); |
| 146 |
assert_eq!(env.event, TestEvent::Done { code: 7 }); |
| 147 |
} |
| 148 |
|
| 149 |
#[tokio::test] |
| 150 |
async fn high_rate_events_ride_the_log_channel() { |
| 151 |
let tx = channel::<TestEvent>(); |
| 152 |
let mut status_rx = tx.subscribe_status(); |
| 153 |
let mut logs_rx = tx.subscribe_logs(); |
| 154 |
emit(&tx, TestEvent::LogChunk { seq: 1 }); |
| 155 |
|
| 156 |
assert_eq!( |
| 157 |
logs_rx.recv().await.unwrap().event, |
| 158 |
TestEvent::LogChunk { seq: 1 } |
| 159 |
); |
| 160 |
|
| 161 |
assert!( |
| 162 |
status_rx.try_recv().is_err(), |
| 163 |
"log chunk must not hit the status channel" |
| 164 |
); |
| 165 |
} |
| 166 |
|
| 167 |
#[tokio::test] |
| 168 |
async fn status_survives_a_log_firehose() { |
| 169 |
|
| 170 |
|
| 171 |
let tx = channel::<TestEvent>(); |
| 172 |
let mut status_rx = tx.subscribe_status(); |
| 173 |
let mut logs_rx = tx.subscribe_logs(); |
| 174 |
emit(&tx, TestEvent::Done { code: 0 }); |
| 175 |
for i in 0..(LOG_CAPACITY + 50) { |
| 176 |
emit(&tx, TestEvent::LogChunk { seq: i as u32 }); |
| 177 |
} |
| 178 |
|
| 179 |
assert_eq!( |
| 180 |
status_rx.recv().await.expect("status survived").event, |
| 181 |
TestEvent::Done { code: 0 } |
| 182 |
); |
| 183 |
|
| 184 |
assert!(matches!( |
| 185 |
logs_rx.recv().await, |
| 186 |
Err(broadcast::error::RecvError::Lagged(_)) |
| 187 |
)); |
| 188 |
} |
| 189 |
|
| 190 |
#[test] |
| 191 |
fn envelope_serializes_with_flat_kind() { |
| 192 |
let env = EventEnvelope { |
| 193 |
at: Utc::now(), |
| 194 |
event: TestEvent::Started { |
| 195 |
name: "build".into(), |
| 196 |
}, |
| 197 |
}; |
| 198 |
let v: serde_json::Value = |
| 199 |
serde_json::from_str(&serde_json::to_string(&env).unwrap()).unwrap(); |
| 200 |
assert_eq!(v["kind"], "started"); |
| 201 |
assert_eq!(v["name"], "build"); |
| 202 |
|
| 203 |
assert!(v.get("event").is_none()); |
| 204 |
|
| 205 |
assert!(v.get("at").is_some()); |
| 206 |
} |
| 207 |
|
| 208 |
#[tokio::test] |
| 209 |
async fn lagged_subscriber_observes_recv_error_lagged() { |
| 210 |
let tx = channel::<TestEvent>(); |
| 211 |
let mut rx = tx.subscribe_status(); |
| 212 |
for i in 0..(STATUS_CAPACITY + 10) { |
| 213 |
emit(&tx, TestEvent::Done { code: i as i32 }); |
| 214 |
} |
| 215 |
let err = rx.recv().await.expect_err("expected Lagged"); |
| 216 |
match err { |
| 217 |
broadcast::error::RecvError::Lagged(n) => assert!(n >= 10), |
| 218 |
broadcast::error::RecvError::Closed => panic!("unexpected error: Closed"), |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
|