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

change(serverless): rename "balancer" phase to "before_proxy" #5992

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions apisix/plugins/serverless/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ return function(plugin_name, priority)
type = "string",
default = "access",
enum = {"rewrite", "access", "header_filter", "body_filter",
"log", "balancer"}
"log", "before_proxy"}
},
functions = {
type = "array",
Expand Down Expand Up @@ -114,7 +114,7 @@ return function(plugin_name, priority)
end

function _M.before_proxy(conf, ctx)
call_funcs('balancer', conf, ctx)
call_funcs('before_proxy', conf, ctx)
end

function _M.header_filter(conf, ctx)
Expand Down
4 changes: 3 additions & 1 deletion docs/en/latest/plugins/serverless.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Both plug-ins receive the same parameters.

| Name | Type | Requirement | Default | Valid | Description |
| --------- | ------------- | ----------- | ---------- | ------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| phase | string | optional | ["access"] | ["rewrite", "access", "header_filter", "body_filter", "log", "balancer"] | |
| phase | string | optional | ["access"] | ["rewrite", "access", "header_filter", "body_filter", "log", "before_proxy"] | |
| functions | array[string] | required | | | A list of functions that are specified to run is an array type, which can contain either one function or multiple functions, executed sequentially. |

Note that only function is accepted here, not other types of Lua code. For example, anonymous functions are legal:<br />
Expand Down Expand Up @@ -71,6 +71,8 @@ ngx.say(count)

Since `v2.6`, we pass the `conf` and `ctx` as the first two arguments to the servelss function, like a regular plugin.

Prior to `v2.12.0`, the phase `before_proxy` used to be called `balancer`. Considering that this method actually runs after `access` and before the request goes upstream, and has nothing to do with `balancer`, the new naming would be more appropriate.

## How To Enable

Here's an example, enable the serverless plugin on the specified route:
Expand Down
4 changes: 3 additions & 1 deletion docs/zh/latest/plugins/serverless.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ serverless 的插件有两个,分别是 `serverless-pre-function` 和 `serverl

| 名称 | 类型 | 必选项 | 默认值 | 有效值 | 描述 |
| --------- | ------------- | -------- | ---------- | ------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ |
| phase | string | 可选 | ["access"] | ["rewrite", "access", "header_filter", "body_filter", "log", "balancer"] | |
| phase | string | 可选 | ["access"] | ["rewrite", "access", "header_filter", "body_filter", "log", "before_proxy"] | |
| functions | array[string] | 必须 | | | 指定运行的函数列表,是数组类型,里面可以包含一个函数,也可以是多个函数,按照先后顺序执行。 |

需要注意的是,这里只接受函数,而不接受其他类型的 Lua 代码。比如匿名函数是合法的:
Expand Down Expand Up @@ -60,6 +60,8 @@ ngx.say(count)

从 `v2.6` 版本开始,我们会把 `conf` 和 `ctx` 作为头两个参数传递给 serverless 函数,就跟一般的插件一样。

`v2.12.0` 版本之前,`before_proxy` 这个 phase 曾被称作 `balancer`。考虑到这一方法事实上运行在 `access` 之后,代理到上游之前,跟 `balancer` 没有关系,新的命名会更加贴切。

## 示例

### 启动插件
Expand Down
4 changes: 2 additions & 2 deletions t/admin/schema.t
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ location /t {
[[{
"properties": {
"phase": {
"enum": ["rewrite", "access", "header_filter", "body_filter", "log", "balancer"],
"enum": ["rewrite", "access", "header_filter", "body_filter", "log", "before_proxy"],
"type": "string"
},
"functions": {
Expand Down Expand Up @@ -183,7 +183,7 @@ location /t {
[[{
"properties": {
"phase": {
"enum": ["rewrite", "access", "header_filter", "body_filter", "log", "balancer"],
"enum": ["rewrite", "access", "header_filter", "body_filter", "log", "before_proxy"],
"type": "string"
},
"functions": {
Expand Down
20 changes: 10 additions & 10 deletions t/plugin/serverless.t
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ match uri /hello



=== TEST 23: run in the balancer phase
=== TEST 23: run in the before_proxy phase
--- config
location /t {
content_by_lua_block {
Expand All @@ -692,7 +692,7 @@ match uri /hello
[[{
"plugins": {
"serverless-pre-function": {
"phase": "balancer",
"phase": "before_proxy",
"functions" : ["return function(conf, ctx) ngx.req.set_header('X-SERVERLESS', ctx.balancer_ip) end"]
}
},
Expand Down Expand Up @@ -736,7 +736,7 @@ x-serverless: 127.0.0.1



=== TEST 25: exit in the balancer phase
=== TEST 25: exit in the before_proxy phase
--- config
location /t {
content_by_lua_block {
Expand All @@ -746,7 +746,7 @@ x-serverless: 127.0.0.1
[[{
"plugins": {
"serverless-pre-function": {
"phase": "balancer",
"phase": "before_proxy",
"functions" : ["return function(conf, ctx) ngx.exit(403) end"]
}
},
Expand Down Expand Up @@ -786,7 +786,7 @@ GET /log_request



=== TEST 27: ensure balancer phase run correct time
=== TEST 27: ensure before_proxy phase run correct time
--- config
location /t {
content_by_lua_block {
Expand All @@ -796,8 +796,8 @@ GET /log_request
[[{
"plugins": {
"serverless-pre-function": {
"phase": "balancer",
"functions" : ["return function(conf, ctx) ngx.log(ngx.WARN, 'run balancer phase with ', ctx.balancer_ip) end"]
"phase": "before_proxy",
"functions" : ["return function(conf, ctx) ngx.log(ngx.WARN, 'run before_proxy phase with ', ctx.balancer_ip) end"]
}
},
"upstream": {
Expand Down Expand Up @@ -831,7 +831,7 @@ passed
--- request
GET /log_request
--- grep_error_log eval
qr/(run balancer phase with [\d.]+)/
qr/(run before_proxy phase with [\d.]+)/
--- grep_error_log_out
run balancer phase with 0.0.0.0
run balancer phase with 127.0.0.1
run before_proxy phase with 0.0.0.0
run before_proxy phase with 127.0.0.1