lua-resty-redis
lua-resty-redis copied to clipboard
lua tcp socket read timed out
@agentzh hello! Here's my code, please help me,thank you!
local redis = require "resty.redis"
local cjson = require "cjson.safe"
local M = {}
local tab_getn = table.getn
local redisidx =60000
local max = 200
local timeout = 30000
local maxcount = 500
local count = 10
function M.connect(conf)
local red = redis:new()
red:set_timeout(timeout) -- 1 sec
local ok, err = red:connect(conf.host, conf.port)
if not ok then
ngx.log(ngx.ERR, "failed to connect redis: ", err)
ngx.sleep(1)
return M.connect(conf)
end
--red:set_timeout(timeout) -- 1 sec
return red
end
function M.rpop(red,name)
ngx.log(ngx.INFO,"---------rpop:"..tostring(name))
local res, err = red:rpop(name)
if not res then
ngx.log(ngx.ERR, "failed to pop redis : "..name, err) --lua tcp socket read timed out
return nil
end
ngx.log(ngx.INFO, "------------ rpop value:"..tostring(res))
local ok, err = red:set_keepalive(redisidx, max)
if not ok then
ngx.log(ngx.ERR,"failed to set keepalive: ", err)
end
ngx.log(ngx.INFO, "------------ get value:"..tostring(res))
return cjson.decode(res)
end
function M.rpoplen(red,name)
local list = {}
local len = count
ngx.log(ngx.INFO,"---------rpoplen:"..tostring(name))
while tab_getn(list)<maxcount and len >0 do
local res, err = red:rpop(name)
if not res then
ngx.log(ngx.ERR, "failed to rpoplen redis : "..name, err)
return nil
end
ngx.log(ngx.INFO, "------------ rpop value:"..tostring(res))
if res ~= ngx.null then
local mlist = cjson.decode(res)
if mlist then
local mlen = tab_getn(mlist)
if mlen == 0 then
break
end
for i=1, mlen do
list[#list+1] = mlist[i]
end
end
else
break
end
len = len -1
ngx.sleep(0.001)
end
local ok, err = red:set_keepalive(redisidx, max)
if not ok then
ngx.log(ngx.ERR,"failed to set keepalive: ", err)
end
if tab_getn(list)== 0 then
return nil
end
return list
end
function M.lpush(red,name,value)
ngx.log(ngx.INFO,"---------lpush:"..tostring(name))
local res, err = red:lpush(name, cjson.encode(value))
if not res then
ngx.log(ngx.ERR, "failed to lpush redis : "..name, err)
return
end
local ok, err = red:set_keepalive(redisidx, max)
if not ok then
ngx.log(ngx.ERR,"failed to set keepalive: ", err)
end
end
function M.rpush(red,name,value)
ngx.log(ngx.INFO,"---------rpush:"..tostring(name))
local res, err = red:rpush(name, cjson.encode(value))
if not res then
ngx.log(ngx.ERR, "failed to rpush redis : "..name, err)
return
end
local ok, err = red:set_keepalive(redisidx, max)
if not ok then
ngx.log(ngx.ERR,"failed to set keepalive: ", err)
end
end
return M
Hi, Do you found how to solve the issue?
@sergeydi @leafo @agentzh I haven't really solved the problem, just getting the value of the timeout, but this is not the fundamental solution, and my timeout I think it's big enough.