Skip to content

Commit

Permalink
throttle: another pass to try to figure out the integer/range issue
Browse files Browse the repository at this point in the history
  • Loading branch information
wez committed Jan 6, 2025
1 parent 4064b4d commit aedf744
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions crates/throttle/src/limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ static MEMORY: LazyLock<Mutex<MemoryStore>> = LazyLock::new(|| Mutex::new(Memory
static ACQUIRE_SCRIPT: LazyLock<Script> = LazyLock::new(|| {
Script::new(
r#"
local now_ts = tonumber(ARGV[1])
local expires_ts = tonumber(ARGV[2])
local now_ts = math.floor(tonumber(ARGV[1]))
local expires_ts = math.ceil(tonumber(ARGV[2]))
local limit = tonumber(ARGV[3])
local uuid = ARGV[4]
-- prune expired values
redis.call("ZREMRANGEBYSCORE", KEYS[1], "-inf", math.floor(now_ts-1))
redis.call("ZREMRANGEBYSCORE", KEYS[1], "-inf", now_ts-1)
local count = redis.call("ZCARD", KEYS[1])
if count + 1 > limit then
Expand All @@ -30,7 +30,7 @@ if count + 1 > limit then
if #smallest == 0 then
return 0
end
return math.floor(smallest[2] - now_ts)
return math.ceil(smallest[2] - now_ts)
end
redis.call("ZADD", KEYS[1], "NX", expires_ts, uuid)
return redis.status_reply('OK')
Expand Down

0 comments on commit aedf744

Please sign in to comment.