Hello!
On Mon, Jul 14, 2014 at 3:33 AM, Lionel Duboeuf wrote:
> Just to make sure i'm doing the right may.
> In nginx.conf, I load a lua script at init_by_lua phase. Whithin it i have a
> method that provide a connection to a server via tcp/ip socket.
[...]
> then in each lua script that are called with "content_by_lua_file"
> context, i use:
Your approach won't work because
1. any socket operations not using ngx_lua's cosocket API (nor
ngx_lua's subrequest API) in request handlers (like
content_by_lua_file) will block nginx's event loop and ruin the
performance of everything.
2. ngx_lua's cosocket object's lifetime cannot go beyond the Lua
handler context creating it. (If you want to reuse the
connection/socket, put it into the connection pool via
setkeepalive()).
3. ngx_lua's cosocket API is not (yet) available in the init_by_lua* context.
For your use case, I think you should use ngx_lua cosocket's
connection pool mechanism directly:
https://github.com/openresty/lua-nginx-module#tcpsocksetkeepalive
where you can recycle the underlying socket connections instead of
recycling the higher-level Lua objects. This way you can also prevent
conflicts due to concurrent accesses.
Regards,
-agentzh