mirror of
https://github.com/nodemailer/wildduck.git
synced 2025-01-04 07:02:45 +08:00
28 lines
714 B
Lua
28 lines
714 B
Lua
local key = KEYS[1];
|
|
local increment = tonumber(ARGV[1]) or 0;
|
|
local limit = tonumber(ARGV[2]) or 0;
|
|
local windowSize = tonumber(ARGV[3]) or 0;
|
|
local current = tonumber(redis.call("GET", key)) or 0;
|
|
|
|
if current >= limit then
|
|
local ttl = tonumber(redis.call("TTL", key)) or 0;
|
|
return {0, current, ttl};
|
|
end;
|
|
|
|
local updated;
|
|
local ttl;
|
|
|
|
if increment > 0 then
|
|
-- increment
|
|
updated = tonumber(redis.call("INCRBY", key, increment));
|
|
if current == 0 then
|
|
redis.call("EXPIRE", key, windowSize);
|
|
end;
|
|
ttl = tonumber(redis.call("TTL", key)) or 0;
|
|
else
|
|
-- return current
|
|
updated = current;
|
|
ttl = tonumber(redis.call("TTL", key)) or windowSize;
|
|
end;
|
|
|
|
return {1, updated, ttl};
|