You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@apisix.apache.org by vi...@apache.org on 2020/10/30 12:51:51 UTC

[apisix] branch master updated: feat: implement `array_find` in `apisix.core.table` (#2564)

This is an automated email from the ASF dual-hosted git repository.

vinci pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 45216a2  feat: implement `array_find` in `apisix.core.table` (#2564)
45216a2 is described below

commit 45216a25f2dc07949c963d989d21d3e3a8dd6270
Author: 罗泽轩 <sp...@gmail.com>
AuthorDate: Fri Oct 30 20:51:44 2020 +0800

    feat: implement `array_find` in `apisix.core.table` (#2564)
    
    Close #2486
---
 apisix/core/table.lua          | 12 ++++++++++++
 apisix/plugins/api-breaker.lua | 19 ++++---------------
 2 files changed, 16 insertions(+), 15 deletions(-)

diff --git a/apisix/core/table.lua b/apisix/core/table.lua
index 4ad92ca..6b77b42 100644
--- a/apisix/core/table.lua
+++ b/apisix/core/table.lua
@@ -20,6 +20,7 @@ local setmetatable = setmetatable
 local select       = select
 local new_tab      = require("table.new")
 local nkeys        = require("table.nkeys")
+local ipairs       = ipairs
 local pairs        = pairs
 local type         = type
 local ngx_re       = require("ngx.re")
@@ -59,6 +60,17 @@ function _M.set(tab, ...)
 end
 
 
+function _M.array_find(array, val)
+    for i, v in ipairs(array) do
+        if v == val then
+            return i
+        end
+    end
+
+    return nil
+end
+
+
 -- only work under lua51 or luajit
 function _M.setmt__gc(t, mt)
     local prox = newproxy(true)
diff --git a/apisix/plugins/api-breaker.lua b/apisix/plugins/api-breaker.lua
index d97577d..f144a06 100644
--- a/apisix/plugins/api-breaker.lua
+++ b/apisix/plugins/api-breaker.lua
@@ -20,7 +20,6 @@ local plugin_name = "api-breaker"
 local ngx = ngx
 local math = math
 local error = error
-local ipairs = ipairs
 
 local shared_buffer = ngx.shared['plugin-'.. plugin_name]
 if not shared_buffer then
@@ -90,18 +89,6 @@ local schema = {
 }
 
 
--- todo: we can move this into `core.talbe`
-local function array_find(array, val)
-    for i, v in ipairs(array) do
-        if v == val then
-            return i
-        end
-    end
-
-    return nil
-end
-
-
 local function gen_healthy_key(ctx)
     return "healthy-" .. core.request.get_host(ctx) .. ctx.var.uri
 end
@@ -188,7 +175,9 @@ function _M.log(conf, ctx)
     end
 
     -- unhealth process
-    if array_find(conf.unhealthy.http_statuses, upstream_status) then
+    if core.table.array_find(conf.unhealthy.http_statuses,
+                             upstream_status)
+    then
         local unhealthy_count, err = shared_buffer:incr(unhealthy_key, 1, 0)
         if err then
             core.log.warn("failed to incr unhealthy_key: ", unhealthy_key,
@@ -212,7 +201,7 @@ function _M.log(conf, ctx)
     end
 
     -- health process
-    if not array_find(conf.healthy.http_statuses, upstream_status) then
+    if not core.table.array_find(conf.healthy.http_statuses, upstream_status) then
         return
     end