lua-resty-http 经常在网络良好情况下 timeout ???

540 views
Skip to first unread message

X山鹰

unread,
Nov 17, 2015, 11:23:37 PM11/17/15
to open...@googlegroups.com
大家好!

    我使用lua-resty-http写了一个http客户端并设置keep alive, 当求情的目标地址与本机网络状况良好的情况(ping 时间在20ms以内)怎么会出现“lua tcp socket connect timed out” 和 “lua tcp socket read timed out”等错误呢? 

源码:

local httpc = http.new()
httpc:set_timeout(5000)
local response, err = httpc:connect(host, port or 80)
if err then
    ngx.log(ngx.ERR, err)
end

response, err = httpc:request(option)
if not response then
    ngx.log(ngx.ERR, err)
end

ngx.status = response.status
-- Filter out hop-by-hop headeres
for k,v in pairs(response.headers) do
    if not HOP_BY_HOP_HEADERS[string.lower(k)] then
        ngx.header[k] = v
    end
end

local reader = response.body_reader
repeat
    local chunk, err = reader(8192)
    if err then
        ngx.log(ngx.ERR, err)
        break
    end

    if chunk then
        ngx.print(chunk)
    end
until not chunk

local ok, err = httpc:set_keepalive(keepalive or 90000, poolsize or 24)
if not ok then
    ngx.log(ngx.ERR, "failed to set keepalive: ", err)
    return nil
end


DeJiang Zhu

unread,
Nov 18, 2015, 12:17:31 AM11/18/15
to open...@googlegroups.com
Hello,

可能是上游服务不能及时响应,最好能结合 tcpdump 抓包分析
如果是线上服务的话,或许还需要配合一段 stap++ 脚本来获取超时的 tcp 端口号,方便找出对应的 tcp

--
--
邮件来自列表“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

YuanSheng Wang

unread,
Nov 18, 2015, 1:24:51 AM11/18/15
to open...@googlegroups.com
> local httpc = http.new()
> httpc:set_timeout(5000)
> local response, err = httpc:connect(host, port or 80)
> if err then
>     ngx.log(ngx.ERR, err)
> end

> response, err = httpc:request(option)
> if not response then
>     ngx.log(ngx.ERR, err)
> end


这段代码逻辑有严重问题,当 connect 出现连接错误的时候,此处应 return 掉,后面的所有代码都不应该往下执行。连接都失败了,后面还要做发送、接收、关闭等操作,这个逻辑太诡异了。




--
--
邮件来自列表“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