I need a nginx-wide (i.e. multiple processes) lock.
My plan is to use a ngx.shared.DICT and methods: add (w/ sleep on failure) and delete around the non 'thread"-safe code
pseudocode would be:
local finished = false;
while (not finished) do
local success, err, forcible = ngx.shared.DICT.add("LOCK", 1);
if (not success and err == "exists") then
ngx.sleep(0); -- LOCK FAILED TRY AGAIN (NEXT NGINX EVENT LOOP)
else
finished = true; -- LOCK SUCCESS
end
end
<<CODE>>
ngx.shared.DICT.delete("LOCK");
Will this work correctly? (conceptually: i.e. ignore that its pseudo-code and that I dont check all errors from the add() call)
Is it a hack? Are there some bad side effects (other than on lock fail the co-routine sleeping some)?
Is there a better way to do this
thanks :)