Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
92 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
local subject = KEYS[1]; | ||
local numKeys = tonumber(ARGV[1]); | ||
local numBurstKeys = tonumber(ARGV[numKeys + 2]); | ||
local totalKeys = numKeys + numBurstKeys; | ||
local bucket = tonumber(ARGV[totalKeys + 2]); | ||
local bucketCount = tonumber(ARGV[totalKeys + 3]); | ||
local bucketExpiry = tonumber(ARGV[totalKeys + 4]); | ||
local threshold = tonumber(ARGV[totalKeys + 5]); | ||
local burstThreshold = tonumber(ARGV[totalKeys + 6]) or 0; | ||
local increment = tonumber(ARGV[totalKeys + 7]); | ||
local success = false; | ||
local withinBurstThreshold = false; | ||
|
||
if numBurstKeys > 0 then | ||
local burstCount = 0; | ||
local burstCounts = redis.call("HMGET", subject, unpack(ARGV, numKeys + 3, totalKeys + 3 )); | ||
for key, value in ipairs(burstCounts) do | ||
burstCount = burstCount + (tonumber(value) or 0) | ||
end; | ||
|
||
if burstCount < burstThreshold then | ||
withinBurstThreshold = true; | ||
end | ||
end | ||
|
||
local count = 0; | ||
local counts = redis.call("HMGET", subject, unpack(ARGV, 2, numKeys + 1)); | ||
for key, value in ipairs(counts) do | ||
count = count + (tonumber(value) or 0) | ||
end; | ||
|
||
if count < threshold or withinBurstThreshold then | ||
redis.call("HINCRBY", subject, bucket, increment); | ||
redis.call("HDEL", subject, (bucket + 1) % bucketCount); | ||
redis.call("HDEL", subject, (bucket + 2) % bucketCount); | ||
redis.call("EXPIRE", subject, bucketExpiry); | ||
success = true; | ||
end | ||
|
||
return success; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
local a=KEYS[1]local b=tonumber(ARGV[1])local c=tonumber(ARGV[b+2])local d=b+c;local e=tonumber(ARGV[d+2])local f=tonumber(ARGV[d+3])local g=tonumber(ARGV[d+4])local h=tonumber(ARGV[d+5])local i=tonumber(ARGV[d+6])or 0;local j=tonumber(ARGV[d+7])local k=false;local l=false;if c>0 then local m=0;local n=redis.call("HMGET",a,unpack(ARGV,b+3,d+3))for o,p in ipairs(n)do m=m+(tonumber(p)or 0)end;if m<i then l=true end end;local q=0;local r=redis.call("HMGET",a,unpack(ARGV,2,b+1))for o,p in ipairs(r)do q=q+(tonumber(p)or 0)end;if q<h or l then redis.call("HINCRBY",a,e,j)redis.call("HDEL",a,(e+1)%f)redis.call("HDEL",a,(e+2)%f)redis.call("EXPIRE",a,g)k=true end;return k |