performance improvements and formatting cleanup

This commit is contained in:
2025-08-11 22:37:46 -07:00
parent c3f81eef25
commit d69a4104fc
10 changed files with 141 additions and 75 deletions
+30 -1
View File
@@ -2,10 +2,13 @@
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;
use sysinfo::System;
use sysinfo::{Components, Disks, Networks, System};
use tokio::sync::{Mutex, Notify, RwLock};
pub type SharedSystem = Arc<Mutex<System>>;
pub type SharedComponents = Arc<Mutex<Components>>;
pub type SharedDisks = Arc<Mutex<Disks>>;
pub type SharedNetworks = Arc<Mutex<Networks>>;
#[derive(Clone)]
pub struct AppState {
@@ -19,4 +22,30 @@ pub struct AppState {
pub client_count: Arc<AtomicUsize>,
pub wake_sampler: Arc<Notify>,
pub auth_token: Option<String>,
// Cached containers (enumerated once; refreshed per tick)
pub components: SharedComponents,
pub disks: SharedDisks,
pub networks: SharedNetworks
}
impl AppState {
#[allow(dead_code)]
pub fn new() -> Self {
let sys = System::new(); // targeted refreshes per tick
let components = Components::new_with_refreshed_list(); // enumerate once
let disks = Disks::new_with_refreshed_list();
let networks = Networks::new_with_refreshed_list();
Self {
sys: Arc::new(Mutex::new(sys)),
components: Arc::new(Mutex::new(components)),
disks: Arc::new(Mutex::new(disks)),
networks: Arc::new(Mutex::new(networks)),
last_json: Arc::new(RwLock::new(String::new())),
client_count: Arc::new(AtomicUsize::new(0)),
wake_sampler: Arc::new(Notify::new()),
auth_token: std::env::var("SOCKTOP_TOKEN").ok().filter(|s| !s.is_empty())
}
}
}