mirror of
https://github.com/MingweiSamuel/Riven.git
synced 2024-12-26 10:56:34 +00:00
refactor: avoid Instant
underflow in wasm web-time
(#63)
This commit is contained in:
parent
2f54bb6381
commit
554f1d6f57
1 changed files with 7 additions and 4 deletions
|
@ -112,11 +112,14 @@ impl VectorTokenBucket {
|
||||||
|
|
||||||
fn update_get_timestamps(&self) -> MutexGuard<VecDeque<Instant>> {
|
fn update_get_timestamps(&self) -> MutexGuard<VecDeque<Instant>> {
|
||||||
let mut timestamps = self.timestamps.lock();
|
let mut timestamps = self.timestamps.lock();
|
||||||
let cutoff = Instant::now() - self.duration - self.duration_overhead;
|
// Only `None` in wasm, for some implementation reason. Probably sets time 0 at the first
|
||||||
|
// `Instant::now()` call or something.
|
||||||
|
if let Some(cutoff) = Instant::now().checked_sub(self.duration + self.duration_overhead) {
|
||||||
// Pop off timestamps that are beyound the bucket duration.
|
// Pop off timestamps that are beyound the bucket duration.
|
||||||
while timestamps.back().map_or(false, |ts| *ts < cutoff) {
|
while timestamps.back().map_or(false, |ts| *ts < cutoff) {
|
||||||
timestamps.pop_back();
|
timestamps.pop_back();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
timestamps
|
timestamps
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue