@@ -14,12 +14,15 @@ use crate::common::{MonitorEvalHandle, MonitorEvaluator};
1414use crate :: log:: { info, warn} ;
1515use crate :: supervisor_api_client:: SupervisorAPIClient ;
1616use containers:: fixed_capacity:: FixedCapacityVec ;
17+ use core:: sync:: atomic:: { AtomicBool , Ordering } ;
1718use core:: time:: Duration ;
19+ use std:: sync:: Arc ;
20+ use std:: time:: Instant ;
1821
1922pub ( super ) struct MonitoringLogic < T : SupervisorAPIClient > {
2023 monitors : FixedCapacityVec < MonitorEvalHandle > ,
2124 client : T ,
22- last_notification : std :: time :: Instant ,
25+ last_notification : Instant ,
2326 supervisor_api_cycle : Duration ,
2427}
2528
@@ -38,7 +41,7 @@ impl<T: SupervisorAPIClient> MonitoringLogic<T> {
3841 monitors,
3942 client,
4043 supervisor_api_cycle,
41- last_notification : std :: time :: Instant :: now ( ) ,
44+ last_notification : Instant :: now ( ) ,
4245 }
4346 }
4447
@@ -55,7 +58,7 @@ impl<T: SupervisorAPIClient> MonitoringLogic<T> {
5558
5659 if !has_any_error {
5760 if self . last_notification . elapsed ( ) > self . supervisor_api_cycle {
58- self . last_notification = std :: time :: Instant :: now ( ) ;
61+ self . last_notification = Instant :: now ( ) ;
5962 self . client . notify_alive ( ) ;
6063 }
6164 } else {
@@ -70,15 +73,15 @@ impl<T: SupervisorAPIClient> MonitoringLogic<T> {
7073/// A struct that manages a unique thread for running monitoring logic periodically.
7174pub struct UniqueThreadRunner {
7275 handle : Option < std:: thread:: JoinHandle < ( ) > > ,
73- should_stop : std :: sync :: Arc < core :: sync :: atomic :: AtomicBool > ,
76+ should_stop : Arc < AtomicBool > ,
7477 internal_duration_cycle : Duration ,
7578}
7679
7780impl UniqueThreadRunner {
7881 pub ( super ) fn new ( internal_duration_cycle : Duration ) -> Self {
7982 Self {
8083 handle : None ,
81- should_stop : std :: sync :: Arc :: new ( core :: sync :: atomic :: AtomicBool :: new ( false ) ) ,
84+ should_stop : Arc :: new ( AtomicBool :: new ( false ) ) ,
8285 internal_duration_cycle,
8386 }
8487 }
@@ -96,10 +99,10 @@ impl UniqueThreadRunner {
9699 let mut next_sleep_time = interval;
97100
98101 // TODO Add some checks and log if cyclicly here is not met.
99- while !should_stop. load ( core :: sync :: atomic :: Ordering :: Relaxed ) {
102+ while !should_stop. load ( Ordering :: Relaxed ) {
100103 std:: thread:: sleep ( next_sleep_time) ;
101104
102- let now = std :: time :: Instant :: now ( ) ;
105+ let now = Instant :: now ( ) ;
103106
104107 if !monitoring_logic. run ( ) {
105108 info ! ( "Monitoring logic failed, stopping thread." ) ;
@@ -115,7 +118,7 @@ impl UniqueThreadRunner {
115118 }
116119
117120 pub fn join ( & mut self ) {
118- self . should_stop . store ( true , core :: sync :: atomic :: Ordering :: Relaxed ) ;
121+ self . should_stop . store ( true , Ordering :: Relaxed ) ;
119122 if let Some ( handle) = self . handle . take ( ) {
120123 let _ = handle. join ( ) ;
121124 }
@@ -152,28 +155,30 @@ mod tests {
152155 use crate :: worker:: { MonitoringLogic , UniqueThreadRunner } ;
153156 use crate :: TimeRange ;
154157 use containers:: fixed_capacity:: FixedCapacityVec ;
158+ use core:: sync:: atomic:: { AtomicUsize , Ordering } ;
155159 use core:: time:: Duration ;
160+ use std:: sync:: Arc ;
156161
157162 #[ derive( Clone ) ]
158163 struct MockSupervisorAPIClient {
159- pub notify_called : std :: sync :: Arc < core :: sync :: atomic :: AtomicUsize > ,
164+ pub notify_called : Arc < AtomicUsize > ,
160165 }
161166
162167 impl MockSupervisorAPIClient {
163168 pub fn new ( ) -> Self {
164169 Self {
165- notify_called : std :: sync :: Arc :: new ( core :: sync :: atomic :: AtomicUsize :: new ( 0 ) ) ,
170+ notify_called : Arc :: new ( AtomicUsize :: new ( 0 ) ) ,
166171 }
167172 }
168173
169174 fn get_notify_count ( & self ) -> usize {
170- self . notify_called . load ( core :: sync :: atomic :: Ordering :: Acquire )
175+ self . notify_called . load ( Ordering :: Acquire )
171176 }
172177 }
173178
174179 impl SupervisorAPIClient for MockSupervisorAPIClient {
175180 fn notify_alive ( & self ) {
176- self . notify_called . fetch_add ( 1 , core :: sync :: atomic :: Ordering :: AcqRel ) ;
181+ self . notify_called . fetch_add ( 1 , Ordering :: AcqRel ) ;
177182 }
178183 }
179184
0 commit comments