refactor: remove unnececary arc and oncecell
This commit is contained in:
parent
640dab8f56
commit
32a5d6042c
2 changed files with 11 additions and 31 deletions
15
README.md
15
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
|
||||
|
|
27
src/main.rs
27
src/main.rs
|
@ -18,7 +18,7 @@ use smol::stream::StreamExt;
|
|||
const SOCKET_PATH: &str = "/tmp/hinoirisetr.sock";
|
||||
|
||||
static CONFIG: OnceLock<Arc<RwLock<Config>>> = OnceLock::new();
|
||||
static LAST_MODIFIED: OnceLock<Arc<AtomicU64>> = OnceLock::new();
|
||||
static LAST_MODIFIED: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
enum NotifyState {
|
||||
Enabled(InitializedNotificationSystem),
|
||||
|
@ -32,12 +32,11 @@ async fn config_realoader(notify: Arc<Sender<()>>) {
|
|||
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<Sender<()>>) {
|
|||
*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) => {
|
||||
|
|
Loading…
Reference in a new issue