lua-resty-redis
lua-resty-redis copied to clipboard
Lots of redis connection in time wait.
trafficstars
Hi, I have a code like this:
function _M.get_redis(config, host, port)
local red = redis:new()
red:set_keepalive(config.redis_keepalive_in_milliseconds, config.redis_pool_size)
logger.debug("GoJek-Auth", "Trying to conntect to Redis")
local ok, _, err = pcall(red.connect, red, host, port)
if not ok or err ~= nil then
ngx.log(ngx.ERR, err)
return nil, errors.ERR_REDIS_CONNECTION
else
return red, nil
end
end
-- another file
-- codes
--
--
local red, err = utils.get_redis(config, config.customer_token_redis_host, config.customer_token_redis_port)
if err then
logger.error("Auth", "could not connect to redis.")
_M.authServiceResponse(config, token)
red:close()
end
where pool size is 50 and timeout is 20k ms. But when I do this.. netstat -an | grep TIME_WAIT | grep 6379 | wc -l it yields 28k , and I get a lot of cannot assign address..
I have also enabled tcp_tw_reuse and tcp_tw_recycle .. Why is this happening ? Does red:close() return the connection to the pool ?
Please read the documentation more carefully. set_keepalive returns the connection to the pool, while close actually closes the connection. set_keepalive is usually called AFTER you have executed your redis queries. Also why do you connect using pcall?