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

pull/64/head
Mingwei Samuel 2024-02-24 20:49:36 -08:00
parent 2f54bb6381
commit 554f1d6f57
1 changed files with 7 additions and 4 deletions

View File

@ -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
}