Skip to content

Commit

Permalink
store increments in redis
Browse files Browse the repository at this point in the history
  • Loading branch information
eguzki committed Mar 25, 2024
1 parent 36e1deb commit 99b4cba
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
4 changes: 1 addition & 3 deletions limitador/src/storage/redis/redis_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ impl AsyncCounterStorage for AsyncRedisStorage {
redis::Script::new(SCRIPT_UPDATE_COUNTER)
.key(key_for_counter(counter))
.key(key_for_counters_of_limit(counter.limit()))
.arg(counter.max_value())
.arg(counter.seconds())
.arg(delta)
.invoke_async::<_, _>(&mut con)
Expand Down Expand Up @@ -138,7 +137,6 @@ impl AsyncCounterStorage for AsyncRedisStorage {
let result = redis::Script::new(SCRIPT_UPDATE_COUNTER)
.key(key)
.key(key_for_counters_of_limit(counter.limit()))
.arg(counter.max_value())
.arg(counter.seconds())
.arg(delta)
.invoke_async::<_, _>(&mut con)
Expand Down Expand Up @@ -187,7 +185,7 @@ impl AsyncCounterStorage for AsyncRedisStorage {
.await?
};
if let Some(val) = option {
counter.set_remaining(val);
counter.set_remaining(limit.max_value() - val);
let ttl = {
let span = trace_span!("datastore");
async { con.ttl(&counter_key).await }
Expand Down
4 changes: 1 addition & 3 deletions limitador/src/storage/redis/redis_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ impl CounterStorage for RedisStorage {
redis::Script::new(SCRIPT_UPDATE_COUNTER)
.key(key_for_counter(counter))
.key(key_for_counters_of_limit(counter.limit()))
.arg(counter.max_value())
.arg(counter.seconds())
.arg(delta)
.invoke(&mut *con)?;
Expand Down Expand Up @@ -95,7 +94,6 @@ impl CounterStorage for RedisStorage {
redis::Script::new(SCRIPT_UPDATE_COUNTER)
.key(key)
.key(key_for_counters_of_limit(counter.limit()))
.arg(counter.max_value())
.arg(counter.seconds())
.arg(delta)
.invoke(&mut *con)?;
Expand Down Expand Up @@ -125,7 +123,7 @@ impl CounterStorage for RedisStorage {
// This does not cause any bugs, but consumes memory
// unnecessarily.
if let Some(val) = con.get::<String, Option<i64>>(counter_key.clone())? {
counter.set_remaining(val);
counter.set_remaining(limit.max_value() - val);
let ttl = con.ttl(&counter_key)?;
counter.set_expires_in(Duration::from_secs(ttl));

Expand Down
16 changes: 8 additions & 8 deletions limitador/src/storage/redis/scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@

// KEYS[1]: counter key
// KEYS[2]: key that contains the counters that belong to the limit
// ARGV[1]: counter max val
// ARGV[2]: counter TTL
// ARGV[3]: delta
// ARGV[1]: counter TTL
// ARGV[2]: delta
pub const SCRIPT_UPDATE_COUNTER: &str = "
local set_res = redis.call('set', KEYS[1], ARGV[1], 'EX', ARGV[2], 'NX')
redis.call('incrby', KEYS[1], - ARGV[3])
if set_res then
redis.call('sadd', KEYS[2], KEYS[1])
end";
local c = redis.call('incrby', KEYS[1], ARGV[2])
if c == tonumber(ARGV[2]) then
redis.call('expire', KEYS[1], ARGV[1], 'NX')
redis.call('sadd', KEYS[2], KEYS[1])
end
return c";

// KEYS: the function returns the value and TTL (in ms) for these keys
// The first position of the list returned contains the value of KEYS[1], the
Expand Down
1 change: 1 addition & 0 deletions limitador/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ mod test {
if let Some(ttl) = counter.expires_in() {
assert!(ttl.as_secs() <= 60);
}
println!("check_rate_limited_and_update_load_counters: hit {}, remaining {}", hit, counter.remaining().unwrap())

Check failure on line 688 in limitador/tests/integration_tests.rs

View workflow job for this annotation

GitHub Actions / Rustfmt

expected `;`, found `assert_eq`

Check failure on line 688 in limitador/tests/integration_tests.rs

View workflow job for this annotation

GitHub Actions / Clippy

expected `;`, found `assert_eq`

Check failure on line 688 in limitador/tests/integration_tests.rs

View workflow job for this annotation

GitHub Actions / Test Suite

expected `;`, found `assert_eq`
assert_eq!(counter.remaining().unwrap(), 3 - (hit + 1));
}
}
Expand Down

0 comments on commit 99b4cba

Please sign in to comment.