membphis commented on a change in pull request #2339:
URL: https://github.com/apache/apisix/pull/2339#discussion_r501548363



##########
File path: conf/config-default-dev.yaml
##########
@@ -0,0 +1,203 @@
+#

Review comment:
       I do not think we need `conf/config-default-dev.yaml` for CI.
   
https://github.com/apache/apisix/pull/2339/checks?check_run_id=1223679517#step:6:310
   
   it seems that you only need to fix the test case `t/admin/plugins.t` .
   
   ```
   Test Summary Report
   -------------------
   t/admin/plugins.t                       (Wstat: 256 Tests: 15 Failed: 1)
     Failed test:  2
     Non-zero exit status: 1

##########
File path: doc/plugins/api-blocker.md
##########
@@ -0,0 +1,102 @@
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+- [中文](../zh-cn/plugins/api-blocker.md)
+
+# Summary
+
+- [**Name**](#name)
+- [**Attributes**](#attributes)
+- [**How To Enable**](#how-to-enable)
+- [**Test Plugin**](#test-plugin)
+- [**Disable Plugin**](#disable-plugin)
+
+## Name
+
+The plugin implements API fuse functionality to help us protect our upstream 
business services.
+
+## Attributes
+
+| Name          | Type          | Requirement | Default | Valid      | 
Description                                                                 |
+| ------------- | ------------- | ----------- | ------- | ---------- | 
--------------------------------------------------------------------------- |
+| unhealthy_response_code           | integer | required |          | [200, 
..., 600] | return error code when unhealthy |
+| unhealthy.http_statuses | array[integer] | required | {500}      | [500, 
..., 599] | Status codes when unhealthy |
+| unhealthy.failures      | integer        | required | 1          | >=1       
      | Number of consecutive error requests that triggered an unhealthy state |
+| healthy.http_statuses   | array[integer] | required | {200, 206} | [200, 
..., 499] | Status codes when healthy |

Review comment:
       doc is wrong. It is not a "required" field, it should be an "optional" 
field. Please fix other similar issues.
   
   
https://github.com/apache/apisix/pull/2339/files#diff-a3aa73270ae0e95e30630eb2b5d825b1R73

##########
File path: doc/zh-cn/plugins/api-blocker.md
##########
@@ -0,0 +1,102 @@
+<!--
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+-->
+
+- [English](../../plugins/api-blocker.md)
+
+# 目录
+
+- [**定义**](#定义)
+- [**属性列表**](#属性列表)
+- [**启用方式**](#启用方式)
+- [**测试插件**](#测试插件)
+- [**禁用插件**](#禁用插件)
+
+## 定义
+
+该插件实现API熔断功能,帮助我们保护上游业务服务。

Review comment:
       need one space between Chinese and English.

##########
File path: apisix/plugins/api-breaker.lua
##########
@@ -0,0 +1,212 @@
+--
+-- Licensed to the Apache Software Foundation (ASF) under one or more
+-- contributor license agreements.  See the NOTICE file distributed with
+-- this work for additional information regarding copyright ownership.
+-- The ASF licenses this file to You under the Apache License, Version 2.0
+-- (the "License"); you may not use this file except in compliance with
+-- the License.  You may obtain a copy of the License at
+--
+--     http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+local plugin_name = "api-breaker"
+local ngx = ngx
+local math = math
+local ipairs = ipairs
+local error = error
+local core = require("apisix.core")
+
+local DEFAULT_EXPTIME = 600
+
+local shared_buffer = ngx.shared['plugin-'.. plugin_name]
+if not shared_buffer then
+    error("get ngx.shared dict error.")
+end
+
+
+local schema = {
+    type = "object",
+    properties = {
+        unhealthy_response_code = {
+            type = "integer",
+            minimum = 200,
+            maximum = 599,
+        },
+        unhealthy = {
+            type = "object",
+            properties = {
+                http_statuses = {
+                    type = "array",
+                    minItems = 1,
+                    items = {
+                        type = "integer",
+                        minimum = 500,
+                        maximum = 599,
+                    },
+                    uniqueItems = true,
+                    default = {500}
+                },
+                failures = {
+                    type = "integer",
+                    minimum = 1,
+                    default = 1,
+                }
+            }
+        },
+        healthy = {
+            type = "object",
+            properties = {
+                http_statuses = {
+                    type = "array",
+                    minItems = 1,
+                    items = {
+                        type = "integer",
+                        minimum = 200,
+                        maximum = 499,
+                    },
+                    uniqueItems = true,
+                    default = {200, 206}
+                },
+                successes = {
+                    type = "integer",
+                    minimum = 1,
+                    default = 1,
+                }
+            }
+        }
+    },
+    required = {"unhealthy_response_code", "unhealthy", "healthy"},
+}
+
+
+local function is_unhealthy(unhealthy_status, upstream_statu)
+    for _, unhealthy in ipairs(unhealthy_status) do
+        if unhealthy == upstream_statu then
+            return true
+        end
+    end
+
+    return false
+end
+
+
+local function is_healthy(healthy_status, upstream_statu)
+    for _, healthy in ipairs(healthy_status) do
+        if healthy == upstream_statu then
+            return true
+        end
+    end
+
+    return false
+end
+
+
+local function healthy_cache_key(ctx)
+    return "healthy-" .. core.request.get_host(ctx) .. ctx.var.uri
+end
+
+
+local function unhealthy_cache_key(ctx)
+    return "unhealthy-" .. core.request.get_host(ctx) .. ctx.var.uri
+end
+
+
+local function unhealthy_lastime_cache_key(ctx)
+    return "unhealthy-lastime" .. core.request.get_host(ctx) .. ctx.var.uri
+end
+
+
+local _M = {
+    version = 0.1,
+    name = plugin_name,
+    priority = 1005,
+    schema = schema,
+}
+
+
+function _M.check_schema(conf)
+    local ok, err = core.schema.check(schema, conf)
+    if not ok then
+        return false, err
+    end
+
+    return true
+end
+
+
+function _M.access(conf, ctx)
+    local unhealthy_val, err = shared_buffer:get(unhealthy_cache_key(ctx))
+    if err then
+        core.log.error("ngx.shared get error", err)
+    end
+
+    local unhealthy_lastime, err = 
shared_buffer:get(unhealthy_lastime_cache_key(ctx))
+    if err then
+        core.log.error("ngx.shared get error", err)
+    end
+
+    if unhealthy_val and unhealthy_lastime then
+        local ride = math.ceil(unhealthy_val / conf.unhealthy.failures)
+        if ride < 1 then
+            ride = 1
+        end
+
+        if unhealthy_lastime + 2^ride >= ngx.time() then

Review comment:
       I think 5 minutes is enough, and we need some comments here for this 
case.
   Or we should allow the user to specify this field is a more friendly way.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to