Add a global channel for new event advertisement (stores event offsets)

This commit is contained in:
Mike Dilger 2024-02-15 08:18:15 +13:00
parent 7f1ddff6e4
commit 52b449b6c7

View File

@ -3,6 +3,7 @@ use crate::store::Store;
use hyper::server::conn::Http; use hyper::server::conn::Http;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use std::sync::OnceLock; use std::sync::OnceLock;
use tokio::sync::broadcast::Sender;
use tokio::sync::RwLock; use tokio::sync::RwLock;
pub struct Globals { pub struct Globals {
@ -10,6 +11,12 @@ pub struct Globals {
pub store: OnceLock<Store>, pub store: OnceLock<Store>,
pub http_server: Http, pub http_server: Http,
pub rid: OnceLock<String>, pub rid: OnceLock<String>,
/// This is a broadcast channel where new incoming events are advertised by their offset.
/// Every handler needs to listen to it and check if the incoming event matches any
/// subscribed fitlers for their client, and if so, send the event to their client under
/// that subscription.
pub new_events: Sender<usize>,
} }
lazy_static! { lazy_static! {
@ -18,11 +25,14 @@ lazy_static! {
http_server.http1_only(true); http_server.http1_only(true);
http_server.http1_keep_alive(true); http_server.http1_keep_alive(true);
let (sender, _) = tokio::sync::broadcast::channel(512);
Globals { Globals {
config: RwLock::new(FriendlyConfig::default().into_config().unwrap()), config: RwLock::new(FriendlyConfig::default().into_config().unwrap()),
store: OnceLock::new(), store: OnceLock::new(),
http_server, http_server,
rid: OnceLock::new(), rid: OnceLock::new(),
new_events: sender,
} }
}; };
} }