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

membphis 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 31f7445  bugfix: used a table array to store the `status`, allow the 
plugin can (#1994)
31f7445 is described below

commit 31f744580d7026e2e8ce2d2500e3e7af01b295f7
Author: YuanSheng Wang <[email protected]>
AuthorDate: Fri Aug 7 08:15:44 2020 +0800

    bugfix: used a table array to store the `status`, allow the plugin can 
(#1994)
    
    be called multiple times.
    
    fix #1986
---
 apisix/plugins/limit-conn.lua | 28 +++++++++----
 t/plugin/limit-conn.t         | 98 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 118 insertions(+), 8 deletions(-)

diff --git a/apisix/plugins/limit-conn.lua b/apisix/plugins/limit-conn.lua
index 6ca46d5..cf0f31f 100644
--- a/apisix/plugins/limit-conn.lua
+++ b/apisix/plugins/limit-conn.lua
@@ -82,9 +82,11 @@ function _M.access(conf, ctx)
     end
 
     if lim:is_committed() then
-        ctx.limit_conn = lim
-        ctx.limit_conn_key = key
-        ctx.limit_conn_delay = delay
+        if not ctx.limit_conn then
+            ctx.limit_conn = core.tablepool.fetch("plugin#limit-conn", 0, 6)
+        else
+            core.table.insert_tail(ctx.limit_conn, lim, key, delay)
+        end
     end
 
     if delay >= 0.001 then
@@ -94,23 +96,33 @@ end
 
 
 function _M.log(conf, ctx)
-    local lim = ctx.limit_conn
-    if lim then
+    local limit_conn = ctx.limit_conn
+    if not limit_conn then
+        return
+    end
+
+    for i = 1, #limit_conn, 3 do
+        local lim = limit_conn[i]
+        local key = limit_conn[i + 1]
+        local delay = limit_conn[i + 2]
+
         local latency
         if ctx.proxy_passed then
             latency = ctx.var.upstream_response_time
         else
-            latency = ctx.var.request_time - ctx.limit_conn_delay
+            latency = ctx.var.request_time - delay
         end
 
-        local key = ctx.limit_conn_key
         local conn, err = lim:leaving(key, latency)
         if not conn then
             core.log.error("failed to record the connection leaving request: ",
                            err)
-            return
+            break
         end
     end
+
+    core.tablepool.release("plugin#limit-conn", limit_conn)
+    return
 end
 
 
diff --git a/t/plugin/limit-conn.t b/t/plugin/limit-conn.t
index 02f907f..4bc99d4 100644
--- a/t/plugin/limit-conn.t
+++ b/t/plugin/limit-conn.t
@@ -858,3 +858,101 @@ GET /t
 passed
 --- no_error_log
 [error]
+
+
+
+=== TEST 21: set global rule with conn = 2
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/global_rules/1',
+                ngx.HTTP_PUT,
+                [[{
+                    "plugins": {
+                        "limit-conn": {
+                            "conn": 2,
+                            "burst": 1,
+                            "default_conn_delay": 0.1,
+                            "key": "remote_addr"
+                        }
+                    }
+                }]]
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 22: exceeding the burst of global rule
+--- request
+GET /test_concurrency
+--- timeout: 10s
+--- response_body
+200
+200
+200
+503
+503
+503
+503
+503
+503
+503
+--- no_error_log
+[error]
+
+
+
+=== TEST 23: delete global rule
+--- config
+    location /t {
+        content_by_lua_block {
+            local t = require("lib.test_admin").test
+            local code, body = t('/apisix/admin/global_rules/1',
+                ngx.HTTP_DELETE
+                )
+
+            if code >= 300 then
+                ngx.status = code
+            end
+            ngx.say(body)
+        }
+    }
+--- request
+GET /t
+--- response_body
+passed
+--- no_error_log
+[error]
+
+
+
+=== TEST 24: not exceeding the burst
+--- request
+GET /test_concurrency
+--- timeout: 10s
+--- response_body
+200
+200
+200
+200
+200
+200
+200
+200
+200
+200
+--- no_error_log
+[error]

Reply via email to