-
-
Notifications
You must be signed in to change notification settings - Fork 171
feat: Add server wide throttling #1777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ use actix_web::{ | |
| error::ErrorServiceUnavailable, | ||
| middleware::Next, | ||
| }; | ||
| use sysinfo::{MemoryRefreshKind, RefreshKind, System}; | ||
| use tokio::{ | ||
| select, | ||
| time::{Duration, interval}, | ||
|
|
@@ -37,8 +38,7 @@ use crate::parseable::PARSEABLE; | |
|
|
||
| const PROCESS_METRICS_SAMPLE_INTERVAL: Duration = Duration::from_secs(5); | ||
|
|
||
| static RESOURCE_CHECK_ENABLED: LazyLock<Arc<AtomicBool>> = | ||
| LazyLock::new(|| Arc::new(AtomicBool::new(false))); | ||
| static SERVER_OK: LazyLock<Arc<AtomicBool>> = LazyLock::new(|| Arc::new(AtomicBool::new(true))); | ||
|
|
||
| /// Spawn a background task to monitor system resources | ||
| pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) { | ||
|
|
@@ -48,61 +48,33 @@ pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) { | |
| let mut process_metrics_interval = interval(PROCESS_METRICS_SAMPLE_INTERVAL); | ||
| let mut shutdown_rx = shutdown_rx; | ||
|
|
||
| let cpu_threshold = PARSEABLE.options.cpu_utilization_threshold; | ||
| let memory_threshold = PARSEABLE.options.memory_utilization_threshold; | ||
| let memory_threshold = (PARSEABLE.options.memory_utilization_threshold / 100.0) as f64; | ||
|
|
||
| info!( | ||
| "Resource monitor started with thresholds - CPU: {:.1}%, Memory: {:.1}%", | ||
| cpu_threshold, memory_threshold | ||
| ); | ||
| loop { | ||
|
parmesant marked this conversation as resolved.
|
||
| select! { | ||
| _ = check_interval.tick() => { | ||
| trace!("Checking system resource utilization..."); | ||
|
|
||
| if !PARSEABLE.options.resource_check_enabled { | ||
| continue; | ||
| } | ||
|
Comment on lines
+56
to
+58
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🩺 Stability & Availability | 🟠 Major | ⚡ Quick win Bypass resource-check middleware when checks are disabled. When 🤖 Prompt for AI Agents |
||
| refresh_sys_info(); | ||
| let (used_memory, total_memory, cpu_usage) = tokio::task::spawn_blocking(|| { | ||
| let sys = SYS_INFO.lock().unwrap(); | ||
| let (used_memory, total_memory) = if let Some(cgroup) = sys.cgroup_limits() { | ||
| (cgroup.rss as f32,cgroup.total_memory as f32) | ||
| } else { | ||
| (sys.used_memory() as f32,sys.total_memory() as f32) | ||
| }; | ||
| let cpu_usage = sys.global_cpu_usage(); | ||
| (used_memory, total_memory, cpu_usage) | ||
| }).await.unwrap(); | ||
|
|
||
| let mut resource_ok = true; | ||
|
|
||
| // Calculate memory usage percentage | ||
| let memory_usage = if total_memory > 0.0 { | ||
| (used_memory / total_memory) * 100.0 | ||
| let mut s = System::new_with_specifics( | ||
| RefreshKind::nothing().with_memory(MemoryRefreshKind::everything()), | ||
| ); | ||
| if let Some(cgroup) = s.cgroup_limits() { | ||
| if (cgroup.rss as f64) > memory_threshold * (cgroup.total_memory as f64) { | ||
| resource_ok = false; | ||
| } | ||
| } else { | ||
| 0.0 | ||
| }; | ||
|
|
||
| // Log current resource usage every few checks for debugging | ||
| info!("Current resource usage - CPU: {:.1}%, Memory: {:.1}% ({:.1}GB/{:.1}GB)", | ||
| cpu_usage, memory_usage, | ||
| used_memory / 1024.0 / 1024.0 / 1024.0, | ||
| total_memory / 1024.0 / 1024.0 / 1024.0); | ||
|
|
||
| // Check memory utilization | ||
| if memory_usage > memory_threshold { | ||
| warn!("High memory usage detected: {:.1}% (threshold: {:.1}%)", | ||
| memory_usage, memory_threshold); | ||
| resource_ok = false; | ||
| } | ||
|
|
||
| // Check CPU utilization | ||
| if cpu_usage > cpu_threshold { | ||
| warn!("High CPU usage detected: {:.1}% (threshold: {:.1}%)", | ||
| cpu_usage, cpu_threshold); | ||
| resource_ok = false; | ||
| s.refresh_memory(); | ||
| if (s.used_memory() as f64) > memory_threshold * (s.total_memory() as f64) { | ||
| resource_ok = false; | ||
| } | ||
| } | ||
|
|
||
| let previous_state = RESOURCE_CHECK_ENABLED.load(std::sync::atomic::Ordering::SeqCst); | ||
| RESOURCE_CHECK_ENABLED.store(resource_ok, std::sync::atomic::Ordering::SeqCst); | ||
| let previous_state = SERVER_OK.load(std::sync::atomic::Ordering::SeqCst); | ||
| SERVER_OK.store(resource_ok, std::sync::atomic::Ordering::SeqCst); | ||
|
|
||
| // Log state changes | ||
| if previous_state != resource_ok { | ||
|
|
@@ -117,14 +89,18 @@ pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) { | |
| refresh_sys_info(); | ||
| let process_metrics = tokio::task::spawn_blocking(|| { | ||
| let sys = SYS_INFO.lock().unwrap(); | ||
| let total_mem = if let Some(cgroup) = sys.cgroup_limits() { | ||
| cgroup.total_memory | ||
| } else { | ||
| sys.total_memory() | ||
| }; | ||
| sysinfo::get_current_pid() | ||
| .ok() | ||
| .and_then(|pid| sys.process(pid)) | ||
| .map(|process| (process.cpu_usage() as f64, process.memory())) | ||
| .map(|process| (process.cpu_usage() as f64, process.memory(), total_mem)) | ||
| }).await.unwrap(); | ||
|
|
||
| if let Some((cpu_usage, memory_bytes)) = process_metrics { | ||
| record_process_metrics_sample(cpu_usage, memory_bytes); | ||
| if let Some((cpu_usage, memory_bytes, total_mem)) = process_metrics { | ||
| record_process_metrics_sample(cpu_usage, memory_bytes, total_mem); | ||
| } | ||
| }, | ||
| _ = &mut shutdown_rx => { | ||
|
|
@@ -142,7 +118,7 @@ pub async fn check_resource_utilization_middleware( | |
| req: ServiceRequest, | ||
| next: Next<impl MessageBody>, | ||
| ) -> Result<ServiceResponse<impl MessageBody>, Error> { | ||
| let resource_ok = RESOURCE_CHECK_ENABLED.load(std::sync::atomic::Ordering::SeqCst); | ||
| let resource_ok = SERVER_OK.load(std::sync::atomic::Ordering::SeqCst); | ||
|
|
||
| if !resource_ok { | ||
| let error_msg = "Server resources over-utilized"; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.