Created
August 14, 2019 11:41
-
-
Save xiezhenye/14cc6ef4295035fa6644459576453582 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
redis.replicate_commands() | |
local key=KEYS[1] | |
local size=tonumber(ARGV[1]) or 10 | |
local count=tonumber(ARGV[2]) or 10 | |
local duration=tonumber(ARGV[3]) or 1 | |
local qps = count / duration | |
local ttl = math.ceil(size / qps) | |
local ta=redis.call('time') | |
local new_t=tonumber(ta[1])*1000000 + tonumber(ta[2]) | |
local old_v=redis.call('get', key) or '0:0' | |
local p=string.find(old_v, ':', 1, true) | |
local old_t=string.sub(old_v,1,p-1) | |
local old_n=string.sub(old_v,p+1) | |
local new_n=old_n+(new_t-old_t) * qps / 1000000 | |
if new_n > size then | |
new_n = size | |
end | |
local ret | |
if new_n >= 1 then | |
new_n = new_n - 1 | |
ret=1 | |
else | |
ret=0 | |
end | |
local new_v=new_t .. ':' .. new_n | |
redis.call('setex', key, ttl, new_v) | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment