From 3cd00cb3920c2c3cd40172e464c9d50453017601 Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Thu, 23 Jun 2022 13:05:02 -0400 Subject: [PATCH] Can't ignore the max_value and name from counter's limit, as we deserialize from the key --- limitador/src/limit.rs | 2 ++ limitador/src/storage/keys.rs | 20 ++++++++++++++++++++ limitador/src/storage/mod.rs | 4 +--- limitador/tests/integration_tests.rs | 9 +++------ 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/limitador/src/limit.rs b/limitador/src/limit.rs index 5e142fc5..2133a43c 100644 --- a/limitador/src/limit.rs +++ b/limitador/src/limit.rs @@ -26,8 +26,10 @@ impl From for Namespace { #[derive(Eq, Debug, Clone, Serialize, Deserialize)] pub struct Limit { namespace: Namespace, + // #[serde(skip_serializing)] max_value: i64, seconds: u64, + // #[serde(skip_serializing)] name: Option, // Need to sort to generate the same object when using the JSON as a key or diff --git a/limitador/src/storage/keys.rs b/limitador/src/storage/keys.rs index 92c0701d..977b3cc0 100644 --- a/limitador/src/storage/keys.rs +++ b/limitador/src/storage/keys.rs @@ -37,3 +37,23 @@ pub fn counter_from_counter_key(key: &str) -> Counter { serde_json::from_str(&key[start_pos_counter..]).unwrap() } + +#[cfg(test)] +mod tests { + use crate::storage::keys::key_for_counters_of_limit; + use crate::Limit; + + #[test] + fn key_for_limit_format() { + let limit = Limit::new( + "example.com", + 10, + 60, + vec!["req.method == GET"], + vec!["app_id"], + ); + assert_eq!( + "namespace:{example.com},counters_of_limit:{\"namespace\":\"example.com\",\"seconds\":60,\"conditions\":[\"req.method == GET\"],\"variables\":[\"app_id\"]}", + key_for_counters_of_limit(&limit)) + } +} diff --git a/limitador/src/storage/mod.rs b/limitador/src/storage/mod.rs index 59cb852c..4e89448b 100644 --- a/limitador/src/storage/mod.rs +++ b/limitador/src/storage/mod.rs @@ -146,9 +146,7 @@ impl AsyncStorage { let mut limits_for_namespace = self.limits.write().unwrap(); match limits_for_namespace.get_mut(&namespace) { - Some(limits) => { - limits.insert(limit) - } + Some(limits) => limits.insert(limit), None => { let mut limits = HashSet::new(); limits.insert(limit); diff --git a/limitador/tests/integration_tests.rs b/limitador/tests/integration_tests.rs index 8fcade34..b269aefe 100644 --- a/limitador/tests/integration_tests.rs +++ b/limitador/tests/integration_tests.rs @@ -854,14 +854,11 @@ mod test { async fn add_limit_only_adds_if_not_present(rate_limiter: &mut TestsLimiter) { let namespace = "test_namespace"; - let limit_1 = - Limit::new(namespace, 10, 60, vec!["req.method == GET"], vec!["app_id"]); + let limit_1 = Limit::new(namespace, 10, 60, vec!["req.method == GET"], vec!["app_id"]); - let limit_2 = - Limit::new(namespace, 20, 60, vec!["req.method == GET"], vec!["app_id"]); + let limit_2 = Limit::new(namespace, 20, 60, vec!["req.method == GET"], vec!["app_id"]); - let mut limit_3 = - Limit::new(namespace, 20, 60, vec!["req.method == GET"], vec!["app_id"]); + let mut limit_3 = Limit::new(namespace, 20, 60, vec!["req.method == GET"], vec!["app_id"]); limit_3.set_name("Name is irrelevant too".to_owned()); assert!(rate_limiter.add_limit(&limit_1).await);