gitdataai/libs/git/hook/pool/metrics.rs
2026-04-14 19:02:01 +08:00

43 lines
859 B
Rust

use std::sync::Arc;
use sysinfo::System;
use tokio::sync::RwLock;
pub struct CpuMonitor {
sys: Arc<RwLock<System>>,
}
impl CpuMonitor {
pub fn new() -> Self {
let mut sys = System::new();
sys.refresh_cpu_all();
Self {
sys: Arc::new(RwLock::new(sys)),
}
}
pub async fn cpu_usage(&self) -> f32 {
let mut sys = self.sys.write().await;
sys.refresh_cpu_all();
sys.global_cpu_usage()
}
pub async fn can_accept_task(
&self,
max_concurrent: usize,
cpu_threshold: f32,
running: usize,
) -> bool {
if running >= max_concurrent {
return false;
}
let cpu = self.cpu_usage().await;
cpu < cpu_threshold
}
}
impl Default for CpuMonitor {
fn default() -> Self {
Self::new()
}
}