Skip to content

Commit

Permalink
Can't ignore the max_value and name from counter's limit, as we deser…
Browse files Browse the repository at this point in the history
…ialize from the key
  • Loading branch information
alexsnaps committed Jun 23, 2022
1 parent c4930d4 commit 7d4fd33
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
2 changes: 2 additions & 0 deletions limitador/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ impl From<String> 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<String>,

// Need to sort to generate the same object when using the JSON as a key or
Expand Down
20 changes: 20 additions & 0 deletions limitador/src/storage/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
4 changes: 1 addition & 3 deletions limitador/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,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);
Expand Down
9 changes: 3 additions & 6 deletions limitador/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 7d4fd33

Please sign in to comment.