Skip to content

Commit

Permalink
Use increments in Redis
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsnaps committed Apr 2, 2024
1 parent 67f74d4 commit 3f5deb8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 2 additions & 4 deletions limitador-server/examples/limits.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@
-
namespace: test_namespace
max_value: 10
seconds: 60
seconds: 10
conditions:
- "req.method == 'GET'"
variables:
- user_id
-
namespace: test_namespace
max_value: 5
seconds: 60
seconds: 10
conditions:
- "req.method == 'POST'"
variables:
- user_id
4 changes: 2 additions & 2 deletions limitador/src/storage/redis/counters_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl CountersCache {
}
}

pub fn increase_by(&mut self, counter: &Counter, delta: i64) {
pub fn increment_by(&mut self, counter: &Counter, delta: i64) {
if let Some(val) = self.cache.get_mut(counter) {
*val += delta
};
Expand Down Expand Up @@ -204,7 +204,7 @@ mod tests {
}

#[test]
fn insert_saves_0_when_redis_val_is_none() {
fn insert_saves_zero_when_redis_val_is_none() {
let max_val = 10;
let mut values = HashMap::new();
values.insert("app_id".to_string(), "1".to_string());
Expand Down
6 changes: 4 additions & 2 deletions limitador/src/storage/redis/redis_cached.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,9 @@ impl AsyncCounterStorage for CachedRedisStorage {
counter_ttls_msecs[i],
ttl_margin,
);
let remaining = counter.max_value() - counter_vals[i].unwrap_or(0) - delta;
let remaining = counter.max_value()
- counter_vals[i].unwrap_or(0)
- delta;
if first_limited.is_none() && remaining < 0 {
first_limited = Some(Authorization::Limited(
counter.limit().name().map(|n| n.to_owned()),
Expand Down Expand Up @@ -166,7 +168,7 @@ impl AsyncCounterStorage for CachedRedisStorage {
{
let mut cached_counters = self.cached_counters.lock().unwrap();
for counter in counters.iter() {
cached_counters.increase_by(counter, delta);
cached_counters.increment_by(counter, delta);
}
}

Expand Down

0 comments on commit 3f5deb8

Please sign in to comment.