Hello!
On Tue, Jan 8, 2013 at 3:59 AM, Anatoli Marinov wrote:
> I found lua module is made by you. It is a grade work! Congratulations!
Thank you :)
> I have a plan to integrate this module in our environment. We may use it for
> dynamic configuration engine for nginx.
> My question is about shared dictionaries. How can I lock this structure for
> a small period of time.
> In my script I use shared dict and some time I need to reset it and to put
> new data in it and this should be atomic so other workers should not try to
> read when dictionary is not completely refreshed.
>
> How can I achieve this with your module?
>
You can use ngx.shared.DICT objects' "add" and "delete" methods to
emulate a custom lock, as in:
local key = "updating"
-- the exptime, 60 sec, is to prevent dead-locking
local ok, err = mystore:add(key, 1, 60)
if not ok then
if err == "exists" then
-- some other nginx worker is already updating it, so we give up
else
-- some other error happens, handle it here
end
else
-- go update the data in mystore, you'd better use pcall here
to prevent crashing in the middle
mystore:delete(key)
end
See the related documentation for more details:
http://wiki.nginx.org/HttpLuaModule#ngx.shared.DICT.add
http://wiki.nginx.org/HttpLuaModule#ngx.shared.DICT.delete
This is a common trick used by memcached users (among others).
I'm cc'ing the openresty-en mailing list:
https://groups.google.com/group/openresty-en You're encouraged to join
this list and discuss such technical issues there :)
Best regards,
-agentzh