Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protect overflow when computing expiration #56

Merged
merged 3 commits into from
Dec 18, 2021

Conversation

barkanido
Copy link
Contributor

This change protects against overflow using 2 measures:

  1. cache builders now won't build caches with time_tolive or
    time_to_idle higher than 1000 years.
  2. for future computations, the internal Instant type will now only
    allow checked additions to force handling a possible overflow. in
    addition, an overflow is now an explicit panic instead of a silent
    overflow.

closes #52

This change protects against overflow using 2 measures:
1. cache builders now won't build caches with time_tolive or
time_to_idle higher than 1000 years.
2. for future  computations, the internal Instant type will now only
allow checked additions to force handling a possible overflow. in
addition, an overflow is now an explicit panic instead of a silent
overflow.
@barkanido
Copy link
Contributor Author

@tatsuya6502 can you help here?

cargo build --no-default-features --features future

fails here since the new code is located at common/time_atomic64.rs, which is not compiled when --no-default-features I have done this before noticing the "atomic64" feature flag, and because Instant was already there. WDYT? should I move it? how to go around this?

@tatsuya6502
Copy link
Member

how to go around this?

The "atomic64" feature is used to select the source file for the time module. Some 32-bit platforms (MIPS and ARMv5) do not support AtomicU64 and some older versions of quanta crate will not compile. So Moka has two different implementations for time:

moka/src/common.rs

Lines 7 to 13 in 1074082

// targe_has_atomic is more convenient but yet unstable (Rust 1.55)
// https://github.com/rust-lang/rust/issues/32976
// #[cfg_attr(target_has_atomic = "64", path = "common/time_atomic64.rs")]
#[cfg_attr(feature = "atomic64", path = "common/time_atomic64.rs")]
#[cfg_attr(not(feature = "atomic64"), path = "common/time_compat.rs")]
pub(crate) mod time;

However, by a historical reason, these two source files (common/time_atomic64.rs and common/time_compat.rs) now have identical definitions for the following types:

pub(crate) type Instant = quanta::Instant;
pub(crate) type Clock = quanta::Clock;

#[cfg(test)]
pub(crate) type Mock = quanta::Mock;

Let's clean them up now. You can remove them from these source files, and put them to common.rs.

@tatsuya6502
Copy link
Member

You can remove them from these source files, and put them to common.rs.

Or maybe rename the current time module to something else (e.g. atomic_time).

  #[cfg_attr(feature = "atomic64", path = "common/time_atomic64.rs")] 
  #[cfg_attr(not(feature = "atomic64"), path = "common/time_compat.rs")] 
- pub(crate) mod time;
+ pub(crate) mod atomic_time;

and delete Instant and Clock and Mock from these file.

Then create common/time.rs and put them.

@tatsuya6502
Copy link
Member

For the Rustfmt warnings, please do the followings:

  1. Make sure you have the latest stable version of Rust installed. (e.g. Run rustup update stable)
    • You will use Rust stable because Moka's GitHub Actions uses Rust stable for Rustfmt check.
  2. At the package root directory (where Cargo.toml is stored), run cargo fmt.
    • This will reformat all Rust source files in the package.
  3. Run cargo fmt -- --check; echo $? to verify code is formatted.
    • If code is formatted, it will only print the exit code 0, otherwise it will print diffs and then the exit code 1.

@tatsuya6502 tatsuya6502 added this to the v0.6.2 milestone Dec 18, 2021
@tatsuya6502 tatsuya6502 added the bug Something isn't working label Dec 18, 2021
Copy link
Member

@tatsuya6502 tatsuya6502 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks a lot for your contribution!

I will take care of the Rustfmt diffs after merging.

@tatsuya6502 tatsuya6502 merged commit 68e5ce0 into moka-rs:master Dec 18, 2021
@barkanido barkanido deleted the handle-ttl-overflow branch December 18, 2021 18:31
@tatsuya6502
Copy link
Member

Hi @barkanido, I noticed you are not on our contributors page. I wish everybody who contributed to this project should be on the page to get some credits.

I think this is because GitHub cannot locate your account from the email address used by your commits. As you can see, your name is not clickable on the list of your commits here, but my name (GitHub user name) is clickable on my commits here.

I think you will need to go to GitHub's settings -> email page, and add your email address there if you have not.

Thanks!

@barkanido
Copy link
Contributor Author

done. Thanks @tatsuya6502 the commits are now clickable. the contributors' page is still not updated though. maybe it take time, idk.

@tatsuya6502
Copy link
Member

Hi @barkanido, Thanks for updating your setting. I wrote to the GitHub support and told that the contributors' page was not updated. They fixed the problem by re-indexing the data. Now you are on the page!

From the GitHub support:

I have reindexed the Insight contributor's graph for the repository and the contributor's list now display correctly.

https://github.com/moka-rs/moka/graphs/contributors

Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] overflow when ttl is large
2 participants