16
16
*
17
17
*/
18
18
19
+ use std:: sync:: { atomic:: AtomicBool , Arc , LazyLock } ;
20
+
19
21
use actix_web:: {
20
22
body:: MessageBody ,
21
23
dev:: { ServiceRequest , ServiceResponse } ,
@@ -24,13 +26,12 @@ use actix_web::{
24
26
middleware:: Next ,
25
27
} ;
26
28
use tokio:: { select, time:: { interval, Duration } } ;
27
- use tokio:: sync:: RwLock ;
28
29
use tracing:: { warn, trace, info} ;
29
30
30
31
use crate :: analytics:: { SYS_INFO , refresh_sys_info} ;
31
32
use crate :: parseable:: PARSEABLE ;
32
33
33
- static RESOURCE_CHECK_ENABLED : RwLock < bool > = RwLock :: const_new ( true ) ;
34
+ static RESOURCE_CHECK_ENABLED : LazyLock < Arc < AtomicBool > > = LazyLock :: new ( || Arc :: new ( AtomicBool :: new ( false ) ) ) ;
34
35
35
36
/// Spawn a background task to monitor system resources
36
37
pub fn spawn_resource_monitor ( shutdown_rx : tokio:: sync:: oneshot:: Receiver < ( ) > ) {
@@ -86,9 +87,9 @@ pub fn spawn_resource_monitor(shutdown_rx: tokio::sync::oneshot::Receiver<()>) {
86
87
resource_ok = false ;
87
88
}
88
89
89
- let previous_state = * RESOURCE_CHECK_ENABLED . read ( ) . await ;
90
- * RESOURCE_CHECK_ENABLED . write ( ) . await = resource_ok;
91
-
90
+ let previous_state = RESOURCE_CHECK_ENABLED . load ( std :: sync :: atomic :: Ordering :: SeqCst ) ;
91
+ RESOURCE_CHECK_ENABLED . store ( resource_ok, std :: sync :: atomic :: Ordering :: SeqCst ) ;
92
+
92
93
// Log state changes
93
94
if previous_state != resource_ok {
94
95
if resource_ok {
@@ -114,8 +115,8 @@ pub async fn check_resource_utilization_middleware(
114
115
next : Next < impl MessageBody > ,
115
116
) -> Result < ServiceResponse < impl MessageBody > , Error > {
116
117
117
- let resource_ok = * RESOURCE_CHECK_ENABLED . read ( ) . await ;
118
-
118
+ let resource_ok = RESOURCE_CHECK_ENABLED . load ( std :: sync :: atomic :: Ordering :: SeqCst ) ;
119
+
119
120
if !resource_ok {
120
121
let error_msg = "Server resources over-utilized" ;
121
122
warn ! ( "Rejecting request to {} due to resource constraints" , req. path( ) ) ;
0 commit comments