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



##########
File path: apisix/plugins/api-breaker.lua
##########
@@ -0,0 +1,221 @@
+--
+-- 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 = 300 -- TODO: user can config
+
+local shared_buffer = ngx.shared['plugin-'.. plugin_name]
+if not shared_buffer then
+    error("failed to get ngx.shared dict when load plugin " .. plugin_name)
+end
+
+
+local schema = {
+    type = "object",
+    properties = {
+        unhealthy_response_code = {
+            type = "integer",
+            minimum = 200,

Review comment:
       why return 2xx for unhealthy response?

##########
File path: apisix/plugins/api-breaker.lua
##########
@@ -0,0 +1,221 @@
+--
+-- 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 = 300 -- TODO: user can config
+
+local shared_buffer = ngx.shared['plugin-'.. plugin_name]
+if not shared_buffer then
+    error("failed to get ngx.shared dict when load plugin " .. plugin_name)
+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_status)
+    for _, unhealthy in ipairs(unhealthy_status) do
+        if unhealthy == upstream_status then
+            return true
+        end
+    end
+
+    return false
+end
+
+
+local function is_healthy(healthy_status, upstream_status)
+    for _, healthy in ipairs(healthy_status) do
+        if healthy == upstream_status then
+            return true
+        end
+    end
+
+    return false
+end
+

Review comment:
       Can be changed to a function, element in array

##########
File path: apisix/plugins/api-breaker.lua
##########
@@ -0,0 +1,221 @@
+--
+-- 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 = 300 -- TODO: user can config
+
+local shared_buffer = ngx.shared['plugin-'.. plugin_name]
+if not shared_buffer then
+    error("failed to get ngx.shared dict when load plugin " .. plugin_name)
+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_status)
+    for _, unhealthy in ipairs(unhealthy_status) do
+        if unhealthy == upstream_status then
+            return true
+        end
+    end
+
+    return false
+end
+
+
+local function is_healthy(healthy_status, upstream_status)
+    for _, healthy in ipairs(healthy_status) do
+        if healthy == upstream_status 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("failed to get unhealthy_cache_key in ngx.shared:", err)

Review comment:
       do we need error level log?
   And we need to log the value of `unhealthy_cache_key(ctx)` for debug.

##########
File path: apisix/plugins/api-breaker.lua
##########
@@ -0,0 +1,221 @@
+--
+-- 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 = 300 -- TODO: user can config
+
+local shared_buffer = ngx.shared['plugin-'.. plugin_name]
+if not shared_buffer then
+    error("failed to get ngx.shared dict when load plugin " .. plugin_name)
+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_status)
+    for _, unhealthy in ipairs(unhealthy_status) do
+        if unhealthy == upstream_status then
+            return true
+        end
+    end
+
+    return false
+end
+
+
+local function is_healthy(healthy_status, upstream_status)
+    for _, healthy in ipairs(healthy_status) do
+        if healthy == upstream_status 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("failed to get unhealthy_cache_key in ngx.shared:", err)
+    end
+
+    local unhealthy_lastime, err = 
shared_buffer:get(unhealthy_lastime_cache_key(ctx))
+    if err then
+        core.log.error("failed to get unhealthy_lastime_cache_key in 
ngx.shared: ", 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
+
+        -- The maximum intercept request is 5 minutes(DEFAULT_EXPTIME),
+        -- and then the upstream service will be retry.
+        if unhealthy_lastime + 2^ride >= ngx.time() then
+            return conf.unhealthy_response_code
+        end
+    end
+end
+
+
+function _M.log(conf, ctx)
+    local unhealthy_status = conf.unhealthy.http_statuses
+    local healthy_status = conf.healthy.http_statuses
+
+    local unhealthy_key = unhealthy_cache_key(ctx)
+    local healthy_key = healthy_cache_key(ctx)
+
+    local upstream_status = core.response.get_upstream_status(ctx)
+
+    if is_unhealthy(unhealthy_status, upstream_status) then
+        local newval, err = shared_buffer:incr(unhealthy_key, 1, 0, 
DEFAULT_EXPTIME)

Review comment:
       why we need the exptime? I am confused.
   the value of `DEFAULT_EXPTIME` should be the max sleep time, right?

##########
File path: apisix/plugins/api-breaker.lua
##########
@@ -0,0 +1,221 @@
+--
+-- 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 = 300 -- TODO: user can config
+
+local shared_buffer = ngx.shared['plugin-'.. plugin_name]
+if not shared_buffer then
+    error("failed to get ngx.shared dict when load plugin " .. plugin_name)
+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_status)
+    for _, unhealthy in ipairs(unhealthy_status) do
+        if unhealthy == upstream_status then
+            return true
+        end
+    end
+
+    return false
+end
+
+
+local function is_healthy(healthy_status, upstream_status)
+    for _, healthy in ipairs(healthy_status) do
+        if healthy == upstream_status 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

Review comment:
       ditto

##########
File path: apisix/plugins/api-breaker.lua
##########
@@ -0,0 +1,221 @@
+--
+-- 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 = 300 -- TODO: user can config
+
+local shared_buffer = ngx.shared['plugin-'.. plugin_name]
+if not shared_buffer then
+    error("failed to get ngx.shared dict when load plugin " .. plugin_name)
+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_status)
+    for _, unhealthy in ipairs(unhealthy_status) do
+        if unhealthy == upstream_status then
+            return true
+        end
+    end
+
+    return false
+end
+
+
+local function is_healthy(healthy_status, upstream_status)
+    for _, healthy in ipairs(healthy_status) do
+        if healthy == upstream_status 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("failed to get unhealthy_cache_key in ngx.shared:", err)
+    end
+
+    local unhealthy_lastime, err = 
shared_buffer:get(unhealthy_lastime_cache_key(ctx))
+    if err then
+        core.log.error("failed to get unhealthy_lastime_cache_key in 
ngx.shared: ", 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
+
+        -- The maximum intercept request is 5 minutes(DEFAULT_EXPTIME),
+        -- and then the upstream service will be retry.
+        if unhealthy_lastime + 2^ride >= ngx.time() then
+            return conf.unhealthy_response_code
+        end
+    end
+end
+
+
+function _M.log(conf, ctx)
+    local unhealthy_status = conf.unhealthy.http_statuses
+    local healthy_status = conf.healthy.http_statuses
+
+    local unhealthy_key = unhealthy_cache_key(ctx)
+    local healthy_key = healthy_cache_key(ctx)
+
+    local upstream_status = core.response.get_upstream_status(ctx)
+
+    if is_unhealthy(unhealthy_status, upstream_status) then
+        local newval, err = shared_buffer:incr(unhealthy_key, 1, 0, 
DEFAULT_EXPTIME)
+        if err then
+            core.log.error("failed to incr unhealthy_key in ngx.shared: ", err)

Review comment:
       we shoulf log the vaule of `unhealthy_key`

##########
File path: apisix/plugins/api-breaker.lua
##########
@@ -0,0 +1,221 @@
+--
+-- 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 = 300 -- TODO: user can config
+
+local shared_buffer = ngx.shared['plugin-'.. plugin_name]
+if not shared_buffer then
+    error("failed to get ngx.shared dict when load plugin " .. plugin_name)
+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_status)
+    for _, unhealthy in ipairs(unhealthy_status) do
+        if unhealthy == upstream_status then
+            return true
+        end
+    end
+
+    return false
+end
+
+
+local function is_healthy(healthy_status, upstream_status)
+    for _, healthy in ipairs(healthy_status) do
+        if healthy == upstream_status 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("failed to get unhealthy_cache_key in ngx.shared:", err)
+    end
+
+    local unhealthy_lastime, err = 
shared_buffer:get(unhealthy_lastime_cache_key(ctx))
+    if err then
+        core.log.error("failed to get unhealthy_lastime_cache_key in 
ngx.shared: ", err)
+    end
+
+    if unhealthy_val and unhealthy_lastime then
+        local ride = math.ceil(unhealthy_val / conf.unhealthy.failures)

Review comment:
       what's `ride` mean?

##########
File path: apisix/plugins/api-breaker.lua
##########
@@ -0,0 +1,221 @@
+--
+-- 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 = 300 -- TODO: user can config
+
+local shared_buffer = ngx.shared['plugin-'.. plugin_name]
+if not shared_buffer then
+    error("failed to get ngx.shared dict when load plugin " .. plugin_name)
+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_status)
+    for _, unhealthy in ipairs(unhealthy_status) do
+        if unhealthy == upstream_status then
+            return true
+        end
+    end
+
+    return false
+end
+
+
+local function is_healthy(healthy_status, upstream_status)
+    for _, healthy in ipairs(healthy_status) do
+        if healthy == upstream_status 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("failed to get unhealthy_cache_key in ngx.shared:", err)
+    end
+
+    local unhealthy_lastime, err = 
shared_buffer:get(unhealthy_lastime_cache_key(ctx))
+    if err then
+        core.log.error("failed to get unhealthy_lastime_cache_key in 
ngx.shared: ", err)

Review comment:
       ditto

##########
File path: apisix/plugins/api-breaker.lua
##########
@@ -0,0 +1,221 @@
+--
+-- 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 = 300 -- TODO: user can config
+
+local shared_buffer = ngx.shared['plugin-'.. plugin_name]
+if not shared_buffer then
+    error("failed to get ngx.shared dict when load plugin " .. plugin_name)
+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_status)
+    for _, unhealthy in ipairs(unhealthy_status) do
+        if unhealthy == upstream_status then
+            return true
+        end
+    end
+
+    return false
+end
+
+
+local function is_healthy(healthy_status, upstream_status)
+    for _, healthy in ipairs(healthy_status) do
+        if healthy == upstream_status 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("failed to get unhealthy_cache_key in ngx.shared:", err)
+    end
+
+    local unhealthy_lastime, err = 
shared_buffer:get(unhealthy_lastime_cache_key(ctx))
+    if err then
+        core.log.error("failed to get unhealthy_lastime_cache_key in 
ngx.shared: ", 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
+
+        -- The maximum intercept request is 5 minutes(DEFAULT_EXPTIME),
+        -- and then the upstream service will be retry.
+        if unhealthy_lastime + 2^ride >= ngx.time() then

Review comment:
       The logic here is hard to understand, what does it mean?

##########
File path: doc/plugins/api-breaker.md
##########
@@ -0,0 +1,106 @@
+<!--
+#
+# 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.
+
+About the breaker timeout logic; the current version does not open the 
relevant configuration items to the user, the code logic automatically 
**triggers the unhealthy state **incrementation of the number of operations.
+
+For example, if the upstream service returns `unhealthy.http_statuses`   state 
and reaches `unhealthy.failures` for the first time, **the breaker will take 2 
seconds**. Then, after 2 seconds, the upstream service returns 
`unhealthy.http_statuses` continuously again and reaches to 
`unhealthy.failures` a second time,  **breaker for 4 seconds**. And so on, up 
to a maximum of 300 seconds is not increased.

Review comment:
       NO  test cases to cover AT ALL.

##########
File path: t/plugin/api-breaker.t
##########
@@ -0,0 +1,219 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+log_level('info');
+run_tests;
+
+__DATA__
+
+=== TEST 1: sanity
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.api-breaker")
+            local ok, err = plugin.check_schema({
+                unhealthy_response_code = 502,
+                unhealthy = {
+                    http_statuses = {500},
+                    failures = 1,
+                },
+                healthy = {
+                    http_statuses = {200},
+                    successes = 1,
+                },
+            })
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log
+[error]
+
+
+=== TEST 2: default http_statuses
+--- config
+    location /t {
+        content_by_lua_block {
+            local plugin = require("apisix.plugins.api-breaker")
+            local ok, err = plugin.check_schema({
+                unhealthy_response_code = 502,
+                unhealthy = {
+                    failures = 1,
+                },
+                healthy = {
+                    successes = 1,
+                },
+            })
+            if not ok then
+                ngx.say(err)
+            end
+
+            ngx.say("done")
+        }
+    }
+--- request
+GET /t
+--- response_body
+done
+--- no_error_log
+[error]
+
+
+=== TEST 3: add plugin
+--- 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,
+                [[{
+                    "plugins": {
+                        "api-breaker": {
+                            "unhealthy_response_code": 502,
+                            "unhealthy": {
+                                "http_statuses": [500, 503],
+                                "failures": 3
+                            },
+                            "healthy": {
+                                "http_statuses": [200, 206],
+                                "successes": 3
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/api_breaker"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+=== TEST 4: trigger breaker
+--- request eval
+["GET /api_breaker?code=200", "GET /api_breaker?code=500", "GET 
/api_breaker?code=503", "GET /api_breaker?code=500", "GET 
/api_breaker?code=500", "GET /api_breaker?code=500"]
+--- error_code eval
+[200, 500, 503, 500, 502, 502]
+--- no_error_log
+[error]
+
+
+=== TEST 5: trigger reset status
+--- request eval
+["GET /api_breaker?code=500", "GET /api_breaker?code=500", "GET 
/api_breaker?code=200", "GET /api_breaker?code=200", "GET 
/api_breaker?code=200", "GET /api_breaker?code=500", "GET 
/api_breaker?code=500"]
+--- error_code eval
+[500, 500, 200, 200, 200, 500, 500]
+--- no_error_log
+[error]
+
+
+=== TEST 6: trigger del healthy numeration
+--- request eval
+["GET /api_breaker?code=500", "GET /api_breaker?code=200", "GET 
/api_breaker?code=500", "GET /api_breaker?code=500", "GET 
/api_breaker?code=500", "GET /api_breaker?code=500", "GET 
/api_breaker?code=500"]
+--- error_code eval
+[500, 200, 500, 500, 502, 502, 502]
+--- no_error_log
+[error]
+
+
+=== TEST 7: add plugin with default config value
+--- 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,
+                [[{
+                    "plugins": {
+                        "api-breaker": {
+                            "unhealthy_response_code": 502,
+                            "unhealthy": {
+                                "failures": 3
+                            },
+                            "healthy": {
+                                "successes": 3
+                            }
+                        }
+                    },
+                    "upstream": {
+                        "nodes": {
+                            "127.0.0.1:1980": 1
+                        },
+                        "type": "roundrobin"
+                    },
+                    "uri": "/api_breaker"
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+=== TEST 8: default value

Review comment:
       why we need this test case?

##########
File path: t/plugin/api-breaker.t
##########
@@ -0,0 +1,219 @@
+#
+# 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.
+#
+use t::APISIX 'no_plan';
+
+$ENV{TEST_NGINX_HTML_DIR} ||= html_dir();
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+log_level('info');
+run_tests;
+
+__DATA__

Review comment:
       No test cases to cover abnormal situation




----------------------------------------------------------------
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:
[email protected]


Reply via email to