Limit sleep duration in pacemaker to achieve faster shutdown
Instead of sleeping for the entire duration, sleep for a second and
check if we have reached the pace interval while awake. This allows
pacemaker to (worst case) shutdown in a second instead of the pace
interval.
@@ -18,7 +18,7 @@ use std::sync::{
Arc,
};
use std::thread;
-use std::time::Duration;
+use std::time::{Duration, Instant};
use super::error::PacemakerStartError;
@@ -95,7 +95,13 @@ where
let join_handle = thread::Builder::new()
.name("Pacemaker".into())
.spawn(move || {
+ let mut start = Instant::now();
+ let loop_duration = Duration::from_secs(1);
+ let pace_duration = Duration::from_secs(interval);
+
while running_clone.load(Ordering::SeqCst) {
+ if start.elapsed() >= pace_duration {
+ start = Instant::now();
if let Err(err) = sender.send(new_message()) {
warn!(
"Sender has disconnected before \
@@ -104,7 +110,8 @@ where
);
break;
}
- thread::sleep(Duration::from_secs(interval));
+ }
+ thread::sleep(loop_duration);
}
})
.map_err(|err| PacemakerStartError(err.to_string()))?;
