Skip to content

Commit

Permalink
Avoid panics from Instant::elapsed (#406)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Sproul <michael@sigmaprime.io>
  • Loading branch information
michaelsproul committed Jul 31, 2021
1 parent 9bbd908 commit a93ed8c
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/histogram.rs
Expand Up @@ -517,7 +517,8 @@ impl Instant {

pub fn elapsed(&self) -> Duration {
match &*self {
Instant::Monotonic(i) => i.elapsed(),
// We use `saturating_duration_since` to avoid panics caused by non-monotonic clocks.
Instant::Monotonic(i) => StdInstant::now().saturating_duration_since(*i),

// It is different from `Instant::Monotonic`, the resolution here is millisecond.
// The processors in an SMP system do not start all at exactly the same time
Expand Down

0 comments on commit a93ed8c

Please sign in to comment.