Created
October 5, 2018 12:19
-
-
Save OleksandrKucherenko/77a65ae9f641308f156fe3b85098fc94 to your computer and use it in GitHub Desktop.
Redis LUA script. Reserve instance from queue/list with TTL.
This file contains hidden or 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
local list = KEYS[1] | |
local user = KEYS[2] | |
local instance = redis.call('RPOPLPUSH', list, list) | |
if nil == instance then return nil end | |
local first = instance | |
while true do | |
local id = string.sub(tostring(instance), string.len('instance.') + 1) | |
local reserved = 'reserved.' .. id | |
local confirmed = redis.call('SET', reserved, user, 'EX', 2400, 'NX') | |
if type(confirmed) == "table" then | |
if confirmed['ok'] == 'OK' then return instance end | |
end | |
instance = redis.call('RPOPLPUSH', list, list) | |
if first == instance then break end | |
end | |
return nil |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: