chorus/src/globals.rs
2024-02-19 11:02:01 +13:00

47 lines
1.5 KiB
Rust

use crate::config::{Config, FriendlyConfig};
use crate::store::Store;
use hyper::server::conn::Http;
use lazy_static::lazy_static;
use std::sync::atomic::AtomicUsize;
use std::sync::OnceLock;
use tokio::sync::broadcast::Sender as BroadcastSender;
use tokio::sync::watch::Sender as WatchSender;
use tokio::sync::RwLock;
pub struct Globals {
pub config: RwLock<Config>,
pub store: OnceLock<Store>,
pub http_server: Http,
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: BroadcastSender<usize>,
pub num_clients: AtomicUsize,
pub shutting_down: WatchSender<bool>,
}
lazy_static! {
pub static ref GLOBALS: Globals = {
let mut http_server = hyper::server::conn::Http::new();
http_server.http1_only(true);
http_server.http1_keep_alive(true);
let (new_events, _) = tokio::sync::broadcast::channel(512);
let (shutting_down, _) = tokio::sync::watch::channel(false);
Globals {
config: RwLock::new(FriendlyConfig::default().into_config().unwrap()),
store: OnceLock::new(),
http_server,
rid: OnceLock::new(),
new_events,
num_clients: AtomicUsize::new(0),
shutting_down,
}
};
}