Re: about nginx lua

849 views
Skip to first unread message

agentzh

unread,
Jan 8, 2013, 2:36:11 PM1/8/13
to Anatoli Marinov, openresty-en
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

agentzh

unread,
Jan 10, 2013, 6:58:57 PM1/10/13
to openre...@googlegroups.com
Hello!

On Wed, Jan 9, 2013 at 8:10 AM, tolisoft wrote:
> When the record exists I want to sleep for an while and after that I want to
> try adding the key again.
> Which is the best way for sleep in lua ...
>

Use ngx.sleep(), which is nonblocking:

http://wiki.nginx.org/HttpLuaModule#ngx.sleep

Best regards,
-agentzh

eng. Anatoli Marinov

unread,
Jan 11, 2013, 4:35:45 AM1/11/13
to openre...@googlegroups.com
Thanks a lot



Best regards,
-agentzh

--
You received this message because you are subscribed to the Google Groups "openresty-en" group.
To unsubscribe from this group, send email to openresty-en...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.





--
Best Regards
eng. Anatoli Marinov
Reply all
Reply to author
Forward
0 new messages