Revert "Move rate limiter logic to Lua scripts"

This reverts commit b585c6676d.
This commit is contained in:
Jon Chambers
2020-07-08 11:53:16 -04:00
committed by Jon Chambers
parent 062bf737c2
commit c5d0d4acd0
8 changed files with 286 additions and 229 deletions

View File

@@ -1,30 +0,0 @@
local bucketId = KEYS[1]
local bucketSize = tonumber(ARGV[1])
local leakRatePerMillis = tonumber(ARGV[2])
local currentTimeMillis = tonumber(ARGV[3])
local amount = tonumber(ARGV[4])
local leakyBucket
if redis.call("EXISTS", bucketId) == 1 then
leakyBucket = cjson.decode(redis.call("GET", bucketId))
else
leakyBucket = {
bucketSize = bucketSize,
leakRatePerMillis = leakRatePerMillis,
spaceRemaining = bucketSize,
lastUpdateTimeMillis = currentTimeMillis
}
end
local elapsedTime = currentTimeMillis - leakyBucket["lastUpdateTimeMillis"]
local updatedSpaceRemaining = math.min(leakyBucket["bucketSize"], math.floor(leakyBucket["spaceRemaining"] + (elapsedTime * leakyBucket["leakRatePerMillis"])))
if updatedSpaceRemaining >= amount then
leakyBucket["spaceRemaining"] = updatedSpaceRemaining - amount
redis.call("SET", bucketId, cjson.encode(leakyBucket))
return true
else
return false
end