Skip to content

Commit

Permalink
Merge pull request #477 from moka-rs/fix-panic-in-to_std_instant
Browse files Browse the repository at this point in the history
Avoid panic in an internal `base_cache::Clocks::to_std_instant` method
  • Loading branch information
tatsuya6502 authored Jan 1, 2025
2 parents cf7a279 + 93be05b commit e99303d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/future/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,20 @@ impl Clocks {
} else {
(self.origin, self.origin_std)
};
origin_std + (time.checked_duration_since(origin).unwrap())

// `checked_duration_since` should always succeed here because the `origin`
// is set when this `Cache` is created, and the `time` is either the last
// modified or last accessed time of a cached entry. So `time` should always
// be greater than or equal to `origin`.
//
// However, this is not always true when `quanta::Instant` is used as the
// time source? https://github.com/moka-rs/moka/issues/472
//
// (Or do we set zero Instant to last modified/accessed time somewhere?)
//
// As a workaround, let's use zero duration when `checked_duration_since`
// fails.
origin_std + (time.checked_duration_since(origin).unwrap_or_default())
}

#[cfg(test)]
Expand Down
15 changes: 14 additions & 1 deletion src/sync_base/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,20 @@ impl Clocks {
} else {
(self.origin, self.origin_std)
};
origin_std + (time.checked_duration_since(origin).unwrap())

// `checked_duration_since` should always succeed here because the `origin`
// is set when this `Cache` is created, and the `time` is either the last
// modified or last accessed time of a cached entry. So `time` should always
// be greater than or equal to `origin`.
//
// However, this is not always true when `quanta::Instant` is used as the
// time source? https://github.com/moka-rs/moka/issues/472
//
// (Or do we set zero Instant to last modified/accessed time somewhere?)
//
// As a workaround, let's use zero duration when `checked_duration_since`
// fails.
origin_std + (time.checked_duration_since(origin).unwrap_or_default())
}

#[cfg(test)]
Expand Down

0 comments on commit e99303d

Please sign in to comment.