From c3a07570757f173957f059199945a2fa9405c947 Mon Sep 17 00:00:00 2001 From: dd di cesare Date: Wed, 3 Apr 2024 15:08:01 +0200 Subject: [PATCH] [refactor] Sending limit_keys as keys, simpler lua script --- limitador/src/storage/redis/redis_async.rs | 14 +++++--------- limitador/src/storage/redis/scripts.rs | 19 +++++++++---------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/limitador/src/storage/redis/redis_async.rs b/limitador/src/storage/redis/redis_async.rs index 8dff6ed5..12a0b8b6 100644 --- a/limitador/src/storage/redis/redis_async.rs +++ b/limitador/src/storage/redis/redis_async.rs @@ -286,18 +286,14 @@ impl AsyncRedisStorage { for (counter, delta) in counters_and_deltas { script_invocation.key(key_for_counter(&counter)); - script_invocation.arg(key_for_counters_of_limit(&counter.limit())); - script_invocation.arg(&counter.seconds()); + script_invocation.key(key_for_counters_of_limit(counter.limit())); + script_invocation.arg(counter.seconds()); script_invocation.arg(delta); } - async { - script_invocation - .invoke_async::<_, _>(&mut con) - .await - } - .instrument(span) - .await?; + async { script_invocation.invoke_async::<_, _>(&mut con).await } + .instrument(span) + .await?; Ok(()) } diff --git a/limitador/src/storage/redis/scripts.rs b/limitador/src/storage/redis/scripts.rs index 8f5c5d67..9dd087f3 100644 --- a/limitador/src/storage/redis/scripts.rs +++ b/limitador/src/storage/redis/scripts.rs @@ -19,23 +19,22 @@ pub const SCRIPT_UPDATE_COUNTER: &str = " end return c"; -// KEYS: List of counter keys -// ARGV[j]: Limit keys -// ARGV[j+1]: TTLs -// ARGV[j+2]: Deltas +// KEY[i]: Counter key +// KEY[i+1]: Limit key +// ARGV[i]: TTLs +// ARGV[i+1]: Deltas pub const BATCH_UPDATE_COUNTERS: &str = " - local j = 1 - for i, counter_key in ipairs(KEYS) do - local limit_key = ARGV[j] - local ttl = tonumber(ARGV[j+1]) - local delta = tonumber(ARGV[j+2]) + for i = 1, #KEYS, 2 do + local counter_key = KEYS[i] + local limit_key = KEYS[i+1] + local ttl = tonumber(ARGV[i]) + local delta = tonumber(ARGV[i+1]) local c = redis.call('incrby', counter_key, delta) if c == delta then redis.call('expire', counter_key, ttl) redis.call('sadd', limit_key, counter_key) end - j = j + 3 end ";