From 32a5d6042caca87f3dd67517bd2d82c1a0c8b310 Mon Sep 17 00:00:00 2001 From: Vladimir Rubin Date: Sun, 27 Apr 2025 19:36:38 +0300 Subject: [PATCH] refactor: remove unnececary arc and oncecell --- README.md | 15 +-------------- src/main.rs | 27 ++++++++++----------------- 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 926ea4b..9aa8633 100644 --- a/README.md +++ b/README.md @@ -13,18 +13,5 @@ has a unix socket (`/tmp/hinoirisetr.sock`) that accepts four commands ## TODO -- [x] добавить логгинг нормальный -- [x] избавиться от env_logger и написать свой собственный -- [x] сделать файл конфига а не блять компайлтайм конфиг(suckless🤢🤢🤮) -- [x] сделать свой парсер для конфига после serde -- [x] заебашить автоматический хотрелоад конфига -- [x] вместо юзания notify-rust напрямую пользоваться libnotify.so и грузить её через dlopen(чтоб бинарник был маленький ваще) -- [x] переползти с chrono на time потому что мне похуй не нужна суперточность(а мб и на std::time) - [ ] fix `status` command crash - -Notify: Replaced tokio::sync::Notify with smol::channel::unbounded. The Sender is used to signal, and Receiver is polled in the main loop. -UnixListener: Switched to smol::net::UnixListener. -Signal Handling: Used signal-hook with smol::spawn for async signal processing. -Timers: Replaced tokio::time::sleep with smol::Timer. -Main Loop: Used smol::future::or to combine timer and notification events, replacing tokio::select!. -Executor: Switched from #[tokio::main] to smol::block_on. +- [ ] handle signals diff --git a/src/main.rs b/src/main.rs index 5e8bde1..85a1ad1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ use smol::stream::StreamExt; const SOCKET_PATH: &str = "/tmp/hinoirisetr.sock"; static CONFIG: OnceLock>> = OnceLock::new(); -static LAST_MODIFIED: OnceLock> = OnceLock::new(); +static LAST_MODIFIED: AtomicU64 = AtomicU64::new(0); enum NotifyState { Enabled(InitializedNotificationSystem), @@ -32,12 +32,11 @@ async fn config_realoader(notify: Arc>) { trace!("config poll tick"); let config_path = get_config_path(); if config_path.exists() { - let last_modified = LAST_MODIFIED.get().expect("last_modified not init"); if let Ok(current_modified) = std::fs::metadata(&config_path) .and_then(|m| m.modified()) .map(|t| t.duration_since(UNIX_EPOCH).unwrap().as_secs()) { - let last: u64 = last_modified.load(Ordering::SeqCst); + let last: u64 = LAST_MODIFIED.load(Ordering::SeqCst); if 0 != last { if current_modified > last { trace!("{current_modified}"); @@ -184,14 +183,13 @@ fn main() { let config_path = get_config_path(); let cfg: Config = if config_path.exists() { debug!("Config file found, loading..."); - LAST_MODIFIED - .set(Arc::new(AtomicU64::new( - std::fs::metadata(&config_path) - .and_then(|m| m.modified()) - .map(|t| t.duration_since(UNIX_EPOCH).unwrap().as_secs()) - .unwrap_or(0), - ))) - .unwrap(); + LAST_MODIFIED.store( + std::fs::metadata(&config_path) + .and_then(|m| m.modified()) + .map(|t| t.duration_since(UNIX_EPOCH).unwrap().as_secs()) + .unwrap_or(0), + SeqCst, + ); match Config::load(&config_path) { Ok(cfg) => cfg, Err(err) => { @@ -309,17 +307,12 @@ async fn reload_config(notify: Arc>) { *config = cfg; trace!("new config: {:#?}", config); - // let mut last_modified = LAST_MODIFIED.get().expect("last_modified not init").clone(); let new_modified = std::fs::metadata(&config_path) .and_then(|m| m.modified()) .map(|t| t.duration_since(UNIX_EPOCH).unwrap().as_secs()) .unwrap_or(0); trace!("new_modified: {new_modified:?}"); - LAST_MODIFIED - .get() - .expect("last_modified not init") - .store(new_modified, SeqCst); - // last_modified = new_modified.into(); + LAST_MODIFIED.store(new_modified, SeqCst); let _ = notify.send(()).await; } Err(err) => {