Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Make duration calculation robust against clock drift #10042

Merged
merged 1 commit into from
Oct 18, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,13 +1076,14 @@ impl<T: Clone> FrozenForDuration<T> {
F: FnOnce() -> T,
{
let mut lock = self.value.lock();
if lock.at.elapsed() > self.duration || lock.value.is_none() {
let now = std::time::Instant::now();
if now.saturating_duration_since(lock.at) > self.duration || lock.value.is_none() {
let new_value = f();
lock.at = std::time::Instant::now();
lock.at = now;
lock.value = Some(new_value.clone());
new_value
} else {
lock.value.as_ref().expect("checked with lock above").clone()
lock.value.as_ref().expect("Checked with in branch above; qed").clone()
}
}
}
Expand Down