Hi,
On 6/23/19 2:37 AM, Hadi Abbasi wrote:
> can I use it to have a shared socket connection without need to close
> and connect again for the next requests and users?
No, you cannot share the same cosocket for several requests (due to
obvious concurrency issues between concurrent requests trying to use the
same connection to Redis). Each Lua coroutine has to call
ngx.socket.tcp() and socket:connect().
That said, if you use socket:keepalive(), OpenResty will maintain
connection pools for you and subsequent socket:connect() operations will
actually reuse an underlying keepalive connection (if available).
All of this is already documented, so please have a more careful read at
the documentation:
https://github.com/openresty/lua-nginx-module#ngxsockettcp
https://github.com/openresty/lua-nginx-module#lua_socket_pool_size
For some referential code, have a look at the source code and
recommended usage (Synopsis) of OpenResty-maintained drivers, e.g.
lua-resty-redis you are trying to use:
https://github.com/openresty/lua-resty-redis
Also, I'd strongly recommend against overriding ngx.shared attributes as
per your previous code example, in order to avoid readers of your code
(or even yourself) from mixing up an actual shared dictionary with the
I/O interface you are trying to build.
Best,
Thibault