mirror of
https://github.com/MingweiSamuel/Riven.git
synced 2024-12-25 10:26:31 +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,10 +112,13 @@ impl VectorTokenBucket {
|
|||
|
||||
fn update_get_timestamps(&self) -> MutexGuard<VecDeque<Instant>> {
|
||||
let mut timestamps = self.timestamps.lock();
|
||||
let cutoff = Instant::now() - self.duration - self.duration_overhead;
|
||||
// Pop off timestamps that are beyound the bucket duration.
|
||||
while timestamps.back().map_or(false, |ts| *ts < cutoff) {
|
||||
timestamps.pop_back();
|
||||
// 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.
|
||||
while timestamps.back().map_or(false, |ts| *ts < cutoff) {
|
||||
timestamps.pop_back();
|
||||
}
|
||||
}
|
||||
timestamps
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue