node: Protect against non-monotonic clocks

Many (most?) system clocks are not monotonic, and even though we're
getting a `LocalTime`, which *is* monotonic when using `now()`, it is
constructed from a `SystemTime` which isn't.
This commit is contained in:
cloudhead
2024-04-30 10:56:37 +02:00
parent bb5355fcb1
commit e5df616ead
2 changed files with 13 additions and 3 deletions
+11 -1
View File
@@ -676,7 +676,17 @@ where
"Tick +{}",
now - self.started_at.expect("Service::tick: service must be initialized")
);
self.clock = now;
if now >= self.clock {
self.clock = now;
} else {
// Nb. In tests, we often move the clock forwards in time to test different behaviors,
// so this warning isn't applicable there.
#[cfg(not(test))]
warn!(
target: "service",
"System clock is not monotonic: {now} is not greater or equal to {}", self.clock
);
}
}
pub fn wake(&mut self) {