fix(client): avoid panics in uses of Instant (#2746)

Even though this is almost definitely a bug in Rust, it seems most
prudent to actively avoid the uses of `Instant` that are prone to this
bug.

This change replaces uses of `Instant::elapsed` and `Instant::sub` with
calls to `Instant::saturating_duration_since` to prevent this class of
panic.
This commit is contained in:
Oliver Gould
2022-01-31 16:33:16 -08:00
committed by GitHub
parent f1b89c117c
commit dcdd6d1090

View File

@@ -458,7 +458,9 @@ impl<T: Poolable> PoolInner<T> {
trace!("idle interval evicting closed for {:?}", key);
return false;
}
if now - entry.idle_at > dur {
// Avoid `Instant::sub` to avoid issues like rust-lang/rust#86470.
if now.saturating_duration_since(entry.idle_at) > dur {
trace!("idle interval evicting expired for {:?}", key);
return false;
}
@@ -721,7 +723,8 @@ impl Expiration {
fn expires(&self, instant: Instant) -> bool {
match self.0 {
Some(timeout) => instant.elapsed() > timeout,
// Avoid `Instant::elapsed` to avoid issues like rust-lang/rust#86470.
Some(timeout) => Instant::now().saturating_duration_since(instant) > timeout,
None => false,
}
}