@@ -25,23 +25,20 @@ use actix_web::{
2525 error:: ErrorServiceUnavailable ,
2626 middleware:: Next ,
2727} ;
28+ use sysinfo:: { MemoryRefreshKind , RefreshKind , System } ;
2829use tokio:: {
2930 select,
3031 time:: { Duration , interval} ,
3132} ;
3233use tracing:: { info, trace, warn} ;
3334
35+ use crate :: analytics:: { SYS_INFO , refresh_sys_info} ;
3436use crate :: metrics:: record_process_metrics_sample;
3537use crate :: parseable:: PARSEABLE ;
36- use crate :: {
37- analytics:: { SYS_INFO , refresh_sys_info} ,
38- metrics:: PROCESS_METRICS_ACCUMULATOR ,
39- } ;
4038
4139const PROCESS_METRICS_SAMPLE_INTERVAL : Duration = Duration :: from_secs ( 5 ) ;
4240
43- static RESOURCE_CHECK_ENABLED : LazyLock < Arc < AtomicBool > > =
44- LazyLock :: new ( || Arc :: new ( AtomicBool :: new ( false ) ) ) ;
41+ static SERVER_OK : LazyLock < Arc < AtomicBool > > = LazyLock :: new ( || Arc :: new ( AtomicBool :: new ( true ) ) ) ;
4542
4643/// Spawn a background task to monitor system resources
4744pub fn spawn_resource_monitor ( shutdown_rx : tokio:: sync:: oneshot:: Receiver < ( ) > ) {
@@ -51,7 +48,7 @@ pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) {
5148 let mut process_metrics_interval = interval ( PROCESS_METRICS_SAMPLE_INTERVAL ) ;
5249 let mut shutdown_rx = shutdown_rx;
5350
54- let memory_threshold = PARSEABLE . options . memory_utilization_threshold ;
51+ let memory_threshold = ( PARSEABLE . options . memory_utilization_threshold / 100.0 ) as f64 ;
5552
5653 loop {
5754 select ! {
@@ -62,19 +59,22 @@ pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) {
6259 refresh_sys_info( ) ;
6360
6461 let mut resource_ok = true ;
65- let process_mem = PROCESS_METRICS_ACCUMULATOR . get_mem( ) ;
66- let total_mem = PROCESS_METRICS_ACCUMULATOR . get_total_mem( ) ;
67- let memory_usage = process_mem / total_mem;
6862
69- // Check memory utilization
70- if memory_usage > memory_threshold as f64 {
71- warn!( "High memory usage detected: {:.1}% (threshold: {:.1}%)" ,
72- memory_usage, memory_threshold) ;
73- resource_ok = false ;
63+ let mut s = System :: new_with_specifics(
64+ RefreshKind :: nothing( ) . with_memory( MemoryRefreshKind :: everything( ) ) ,
65+ ) ;
66+ if let Some ( cgroup) = s. cgroup_limits( ) {
67+ if ( cgroup. rss as f64 ) > memory_threshold * ( cgroup. total_memory as f64 ) {
68+ resource_ok = false ;
69+ }
70+ } else {
71+ s. refresh_memory( ) ;
72+ if ( s. used_memory( ) as f64 ) > memory_threshold * ( s. total_memory( ) as f64 ) {
73+ resource_ok = false ;
74+ }
7475 }
75-
76- let previous_state = RESOURCE_CHECK_ENABLED . load( std:: sync:: atomic:: Ordering :: SeqCst ) ;
77- RESOURCE_CHECK_ENABLED . store( resource_ok, std:: sync:: atomic:: Ordering :: SeqCst ) ;
76+ let previous_state = SERVER_OK . load( std:: sync:: atomic:: Ordering :: SeqCst ) ;
77+ SERVER_OK . store( resource_ok, std:: sync:: atomic:: Ordering :: SeqCst ) ;
7878
7979 // Log state changes
8080 if previous_state != resource_ok {
@@ -118,7 +118,7 @@ pub async fn check_resource_utilization_middleware(
118118 req : ServiceRequest ,
119119 next : Next < impl MessageBody > ,
120120) -> Result < ServiceResponse < impl MessageBody > , Error > {
121- let resource_ok = RESOURCE_CHECK_ENABLED . load ( std:: sync:: atomic:: Ordering :: SeqCst ) ;
121+ let resource_ok = SERVER_OK . load ( std:: sync:: atomic:: Ordering :: SeqCst ) ;
122122
123123 if !resource_ok {
124124 let error_msg = "Server resources over-utilized" ;
0 commit comments