Arc Swap Config Reload Hot

Reload configuration atomically with arc_swap for lock-free hot config updates.

RustConfig Management

Code

snippet
use arc_swap::ArcSwap;
use std::sync::Arc;
use std::time::Duration;
use tokio::time::sleep;

struct Config { port: u16, debug: bool }

static CONFIG: ArcSwap<Config> = ArcSwap::from_pointee(Config { port: 8080, debug: false });

fn get_config() -> Arc<Config> { CONFIG.load_full() }
fn reload(config: Config) { CONFIG.store(Arc::new(config)); }

#[tokio::main]
async fn main() {
    println!("port: {}", get_config().port);
    reload(Config { port: 9090, debug: true });
    sleep(Duration::from_millis(10)).await;
    println!("port: {}", get_config().port);
}