mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 19:17:58 +00:00
Make duration calculation robust against clock drift (#10042)
It is possible that `Instant::now()` is returning an earlier clock time when being called a second time. To guard against this, we should use `saturating_duration_since`.
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user