diff --git a/apisix/plugins/serverless/init.lua b/apisix/plugins/serverless/init.lua index ef3c2225..65ee7e81 100644 --- a/apisix/plugins/serverless/init.lua +++ b/apisix/plugins/serverless/init.lua @@ -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", @@ -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) diff --git a/docs/en/latest/plugins/serverless.md b/docs/en/latest/plugins/serverless.md index 3530cff7..00379e5b 100644 --- a/docs/en/latest/plugins/serverless.md +++ b/docs/en/latest/plugins/serverless.md @@ -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:
@@ -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: diff --git a/docs/zh/latest/plugins/serverless.md b/docs/zh/latest/plugins/serverless.md index 9ad6bbad..3c926ef2 100644 --- a/docs/zh/latest/plugins/serverless.md +++ b/docs/zh/latest/plugins/serverless.md @@ -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 代码。比如匿名函数是合法的: @@ -60,6 +60,8 @@ ngx.say(count) 从 `v2.6` 版本开始,我们会把 `conf` 和 `ctx` 作为头两个参数传递给 serverless 函数,就跟一般的插件一样。 +`v2.12.0` 版本之前,`before_proxy` 这个 phase 曾被称作 `balancer`。考虑到这一方法事实上运行在 `access` 之后,代理到上游之前,跟 `balancer` 没有关系,新的命名会更加贴切。 + ## 示例 ### 启动插件 diff --git a/t/admin/schema.t b/t/admin/schema.t index 3c0ddbcf..70e844a4 100644 --- a/t/admin/schema.t +++ b/t/admin/schema.t @@ -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": { @@ -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": { diff --git a/t/plugin/serverless.t b/t/plugin/serverless.t index 3c8163c1..9fef3650 100644 --- a/t/plugin/serverless.t +++ b/t/plugin/serverless.t @@ -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 { @@ -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"] } }, @@ -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 { @@ -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"] } }, @@ -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 { @@ -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": { @@ -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