refactor: avoid Instant underflow in wasm web-time (#63)

This commit is contained in:
Mingwei Samuel 2024-02-24 20:49:36 -08:00
parent 2f54bb6381
commit 554f1d6f57

View file

@ -112,11 +112,14 @@ 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;
// 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
}
}