settimeouts 设置的是单次 socket 操作的超时时间。而 http 请求中会涉及多次 socket 操作。
一种解决办法是,用 timer 在给定超时时间后执行一个收割函数,kill 掉还没有结束的 thread。
在 2017年8月16日星期三 UTC+8下午6:43:40,doo...@gmail.com写道:
Hi 各位大大:
问题: 在http中已经设置了各项timeout值 , 实际打印的日志,从http.new开始,
到响应超时有可能长达十几秒
现有的需求:根据给出的服务器列表(300+),访问每一个服务器列表的http api接口
做法:
function run(url)
local httpc = http.new()
httpc:set_timeouts(1000, 2000, 2000)
httpc:request(url)
...
end
function get_url(server_ip)
...return url
end
function gao(server_list) -- server_list = {ip1, ip2...}
local tb = {}
for _, v in pairs(server_list) do
table.insert(tb, ngx.thread.spawn(run(get_url(v))))
end
...thread wait
end
function loop()
while true do
gao(get_server_list())
end
end
根据以上,想问下是因为什么使用姿势不正确引起的问题吗?期待对于请求出错的最大时间符合设置的参数总和