-
-
Notifications
You must be signed in to change notification settings - Fork 73
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
[BUG] overflow when ttl is large #52
Comments
Is this a good first issues for newcomers? I would like to try an contribute here |
@barkanido — Sorry for not getting back to you sooner. Yes. I believe this is a good first issue. Here are my initial thoughts. If you have better idea, please let me know. I actually think Moka should panic when a However, I think there are some rooms to improve:
Thanks! |
sounds reasonable to me. #[tokio::test]
#[should_panic(expected = "overflow when adding duration to instant")]
async fn ttl_overflow() {
// https://github.com/moka-rs/moka/issues/52
let cache:Cache<(),i32> = CacheBuilder::new(usize::MAX)
.time_to_live(Duration::MAX)
.build();
cache.insert((), 42).await;
cache.sync();
} thoughts:
[edit] I think that the problem also happens in both sync and unsync versions as well. however, testing this depends on a real clock and I think that since everything is so fast (less than a nanosecond) the addition does not overflow. Indeed, this test (for sync cache) also fails: #[test]
fn ttl_overflow() {
let cache = CacheBuilder::new(usize::MAX)
.time_to_live(Duration::new(u64::MAX, 1_000_000_000 - 1))
.build();
cache.insert((), 42);
sleep(Duration::from_millis(1000)); // funny changing this to 1 passes the test
let value = cache.get(&());
assert_eq!(value.unwrap(), 42);
} so maybe is it wiser to unify all additions to |
@barkanido — Thanks for working on it.
Yeah. Probably it is better to fail in build phase. e.g. Make Then maybe we do not have to use I am not sure because
Oh. Sorry I did not tell, but please use a mocked clock from Quanta for testing (doc). Moka has support for it. See
It is because the eviction/expiration process will run at 500 milliseconds after the cache creation. Then it will run every 300 milliseconds. (code) For testing, you want to stop the repeating eviction/expiration process by calling
I think so. You can create a function in |
It might help to read the Implementation Details section in the doc to understand how the expiration is handled. Here is a summary:
|
Thanks, @tatsuya6502 . I have implemented it type-driven by wrapping About the max allowed value, Redis allows max int of seconds (which is 136 years) and Aerospike allows for a max of 10 years (just as a reference). So to me, 1000 years is a bit too much as a Durationbut it certainly fits in a let year_secs: u64 = 365 * 24 * 3600;
let d3 = Duration::from_secs(1000 * year_secs); |
Thank you for the information. Good to know. |
The following panics:
The text was updated successfully, but these errors were encountered: