Skip to main content

postrust_proxy/config/
reload.rs

1//! Configuration hot-reload via file watching and database LISTEN/NOTIFY.
2
3use crate::config::ProxyConfig;
4use std::sync::Arc;
5use tokio::sync::RwLock;
6
7/// Configuration change event.
8#[derive(Debug, Clone)]
9pub enum ConfigChangeEvent {
10    /// File configuration changed
11    FileChanged,
12    /// Database route changed
13    RouteChanged { id: uuid::Uuid },
14    /// Database upstream changed
15    UpstreamChanged { id: uuid::Uuid },
16    /// Database backend changed
17    BackendChanged { id: uuid::Uuid },
18    /// Full reload requested
19    FullReload,
20}
21
22/// Manages configuration reloading.
23pub struct ConfigReloader {
24    /// Current configuration
25    config: Arc<RwLock<ProxyConfig>>,
26    /// Change event sender
27    change_tx: tokio::sync::mpsc::Sender<ConfigChangeEvent>,
28    /// Change event receiver
29    change_rx: tokio::sync::Mutex<tokio::sync::mpsc::Receiver<ConfigChangeEvent>>,
30}
31
32impl ConfigReloader {
33    /// Create a new config reloader.
34    pub fn new(config: Arc<RwLock<ProxyConfig>>) -> Self {
35        let (change_tx, change_rx) = tokio::sync::mpsc::channel(100);
36
37        Self {
38            config,
39            change_tx,
40            change_rx: tokio::sync::Mutex::new(change_rx),
41        }
42    }
43
44    /// Request a full configuration reload.
45    pub async fn request_reload(&self) {
46        let _ = self.change_tx.send(ConfigChangeEvent::FullReload).await;
47    }
48
49    /// Get the change event sender for external triggers.
50    pub fn change_sender(&self) -> tokio::sync::mpsc::Sender<ConfigChangeEvent> {
51        self.change_tx.clone()
52    }
53}