nic-6443 commented on code in PR #12872:
URL: https://github.com/apache/apisix/pull/12872#discussion_r2710848953


##########
apisix/plugins/limit-conn/util.lua:
##########
@@ -18,36 +18,89 @@
 local assert            = assert
 local math              = require "math"
 local floor             = math.floor
+local ngx               = ngx
+local ngx_time          = ngx.time
+local tonumber          = tonumber
+local uuid              = require("resty.jit-uuid")
+local core              = require("apisix.core")
+
 local _M = {version = 0.3}
+local redis_incoming_script = [[
+    local key = KEYS[1]
+    local limit = tonumber(ARGV[1])
+    local ttl = tonumber(ARGV[2])
+    local now = tonumber(ARGV[3])
+    local req_id = ARGV[4]
+
+    redis.call('ZREMRANGEBYSCORE', key, 0, now)
+
+    local count = redis.call('ZCARD', key)
+    if count >= limit then
+        return {0, count}
+    end
+
+    redis.call('ZADD', key, now + ttl, req_id)
+    redis.call('EXPIRE', key, ttl)
+    return {1, count + 1}
+]]
 
 
 function _M.incoming(self, red, key, commit)
     local max = self.max
     self.committed = false
+    local raw_key = key
     key = "limit_conn" .. ":" .. key
 
     local conn, err
     if commit then
-        conn, err = red:incrby(key, 1)
-        if not conn then
-            return nil, err
-        end
+        if self.conf.key_ttl then
+            local req_id = ngx.ctx.request_id or uuid.generate_v4()
+            if not ngx.ctx.limit_conn_req_ids then
+                ngx.ctx.limit_conn_req_ids = {}
+            end
+            ngx.ctx.limit_conn_req_ids[raw_key] = req_id

Review Comment:
   `limit_conn_req_ids` needs to use an array to record multiple request ids 
because the limit-conn plugin may be executed multiple times in the same 
request, ref: https://github.com/apache/apisix/pull/1994



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to