openresty/lua-resty-memcached

How to use the connect pool properly?

Vortexxxx opened this issue · 2 comments

Hi there, Happy new year

The code basically comes from the Synopsis:

local memcached = require "resty.memcached"
local memc, err = memcached:new()

if not memc then
    ngx.log(ngx.ERR, err)
    ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE)
end

memc:set_timeout(3000)

local ok, err = memc:connect("127.0.0.1", 11211)
if not ok then
    ngx.log(ngx.ERR, err)
    ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE)
end

local res, flags, err = memc:get("dog")
if err then
    ngx.say("failed to get dog: ", err)
    return
end

if not res then
    ngx.say("dog not found")
    return
end
local ok, err = memc:set_keepalive(100000, 100)
if not ok then
    ngx.say("cannot set keepalive: ", err)
    return
end

I tryed pressure test, the nginx config have 4 nginx worker, So I expect memcache will keep 400 connections after the test is done, but I got 0.

I tried a similar test using lua-resty-redis and found the result normal

@Vortexxxx I noted one obvious mistake: when result is not found ("dog not found"), you should also call set_keepalive() otherwise the connection would be closed automatically.

Also, requests may not get very even across all the 4 nginx worker processes (enabling reuseport in the listen directive can be helpful). But still 0 means something is definitely wrong here.

Another suggestion is to produce error logs when your set_keepalive() call returns any errors (instead of simply writing back to the client, which you may not see at all).

One further debugging tip is to temporarily enable the nginx debugging logs to see what's happening inside the nginx core.

@agentzh Thank you very much for your reply, it is working now