forge
Explore
Log in

Don't you dare go Hollow

forge

Clone
★ 0

0 watching0 forksRustAGPL-3.07.1 MiB490f099Tahinli7 hours ago322 commits

live.rs43 lines, 1.4 KiBHistoryRawDownload
1//! The change signal of the mail queue. Each change to the outbox (a message queued, a try's
2//! outcome, a retry, a discard) bumps one counter, and every open queue page wakes on it through
3//! its event stream. The counter carries no data: a subscriber reads the queue again when it
4//! wakes. Nothing polls. The signal is process-wide, like the mail worker's waker.
5
6use std::sync::OnceLock;
7
8use tokio::sync::watch;
9
10static CHANGES: OnceLock<watch::Sender<u64>> = OnceLock::new();
11
12fn sender() -> &'static watch::Sender<u64> {
13 CHANGES.get_or_init(|| watch::channel(0).0)
14}
15
16/// Subscribes to queue changes. Only a bump after this call wakes the receiver.
17pub fn subscribe() -> watch::Receiver<u64> {
18 sender().subscribe()
19}
20
21/// Counts one change of the queue. Nobody has to be listening.
22pub fn bump() {
23 sender().send_modify(|generation| *generation = generation.wrapping_add(1));
24 // The health page counts the queue too.
25 crate::live::admin().bump(&crate::live::Topic::AdminHealth);
26}
27
28#[cfg(test)]
29mod tests {
30 use std::time::Duration;
31
32 use super::*;
33
34 #[tokio::test]
35 async fn a_bump_wakes_a_subscriber_that_was_subscribed_before_it() {
36 let mut rx = subscribe();
37 bump();
38 tokio::time::timeout(Duration::from_secs(1), rx.changed())
39 .await
40 .expect("woken in time")
41 .expect("the signal is open");
42 }
43}