acdr/cwzoonest/cwzoonest-api/target/classes/rateLimiter.lua
2024-09-05 17:58:29 +08:00

20 lines
441 B
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 限流Lua脚本
-- KEYS[1] 是键ARGV[1] 是限流次数ARGV[2] 是限流时间
local key = KEYS[1]
local limit = tonumber(ARGV[1])
local timeout = tonumber(ARGV[2])
-- 当前计数
local current = redis.call('incr', key)
if current == 1 then
-- 如果是第一次访问,设置过期时间
redis.call('expire', key, timeout)
return 1
end
-- 检查是否超过限流
if current > limit then
return 0
end
return 1