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,10 +112,13 @@ 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
// Pop off timestamps that are beyound the bucket duration. // `Instant::now()` call or something.
while timestamps.back().map_or(false, |ts| *ts < cutoff) { if let Some(cutoff) = Instant::now().checked_sub(self.duration + self.duration_overhead) {
timestamps.pop_back(); // Pop off timestamps that are beyound the bucket duration.
while timestamps.back().map_or(false, |ts| *ts < cutoff) {
timestamps.pop_back();
}
} }
timestamps timestamps
} }