Skip to content

Commit

Permalink
Ban uses of Instant operations that can panic
Browse files Browse the repository at this point in the history
When comparing instances, we should use saturating varieties to help
ensure that we can't hit panics.

This change bans uses of `std::time::Instant::{duration_since, elapsed,
sub}` via clippy. Uses are ported to using
`Instant::saturating_duration_since`.

Related to linkerd/linkerd2#7748

Signed-off-by: Oliver Gould <[email protected]>
  • Loading branch information
olix0r committed Jan 31, 2022
1 parent d9dbdec commit 9772a89
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
7 changes: 6 additions & 1 deletion .clippy.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ disallowed-methods = [
# see https://github.com/rust-lang/rust/issues/90308 for details.
"std::env::set_var",
"std::env::remove_var",
]

# Avoid instances of https://github.com/rust-lang/rust/issues/86470
"std::time::Instant::duration_since",
"std::time::Instant::elapsed",
"std::time::Instant::sub",
]
2 changes: 1 addition & 1 deletion linkerd/app/integration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ macro_rules! assert_eventually {
} else if i == $retries {
panic!(
"assertion failed after {} (retried {} times): {}",
crate::HumanDuration(start_t.elapsed()),
crate::HumanDuration(Instant::now().saturating_duration_since(start_t)),
i,
format_args!($($arg)+)
)
Expand Down
2 changes: 1 addition & 1 deletion linkerd/app/integration/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl MetricMatch {
"{}\n retried {} times ({:?})",
e,
MAX_RETRIES,
start_t.elapsed()
Instant::now().saturating_duration_since(start_t)
),
Err(_) => {
tracing::trace!("waiting...");
Expand Down
2 changes: 1 addition & 1 deletion linkerd/detect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ where
let mut buf = BytesMut::with_capacity(capacity);
let detected = match time::timeout(timeout, detect.detect(&mut io, &mut buf)).await {
Ok(Ok(protocol)) => {
debug!(?protocol, elapsed = ?t0.elapsed(), "DetectResult");
debug!(?protocol, elapsed = ?time::Instant::now().saturating_duration_since(t0), "DetectResult");
Ok(protocol)
}
Err(_) => Err(DetectTimeoutError(timeout, std::marker::PhantomData)),
Expand Down
2 changes: 1 addition & 1 deletion linkerd/http-access-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ where

let response: http::Response<B2> = match this.inner.try_poll(cx) {
Poll::Pending => {
data.processing += Instant::now().duration_since(poll_start);
data.processing += Instant::now().saturating_duration_since(poll_start);
return Poll::Pending;
}
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
Expand Down

0 comments on commit 9772a89

Please sign in to comment.