Hello!
On Thu, Aug 21, 2014 at 8:32 AM, Bogdan Irimia wrote:
> I am using a combination of openresty+redis in my project and I would like to ask what happens in this situation:
[...]
> As you can see, no "close" or "set_keepalive" is called. What happens in
> this case? Is the connection automatically closed and all objects freed when
> the function finishes its execution?
The lua-resty-redis library is based on ngx_lua's cosocket API. For
every cosocket object's underlying connection, if you do not
explicitly close it (via close()) or put it back to the connection
pool (via setkeepalive()), then it is automatically closed when one of
the following two events happen:
1. the current request handler completes, or
2. the Lua cosocket object value gets collected by the Lua GC.
> Is this a good programming practice?
>
If your connection is likely to get reused soon, then you should
really put it into the connection pool. Otherwise, I'd say being
explicit by calling close() on it is more efficient (because you
release resources earlier rather than later) and is of a better style
that other readers of your code won't think you've made a mistake by
not recycling the connection.
BTW, please join the openresty-en mailing list for such discussions:
https://groups.google.com/group/openresty-en I'm cc'ing the list.
Regards,
-agentzh