Hello!
On Tue, Sep 2, 2014 at 11:44 AM, Jon Keys wrote:
> As a general rule of thumb is it always better to create a new object per
> request or is it just for those objects which do async I/O?
> For example, if the object is not doing any I/O (e.g. just calculating hash,
> encoding base64, etc...) do I still need to create a new object per request?
You can share changeable data among all the concurrent requests as
long as there is NO nonblocking I/O operations (including ngx.sleep)
in the middle of your calculations. As long as you do not give the
control back to the nginx event loop and ngx_lua's light thread
scheduler (even implicitly), there can never be any race conditions in
between. So always be careful when you share changeable data on the
worker level. Buggy optimizations can easily lead to hard-to-debug
race conditions under load.
BTW, most of the (nonblocking) IO operations on the Lua land (like
cosocket operations) are actually *synchronous*. Nonblocking I/O is a
much better term here. The term "async I/O" can also easily get
confused with the kernel AIO feature.
Regards,
-agentzh