@@ -40,6 +40,27 @@ const PROCESS_METRICS_SAMPLE_INTERVAL: Duration = Duration::from_secs(5);
4040
4141static SERVER_OK : LazyLock < Arc < AtomicBool > > = LazyLock :: new ( || Arc :: new ( AtomicBool :: new ( true ) ) ) ;
4242
43+ async fn sample_process_metrics ( ) {
44+ refresh_sys_info ( ) ;
45+ let process_metrics = tokio:: task:: spawn_blocking ( || {
46+ let sys = SYS_INFO . lock ( ) . unwrap ( ) ;
47+ let total_mem = if let Some ( cgroup) = sys. cgroup_limits ( ) {
48+ cgroup. total_memory
49+ } else {
50+ sys. total_memory ( )
51+ } ;
52+ sysinfo:: get_current_pid ( )
53+ . ok ( )
54+ . and_then ( |pid| sys. process ( pid) )
55+ . map ( |process| ( process. cpu_usage ( ) as f64 , process. memory ( ) , total_mem) )
56+ } )
57+ . await
58+ . unwrap ( ) ;
59+ if let Some ( ( cpu_usage, memory_bytes, total_mem) ) = process_metrics {
60+ record_process_metrics_sample ( cpu_usage, memory_bytes, total_mem) ;
61+ }
62+ }
63+
4364/// Spawn a background task to monitor system resources
4465pub fn spawn_resource_monitor ( shutdown_rx : tokio:: sync:: oneshot:: Receiver < ( ) > ) {
4566 tokio:: spawn ( async move {
@@ -80,25 +101,29 @@ pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) {
80101 info!( "Resource utilization back to normal - requests will be accepted" ) ;
81102 } else {
82103 warn!( "Resource utilization too high - requests will be rejected" ) ;
104+
105+ // sleep for rejection duration (+ the check interval)
106+ let rejection_delay = tokio:: time:: sleep( Duration :: from_secs(
107+ PARSEABLE . options. rejection_duration,
108+ ) ) ;
109+ tokio:: pin!( rejection_delay) ;
110+
111+ loop {
112+ select! {
113+ _ = & mut rejection_delay => break ,
114+ _ = process_metrics_interval. tick( ) => {
115+ sample_process_metrics( ) . await ;
116+ } ,
117+ _ = & mut shutdown_rx => {
118+ trace!( "Resource monitor shutting down" ) ;
119+ return ;
120+ }
121+ }
122+ }
83123 }
84124 } ,
85125 _ = process_metrics_interval. tick( ) => {
86- refresh_sys_info( ) ;
87- let process_metrics = tokio:: task:: spawn_blocking( || {
88- let sys = SYS_INFO . lock( ) . unwrap( ) ;
89- let total_mem = if let Some ( cgroup) = sys. cgroup_limits( ) {
90- cgroup. total_memory
91- } else {
92- sys. total_memory( )
93- } ;
94- sysinfo:: get_current_pid( )
95- . ok( )
96- . and_then( |pid| sys. process( pid) )
97- . map( |process| ( process. cpu_usage( ) as f64 , process. memory( ) , total_mem) )
98- } ) . await . unwrap( ) ;
99- if let Some ( ( cpu_usage, memory_bytes, total_mem) ) = process_metrics {
100- record_process_metrics_sample( cpu_usage, memory_bytes, total_mem) ;
101- }
126+ sample_process_metrics( ) . await ;
102127 } ,
103128 _ = & mut shutdown_rx => {
104129 trace!( "Resource monitor shutting down" ) ;
0 commit comments