lua-resty-redis的set_timeout()方法没有按照预想的方式超时

179 views
Skip to first unread message

Changhao Li

unread,
Mar 24, 2016, 6:25:57 AM3/24/16
to openresty
hi,各位大牛,

以下是我封装的连接Redis的通用代码。

我发现我把Redis关了后,接口并不会经过set_timeout()里面设置的时间后才失败,而是极快的就失败了。

这个是cosocket干的吗?这个set_timeout()设置的超时时间,到底是什么情况下才会生效?

谢谢~


-- 连接Redis的通用方法,当连接失败时,返回nil
-- @param string ip Redis服务器IP
-- @param number port Redis服务器端口
-- @param number timeout 连接超时时间
-- @return nil or connected redis obj
local function get_connected_redis(ip, port, timeout)
-- 校验输入参数
if (ip == nil or type(ip) ~= "string") or (port == nil or type(port) ~= "number") or (timeout ~= nil and type(timeout) ~= "number")then
return nil
end

-- 默认超时时间2s
local default_timeout = 2000 
timeout = timeout or default_timeout

-- 创建Redis连接
local red = redis:new()
-- 设置连接超时时间
red:set_timeout(timeout) 

-- 连接Redis
local ok, err = red:connect(ip, port)
if not ok then
ngx.log(ngx.ERR, "fail to connect redis, ip: " .. ip .. ", port: " .. port .. ", timeout: " .. timeout .. "err msg: " .. err)
return nil
else
return red
end
end

Changhao Li

unread,
Mar 24, 2016, 6:38:29 AM3/24/16
to openresty
补充一下,使用完后,是调用set_keepalive(10000, 1024)将Redis连接返回连接池中,而不是用close()关闭的,不知道这个会不会有影响。

在 2016年3月24日星期四 UTC+8下午6:25:57,Changhao Li写道:

Guanglin Lv

unread,
Mar 24, 2016, 11:20:06 PM3/24/16
to openresty
  你应该看捕获分析下,失败的cosocket日志。按你说的,关掉redis,应该是reset by peer而不是timeout。

  你应该尝试把redis挂起,然后验证timeout。


在 2016年3月24日星期四 UTC+8下午6:25:57,Changhao Li写道:
hi,各位大牛,

Changhao Li

unread,
Mar 25, 2016, 4:07:06 AM3/25/16
to open...@googlegroups.com
嗯,问题确认了,Redis挂了之后,timeout设置是不会应用的,内核直接会发送RST包拒绝连接。

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html

Reply all
Reply to author
Forward
0 new messages