openresty/lua-resty-memcached

What happens when you do not close a socket?

edo888 opened this issue · 5 comments

Hi,

Could you provide more details about what happens when you do call close or set_keepalive on a socket connection like in this example: https://github.com/openresty/lua-resty-memcached/blob/master/t/touch.t

Thanks!

It will get automatically closed when the current request handler quits. It's documented officially: https://github.com/openresty/lua-nginx-module#ngxsockettcp

To quote:

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 happens:

  • the current request handler completes, or
  • the Lua cosocket object value gets collected by the Lua GC.

Thanks for your input!

Which is better in your opinion during one request:

(connect, query, return to connection pool) x 100
vs
connect, query x 100, return to connection pool?

Thanks!

The latter is much more efficient, obviously. Since manipulating the connection pools has its own overhead (though small, but can be accumulated quickly).

@edo888 There is a typo in my previous comment. Just updated it. Sorry.

Thank you!