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

Support providing an optional id to limits/counters #356

Merged
merged 2 commits into from
Jul 23, 2024

Conversation

chirino
Copy link
Contributor

@chirino chirino commented Jun 12, 2024

  • add a key_for_counters_v2 function that uses the id as the key if set, otherwise uses the previous key encoding strategy.
  • updated the distributed store to use key_for_counters_v2. Since we can’t decode a partial counter from id based keys, we now also keep in memory the Counter in a counter field of the limits map.

let key: CounterKey = counter.into();
encoded_key = postcard::to_extend(&2u8, encoded_key).unwrap();
encoded_key = postcard::to_extend(&key, encoded_key).unwrap()
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A hard choice was made here... we prefix the encoding with 0x01 or 0x02 so that a future decoder knows which type of encoding was used. The problem is that this makes it incompatible with the existing key_for_counter encoding. I guess we could commit the encoded_key = postcard::to_extend(&2u8, encoded_key).unwrap(); line to remain compatible, but then the encoding is non-determinist. You could maybe even figure out how to craft an id so that it's encoding matches an existing key in the key_for_counter format.

@@ -307,6 +311,7 @@ where

impl Limit {
pub fn new<N: Into<Namespace>, T: TryInto<Condition>>(
id: Option<String>,
Copy link
Member

Choose a reason for hiding this comment

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

Note to mostly myself here, as I'll probably pick this up, but...
Let's add a Limit::with_id(String, ..) or something, and keep ::new as is, implying the None. That would be less of change across all the files, and make I think for a cleaner API

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea. ❤️ in flight wifi.

@chirino chirino force-pushed the add-optional-id branch from 92a1333 to 70ebd0e Compare July 2, 2024 16:32
@chirino
Copy link
Contributor Author

chirino commented Jul 2, 2024

moving the id out of the constuctor drastically reduced the size of the change set.

@chirino chirino marked this pull request as ready for review July 10, 2024 12:35
@chirino
Copy link
Contributor Author

chirino commented Jul 10, 2024

@alexsnaps redis keys will now encode using shorter binary encoding only when the id is set.

* add a key_for_counters_v2 function that uses the id as the key if set, otherwise uses the previous key encoding strategy.
* updated the distributed store to use key_for_counters_v2.  Since we can’t decode a partial counter from id based keys, we now also keep in memory the Counter in a counter field of the limits map.
* Use the shorter binary encoding for limits/counters when the id is set, continue to use the older text encoding for backward compatibility when the id is not set.

Signed-off-by: Hiram Chirino <[email protected]>
@chirino
Copy link
Contributor Author

chirino commented Jul 10, 2024

after running the rate_limited_id_counter_with_sync_redis the keys in redis look like:

root@255340cd3402:/data# redis-cli --scan --pattern '*'
"\x02\x1ctest-rate_limited_id_counter"
"\x02\x1ctest-rate_limited_id_counter\x01\x06app_id\x0btest_app_id"

Copy link
Member

@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.

Wondering...

  • Should we push the version in our cargo.toml to 2.0.0-dev (and 1.0.0-dev for the crate)?
  • Should we add a test that actually verifies the encoded bytes for the new method, so to make sure they remain stable ?

…sure they remain stable.

Signed-off-by: Hiram Chirino <[email protected]>
@chirino
Copy link
Contributor Author

chirino commented Jul 15, 2024

Since I'm new to rust, I'm not sure about the "Should we push the version in our cargo.toml to 2.0.0-dev (and 1.0.0-dev for the crate)?" bit. I think this change does not break backward compact so I don't know if it needs a major rev.

@chirino chirino merged commit 1754e23 into Kuadrant:main Jul 23, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants