Skip to content

Commit

Permalink
Use the proper ref to update existing limits
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Nov 21, 2024
1 parent b41e335 commit b06e969
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions limitador/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ impl RateLimiter {
self.add_limit(limit.clone());
}

for limit in limits_to_keep_in_ns.union(&limits_in_namespace) {
for limit in limits_in_namespace.union(&limits_to_keep_in_ns) {
self.storage.update_limit(limit);
}
}
Expand Down Expand Up @@ -649,7 +649,7 @@ impl AsyncRateLimiter {
self.add_limit(limit.clone());
}

for limit in limits_to_keep_in_ns.union(&limits_in_namespace) {
for limit in limits_in_namespace.union(&limits_to_keep_in_ns) {
self.storage.update_limit(limit);
}
}
Expand Down Expand Up @@ -694,3 +694,42 @@ fn classify_limits_by_namespace(

res
}

#[cfg(test)]
mod test {
use crate::limit::Limit;
use crate::RateLimiter;
use std::collections::HashMap;

#[test]
fn properly_updates_existing_limits() {
let rl = RateLimiter::new(100);
let namespace = "foo";

let l = Limit::new::<_, String>(
namespace,
42,
100,
Vec::<String>::default(),
Vec::<String>::default(),
);
rl.configure_with([l.clone()]).unwrap();
let limits = rl.get_limits(&namespace.into());
assert!(limits.contains(&l));

let _ = rl.check_rate_limited_and_update(&namespace.into(), &HashMap::default(), 1, false);

let l = Limit::new(
namespace,
50,
100,
Vec::<String>::default(),
Vec::<String>::default(),
);
rl.configure_with([l.clone()]).unwrap();
let limits = rl.get_limits(&namespace.into());
assert!(limits.contains(&l));

let _ = rl.check_rate_limited_and_update(&namespace.into(), &HashMap::default(), 1, false);
}
}

0 comments on commit b06e969

Please sign in to comment.