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

Cached value #288

Merged
merged 7 commits into from
Apr 10, 2024
Merged

Cached value #288

merged 7 commits into from
Apr 10, 2024

Conversation

alexsnaps
Copy link
Member

This fixes the issue when a partition lasts longer than a counter's time window and all the increments from the previous window get batched with the current one, resulting in limiting the requests too early

@alexsnaps alexsnaps force-pushed the cached-value branch 2 times, most recently from 6df11ef to aad9e79 Compare April 5, 2024 17:57
Copy link
Member Author

@alexsnaps alexsnaps left a comment

Choose a reason for hiding this comment

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

As you can see I'm not entirely happy with things as they are...
But this could be a start, and certainly fixes the issue we had. In any case, putting this up for review by you smart folks! I need some input...

- user_id
Copy link
Member Author

Choose a reason for hiding this comment

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

Wherever (probably I) that got removed, that was a mistake

@@ -5,22 +5,19 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
#[derive(Debug)]
pub(crate) struct AtomicExpiringValue {
value: AtomicI64,
expiry: AtomicU64, // in microseconds
expiry: AtomicExpiryTime,
Copy link
Member Author

Choose a reason for hiding this comment

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

I think encapsulating that behavior made sense, regardless of the change... but now that I needed reusing that very same thing... it was time ;)

self.expiry.duration()
}

#[allow(dead_code)]
Copy link
Member Author

Choose a reason for hiding this comment

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

This is annoying, it's dead code when compiling for wasm... I don't think we should fail the build when that happens tho. Dead code for that target is fine, it's all DCE'd anyways...

Comment on lines +116 to 121
let cache_ttl = self.ttl_from_redis_ttl(
redis_ttl_ms,
counter.seconds(),
counter_val,
counter.max_value(),
);
Copy link
Member Author

Choose a reason for hiding this comment

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

This probably should be folded in the CachedCounterValue type... more specifically its constructor

@alexsnaps alexsnaps marked this pull request as ready for review April 8, 2024 12:45
Copy link
Member

@didierofrivia didierofrivia left a comment

Choose a reason for hiding this comment

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

Makes more sense to use moka's cache and keep track of the counter values with the AtomicExpiringValue, also, a nice refactor done to that one. This changes drastically the way I'll be updating the cache once I get the updated values from Redis, so hopefully will be merged soon :)

Copy link
Member

@adam-cattermole adam-cattermole left a comment

Choose a reason for hiding this comment

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

This also looks good to me. The changes all seem to be make sense and it's nice to have all the logic contained in these new types

@alexsnaps alexsnaps merged commit c672633 into main Apr 10, 2024
20 checks passed
@alexsnaps alexsnaps deleted the cached-value branch April 10, 2024 06:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

3 participants