Hi guys,
I'm trying to wrap my head around how to properly use and reuse sockets.
I have access_by_lua_block{} that check creds against my token server. Following lua-resty-http examples, I'm doing
access_by_kua_block {
local httpc = require("resty.http").new()
httpc:connect(host, port)
res, err = httpc.request({path="/", method="GET"})
}
So on the very first request, the resty.http module gets loaded, then .new() creates new ngx.socket.tcp(), and then actual connection to my token server is created followed by a HTTP request.
Now what happens when the lua block finishes? Docs say "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"... So does it mean that on the next request, the brand new connection (i.e. new TCP handshake) will be created once again?
Assuming the answer is yes, lets say I'm adding httpc:set_keepalive (which calls tcpsock:keepalive()) to explicitly preserve it, then will httpc:connect() reuse an exiting available connection to my backend on subsequent requests? - tcpsock:connect() docs say "Calling this method on an already connected socket object will cause the original connection to be closed first.", through just before that it said "Before actually resolving the host name and connecting to the remote backend, this method will always look up the connection pool for matched idle connections created by previous calls of this method".
So to sum up:
- Will my above code reuse previously created connections (if available) on subsequent requests?
- If not, then will calling to httpc.set_keepalive() fix it?