@@ -25,31 +25,37 @@ use actix_web::{
2525 error:: ErrorServiceUnavailable ,
2626 middleware:: Next ,
2727} ;
28- use tokio:: { select, time:: { interval, Duration } } ;
29- use tracing:: { warn, trace, info} ;
28+ use tokio:: {
29+ select,
30+ time:: { interval, Duration } ,
31+ } ;
32+ use tracing:: { info, trace, warn} ;
3033
31- use crate :: analytics:: { SYS_INFO , refresh_sys_info } ;
34+ use crate :: analytics:: { refresh_sys_info , SYS_INFO } ;
3235use crate :: parseable:: PARSEABLE ;
3336
34- static RESOURCE_CHECK_ENABLED : LazyLock < Arc < AtomicBool > > = LazyLock :: new ( || Arc :: new ( AtomicBool :: new ( false ) ) ) ;
37+ static RESOURCE_CHECK_ENABLED : LazyLock < Arc < AtomicBool > > =
38+ LazyLock :: new ( || Arc :: new ( AtomicBool :: new ( false ) ) ) ;
3539
3640/// Spawn a background task to monitor system resources
3741pub fn spawn_resource_monitor ( shutdown_rx : tokio:: sync:: oneshot:: Receiver < ( ) > ) {
3842 tokio:: spawn ( async move {
3943 let resource_check_interval = PARSEABLE . options . resource_check_interval ;
4044 let mut check_interval = interval ( Duration :: from_secs ( resource_check_interval) ) ;
4145 let mut shutdown_rx = shutdown_rx;
42-
46+
4347 let cpu_threshold = PARSEABLE . options . cpu_utilization_threshold ;
4448 let memory_threshold = PARSEABLE . options . memory_utilization_threshold ;
45-
46- info ! ( "Resource monitor started with thresholds - CPU: {:.1}%, Memory: {:.1}%" ,
47- cpu_threshold, memory_threshold) ;
49+
50+ info ! (
51+ "Resource monitor started with thresholds - CPU: {:.1}%, Memory: {:.1}%" ,
52+ cpu_threshold, memory_threshold
53+ ) ;
4854 loop {
4955 select ! {
5056 _ = check_interval. tick( ) => {
5157 trace!( "Checking system resource utilization..." ) ;
52-
58+
5359 refresh_sys_info( ) ;
5460 let ( used_memory, total_memory, cpu_usage) = tokio:: task:: spawn_blocking( || {
5561 let sys = SYS_INFO . lock( ) . unwrap( ) ;
@@ -58,36 +64,36 @@ pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) {
5864 let cpu_usage = sys. global_cpu_usage( ) ;
5965 ( used_memory, total_memory, cpu_usage)
6066 } ) . await . unwrap( ) ;
61-
67+
6268 let mut resource_ok = true ;
63-
69+
6470 // Calculate memory usage percentage
6571 let memory_usage = if total_memory > 0.0 {
6672 ( used_memory / total_memory) * 100.0
6773 } else {
6874 0.0
6975 } ;
70-
76+
7177 // Log current resource usage every few checks for debugging
72- info!( "Current resource usage - CPU: {:.1}%, Memory: {:.1}% ({:.1}GB/{:.1}GB)" ,
73- cpu_usage, memory_usage,
74- used_memory / 1024.0 / 1024.0 / 1024.0 ,
78+ info!( "Current resource usage - CPU: {:.1}%, Memory: {:.1}% ({:.1}GB/{:.1}GB)" ,
79+ cpu_usage, memory_usage,
80+ used_memory / 1024.0 / 1024.0 / 1024.0 ,
7581 total_memory / 1024.0 / 1024.0 / 1024.0 ) ;
76-
82+
7783 // Check memory utilization
7884 if memory_usage > memory_threshold {
79- warn!( "High memory usage detected: {:.1}% (threshold: {:.1}%)" ,
85+ warn!( "High memory usage detected: {:.1}% (threshold: {:.1}%)" ,
8086 memory_usage, memory_threshold) ;
8187 resource_ok = false ;
8288 }
83-
89+
8490 // Check CPU utilization
8591 if cpu_usage > cpu_threshold {
86- warn!( "High CPU usage detected: {:.1}% (threshold: {:.1}%)" ,
92+ warn!( "High CPU usage detected: {:.1}% (threshold: {:.1}%)" ,
8793 cpu_usage, cpu_threshold) ;
8894 resource_ok = false ;
8995 }
90-
96+
9197 let previous_state = RESOURCE_CHECK_ENABLED . load( std:: sync:: atomic:: Ordering :: SeqCst ) ;
9298 RESOURCE_CHECK_ENABLED . store( resource_ok, std:: sync:: atomic:: Ordering :: SeqCst ) ;
9399
@@ -115,12 +121,14 @@ pub async fn check_resource_utilization_middleware(
115121 req : ServiceRequest ,
116122 next : Next < impl MessageBody > ,
117123) -> Result < ServiceResponse < impl MessageBody > , Error > {
118-
119124 let resource_ok = RESOURCE_CHECK_ENABLED . load ( std:: sync:: atomic:: Ordering :: SeqCst ) ;
120125
121126 if !resource_ok {
122127 let error_msg = "Server resources over-utilized" ;
123- warn ! ( "Rejecting request to {} due to resource constraints" , req. path( ) ) ;
128+ warn ! (
129+ "Rejecting request to {} due to resource constraints" ,
130+ req. path( )
131+ ) ;
124132 return Err ( ErrorServiceUnavailable ( error_msg) ) ;
125133 }
126134
0 commit comments