shreemaan-abhishek commented on code in PR #12872:
URL: https://github.com/apache/apisix/pull/12872#discussion_r2727073505
##########
apisix/plugins/limit-conn/util.lua:
##########
@@ -18,36 +18,76 @@
local assert = assert
local math = require "math"
local floor = math.floor
+local ngx = ngx
+local ngx_time = ngx.time
+local uuid = require("resty.jit-uuid")
+local core = require("apisix.core")
+local hex_encode = require("resty.string").to_hex
+
local _M = {version = 0.3}
+local redis_incoming_script = core.string.compress_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}
+]=])
+local redis_incoming_script_sha =
hex_encode(ngx.sha1_bin(redis_incoming_script))
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
+ local conn
if commit then
- conn, err = red:incrby(key, 1)
- if not conn 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
+
+ local now = ngx_time()
+ local res, err = red:evalsha(redis_incoming_script_sha, 1, key,
Review Comment:
got it, I have fixed this too now.
FYI, `lua-resty-redis-cluster` didn't work with `evalsha` so I ensured a
conditional usage of plain redis eval by rediscluster policy and evalsha usage
by `redis` policy.
--
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]