mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-21 18:28:01 +01:00
Revert "Move rate limiter logic to Lua scripts"
This reverts commit b585c6676d.
This commit is contained in:
committed by
Jon Chambers
parent
062bf737c2
commit
c5d0d4acd0
@@ -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
|
||||
Reference in New Issue
Block a user