use crate::config::ProxyConfig;
use std::sync::Arc;
use tokio::sync::RwLock;
#[derive(Debug, Clone)]
pub enum ConfigChangeEvent {
FileChanged,
RouteChanged { id: uuid::Uuid },
UpstreamChanged { id: uuid::Uuid },
BackendChanged { id: uuid::Uuid },
FullReload,
}
pub struct ConfigReloader {
config: Arc<RwLock<ProxyConfig>>,
change_tx: tokio::sync::mpsc::Sender<ConfigChangeEvent>,
change_rx: tokio::sync::Mutex<tokio::sync::mpsc::Receiver<ConfigChangeEvent>>,
}
impl ConfigReloader {
pub fn new(config: Arc<RwLock<ProxyConfig>>) -> Self {
let (change_tx, change_rx) = tokio::sync::mpsc::channel(100);
Self {
config,
change_tx,
change_rx: tokio::sync::Mutex::new(change_rx),
}
}
pub async fn request_reload(&self) {
let _ = self.change_tx.send(ConfigChangeEvent::FullReload).await;
}
pub fn change_sender(&self) -> tokio::sync::mpsc::Sender<ConfigChangeEvent> {
self.change_tx.clone()
}
}