Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to set custom timeout for route #4340

Merged
merged 5 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions apisix/balancer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,18 @@ end
-- set_balancer_opts will be called in balancer phase and before any tries
local function set_balancer_opts(ctx)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can pass route directly:

function _M.run(route, ctx)

local up_conf = ctx.upstream_conf
if up_conf.timeout then
local timeout = up_conf.timeout

-- If the matched route has timeout config, prefer to use the route config.
local route_conf = ctx.matched_route.value
Yiyiyimu marked this conversation as resolved.
Show resolved Hide resolved
local timeout = nil
if route_conf.upstream_timeout then
timeout = route_conf.upstream_timeout
else
if up_conf.timeout then
timeout = up_conf.timeout
end
end
if timeout then
local ok, err = set_timeouts(timeout.connect, timeout.send,
timeout.read)
if not ok then
Expand Down
9 changes: 9 additions & 0 deletions apisix/schema_def.lua
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,15 @@ _M.route = {
minItems = 1,
uniqueItems = true,
},
upstream_timeout = {
Yiyiyimu marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May abstract the timeout schema so it can be reused by route and upstream schema.

type = "object",
properties = {
connect = {type = "number", exclusiveMinimum = 0},
send = {type = "number", exclusiveMinimum = 0},
read = {type = "number", exclusiveMinimum = 0},
},
required = {"connect", "send", "read"},
},
vars = {
type = "array",
},
Expand Down
6 changes: 6 additions & 0 deletions docs/en/latest/admin-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Note: When the `Admin API` is enabled, it will occupy the API prefixed with `/ap
| script | False | Script | See [Script](architecture-design/script.md) for more | |
| upstream | False | Upstream | Enabled Upstream configuration, see [Upstream](architecture-design/upstream.md) for more | |
| upstream_id | False | Upstream | Enabled upstream id, see [Upstream](architecture-design/upstream.md) for more | |
| upstream_timeout | False | Upstream | Set the upstream timeout for connection, sending and receiving messages of the route. This option will overwrite the [timeout](#upstream) option which set in upstream configuration. | {"connect": 3, "send": 3, "read": 3} |
Yiyiyimu marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
| upstream_timeout | False | Upstream | Set the upstream timeout for connection, sending and receiving messages of the route. This option will overwrite the [timeout](#upstream) option which set in upstream configuration. | {"connect": 3, "send": 3, "read": 3} |
| upstream_timeout | False | Upstream | Set the upstream timeout for connecting, sending and receiving messages of the route. This option will overwrite the [timeout](#upstream) option which set in upstream configuration. | {"connect": 3, "send": 3, "read": 3} |

| service_id | False | Service | Binded Service configuration, see [Service](architecture-design/service.md) for more | |
| plugin_config_id | False, can't be used with `script` | Plugin | Binded plugin config object, see [Plugin Config](architecture-design/plugin-config.md) for more | |
| labels | False | Match Rules | Key/value pairs to specify attributes | {"version":"v2","build":"16","env":"production"} |
Expand Down Expand Up @@ -117,6 +118,11 @@ Config Example:
"vars": [["http_user", "==", "ios"]], # A list of one or more `[var, operator, val]` elements
"upstream_id": "1", # upstream id, recommended
"upstream": {}, # upstream, not recommended
"upstream_timeout": { # Set the upstream timeout for connection, sending and receiving messages of the route.
"connect": 3,
"send": 3,
"read": 3
},
"filter_func": "", # User-defined filtering function
}
```
Expand Down
6 changes: 6 additions & 0 deletions docs/zh/latest/admin-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ Admin API 是为 Apache APISIX 服务的一组 API,我们可以将参数传递
| script | 可选 | Script | 详见 [Script](architecture-design/script.md) | |
| upstream | 可选 | Upstream | 启用的 Upstream 配置,详见 [Upstream](architecture-design/upstream.md) | |
| upstream_id | 可选 | Upstream | 启用的 upstream id,详见 [Upstream](architecture-design/upstream.md) | |
| upstream_timeout | 可选 | Upstream | 为 route 设置 upstream 的连接、发送消息、接收消息的超时时间。这个配置将会覆盖在 upstream 中 配置的 [timeout](#upstream) 选项 | {"connect": 3, "send": 3, "read": 3} |
| service_id | 可选 | Service | 绑定的 Service 配置,详见 [Service](architecture-design/service.md) | |
| plugin_config_id | 可选,无法跟 script 一起配置 | Plugin | 绑定的 Plugin config 配置,详见 [Plugin config](architecture-design/plugin-config.md) | |
| name | 可选 | 辅助 | 标识路由名称 | route-xxxx |
Expand Down Expand Up @@ -118,6 +119,11 @@ route 对象 json 配置内容:
"vars": [["http_user", "==", "ios"]], # 由一个或多个 [var, operator, val] 元素组成的列表
"upstream_id": "1", # upstream 对象在 etcd 中的 id ,建议使用此值
"upstream": {}, # upstream 信息对象,建议尽量不要使用
"upstream_timeout": { # 为 route 设置 upstream 的连接、发送消息、接收消息的超时时间。
"connect": 3,
"send": 3,
"read": 3
},
"filter_func": "", # 用户自定义的过滤函数,非必填
}
```
Expand Down
37 changes: 37 additions & 0 deletions t/admin/routes2.t
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,40 @@ GET /t
{"error_msg":"failed to fetch plugin config info by plugin config id [not_found], response code: 404"}
--- no_error_log
[error]



=== TEST 18: valid route with upstream_timeout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need further test cases to cover the usage of upstream_timeout.

--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"methods": ["GET"],
"upstream": {
"nodes": {
"127.0.0.1:8080": 1
},
"type": "roundrobin"
},
"upstream_timeout": {
"connect": 3,
"send": 3,
"read": 3
},
"uri": "/index.html"
}]]
)

ngx.status = code
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]