Hello!
2016-06-17 6:49 GMT-07:00 yang.zheng:
> 在使用ngx.location.capture_multi向上游服务器发起并行请求时,如何指定超时时间?
>
你只能在子请求内部的 location 里面指定。
--> 没错,是在location里面设置,我理解错误,以为是在请求处理阶段content_by_lua_file设置超时时间。
测试用的Nginx配置文件如下:
upstream calc_server{
server 127.0.0.1:8088;
keepalive 20;
}
# 模拟API主入口
server {
#监听端口
listen 8080;
# 关闭缓存 开发使用
lua_code_cache off;
location /main {
access_by_lua_file lua/auth.lua;
content_by_lua_file lua/main.lua;
log_by_lua_file lua/log.lua;
}
location /reward {
internal;
proxy_pass http://calc_server/reward;
proxy_connect_timeout 1;
proxy_read_timeout 1;
proxy_send_timeout 1;
}
location /quota {
internal;
proxy_pass http://calc_server/quota;
proxy_connect_timeout 1;
proxy_read_timeout 1;
proxy_send_timeout 1;
}
}
# 模拟服务处理
server {
#监听端口
listen 8088;
# 关闭缓存 开发使用
lua_code_cache off;
location /reward {
content_by_lua_file lua/reward.lua;
log_by_lua_file lua/log.lua;
}
location /quota {
content_by_lua_file lua/quota.lua;
log_by_lua_file lua/log.lua;
}
}
其中mainl.lua脚本内容为:
local reqs = {}
table.insert(reqs, { "/reward", { method = ngx.HTTP_POST, always_forward_body = true } })
table.insert(reqs, { "/quota", { method = ngx.HTTP_POST, body = data } })
local resps = { ngx.location.capture_multi(reqs) }
for i, resp in ipairs(resps) do
ngx.say("index ", i, ", status:", resp.status, " response:", resp.body)
end
在其中一个服务脚本中,利用ngx.sleep特意超时,可以看到结果。
> 比如向上游A、B、C三个服务器发起请求,ngx.location.capture_multi会等待全部的响应,即对客户端的响应时间等于三个服务器中用时最长的响应时间。
> 问题是如何设置一个超时时间,避免在极端情况下三个服务器中一个响应过长?
>
> upstream模块的配置proxy_connect_timeout,proxy_read_timeout和proxy_send_timeout都不能解决这个问题。
为什么不能?这是标准做法。
--> 是我理解错误,应该配置在子请求内部的 location ,一开始我配置到上面配置文件中/main的部分了。
另外,这里设置的单位都是秒,有没有办法配置毫秒单位的超时时间?
Regards,
-agentzh
--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty+subscribe@googlegroups.com
发言: 请发邮件到 openresty@googlegroups.com
退订: 请发邮件至 openresty+unsubscribe@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