durable-actors 0.11.0

Standalone regional durable-actors control plane, host, and durability runtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if redis.call('EXISTS', KEYS[3]) == 1 then
    if redis.call('HGET', KEYS[3], 'stopped') == '1' then return {3, 0} end
    return {0, 0}
end
if redis.call('SCARD', KEYS[1]) >= tonumber(ARGV[2]) then return {1, 0} end
local time = redis.call('TIME')
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
local rate, burst = tonumber(ARGV[3]), tonumber(ARGV[4])
local previous = redis.call('HMGET', KEYS[2], 'tokens', 'updated')
local updated = tonumber(previous[2]) or now
local tokens = math.min(burst, (tonumber(previous[1]) or burst) + math.max(0, now - updated) * rate / 1000)
if tokens < 1 then return {2, math.max(1, math.ceil((1 - tokens) / rate))} end
redis.call('HSET', KEYS[2], 'tokens', tokens - 1, 'updated', math.max(now, updated))
redis.call('PEXPIRE', KEYS[2], math.max(1000, math.ceil(burst / rate * 1000)))
redis.call('SADD', KEYS[1], ARGV[1])
-- Unknown or failed starts stay reserved until a confirmed stop or operator cleanup.
redis.call('HSET', KEYS[3], 'epoch', 0, 'sequence', 0, 'stopped', 0)
return {0, 0}