-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from gosquared/pruning-and-improving
Pruning and improving
- Loading branch information
Showing
4 changed files
with
62 additions
and
120 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
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 |
---|---|---|
@@ -1,28 +1,47 @@ | ||
local t = redis.call('getset',KEYS[1]..'timestamp', KEYS[2]) | ||
redis.call('expire', KEYS[1]..'timestamp', KEYS[6]) | ||
if t == false then | ||
redis.call('set', KEYS[1]..'pool', KEYS[4] - KEYS[3]) | ||
redis.call('expire',KEYS[1]..'pool', KEYS[6]) | ||
return tostring(KEYS[4] - KEYS[3]) | ||
end | ||
local key = KEYS[1] | ||
local now = KEYS[2] | ||
local cost = KEYS[3] | ||
local poolMax = tonumber(KEYS[4]) | ||
local fillRate = KEYS[5] | ||
local expiry = KEYS[6] | ||
|
||
local owed = (KEYS[2] - t) / KEYS[5] | ||
local r = redis.call('get', KEYS[1]..'pool') | ||
local timestampKey = key..'timestamp' | ||
local poolKey = key..'pool' | ||
|
||
if r == false then | ||
r = KEYS[4] | ||
local before = redis.call('get', timestampKey) | ||
|
||
if before == false then | ||
redis.call('set', timestampKey, now, 'ex', expiry) | ||
|
||
local ret = poolMax - cost | ||
redis.call('set', poolKey, ret, 'ex', expiry) | ||
return tostring(ret) | ||
end | ||
if r + owed < tonumber(KEYS[4]) then | ||
r = r + owed | ||
|
||
local timediff = now - before | ||
|
||
if timediff > 0 then | ||
redis.call('set', timestampKey, now, 'ex', expiry) | ||
else | ||
r = KEYS[4] | ||
timediff = 0 | ||
end | ||
|
||
local owed = timediff / fillRate | ||
local r = redis.call('get', poolKey) | ||
|
||
if r == false then | ||
r = poolMax | ||
end | ||
|
||
r = math.min(r + owed, poolMax) | ||
|
||
local limit = 1 | ||
if r - KEYS[3] >= 0 then | ||
r = r - KEYS[3] | ||
if r - cost >= 0 then | ||
r = r - cost | ||
else | ||
limit = -1 | ||
end | ||
redis.call('set', KEYS[1]..'pool', r) | ||
redis.call('expire',KEYS[1]..'pool', KEYS[6]) | ||
|
||
redis.call('set', poolKey, r, 'ex', expiry) | ||
|
||
return tostring(r * limit) |
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