Skip to content

Commit 58c34f1

Browse files
committed
feat: handling the case of same timestamp
Signed-off-by: Sihyeon Jang <[email protected]>
1 parent 8e6f162 commit 58c34f1

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

apisix/plugins/limit-count/limit-count-redis-cluster.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ local redis_cluster = require("apisix.utils.rediscluster")
1919
local core = require("apisix.core")
2020
local setmetatable = setmetatable
2121
local tostring = tostring
22+
local ngx_var = ngx.var
2223

2324
local _M = {version = 0.2}
2425

@@ -46,6 +47,7 @@ local script_sliding = core.string.compress_script([=[
4647
local window = tonumber(ARGV[2])
4748
local limit = tonumber(ARGV[3])
4849
local cost = tonumber(ARGV[4])
50+
local req_id = ARGV[5]
4951
5052
local window_start = now - window
5153
@@ -66,7 +68,8 @@ local script_sliding = core.string.compress_script([=[
6668
end
6769
6870
for i = 1, cost do
69-
redis.call('ZADD', KEYS[1], now, now .. ':' .. i)
71+
local member = req_id .. ':' .. i
72+
redis.call('ZADD', KEYS[1], now, member)
7073
end
7174
7275
redis.call('PEXPIRE', KEYS[1], window)
@@ -117,8 +120,9 @@ function _M.incoming(self, key, cost)
117120
if self.window_type == "sliding" then
118121
local now = ngx.now() * 1000
119122
local window = self.window * 1000
123+
local req_id = ngx_var.request_id
120124

121-
res, err = red:eval(script_sliding, 1, key, now, window, limit, c)
125+
res, err = red:eval(script_sliding, 1, key, now, window, limit, c, req_id)
122126
else
123127
local window = self.window
124128

apisix/plugins/limit-count/limit-count-redis.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ local core = require("apisix.core")
1919
local assert = assert
2020
local setmetatable = setmetatable
2121
local tostring = tostring
22+
local ngx_var = ngx.var
2223

2324

2425
local _M = {version = 0.4}
@@ -47,6 +48,7 @@ local script_sliding = core.string.compress_script([=[
4748
local window = tonumber(ARGV[2])
4849
local limit = tonumber(ARGV[3])
4950
local cost = tonumber(ARGV[4])
51+
local req_id = ARGV[5]
5052
5153
local window_start = now - window
5254
@@ -68,7 +70,8 @@ local script_sliding = core.string.compress_script([=[
6870
end
6971
7072
for i = 1, cost do
71-
redis.call('ZADD', KEYS[1], now, now .. ':' .. i)
73+
local member = req_id .. ':' .. i
74+
redis.call('ZADD', KEYS[1], now, member)
7275
end
7376
7477
redis.call('PEXPIRE', KEYS[1], window)
@@ -118,8 +121,9 @@ function _M.incoming(self, key, cost)
118121
if self.window_type == "sliding" then
119122
local now = ngx.now() * 1000
120123
local window = self.window * 1000
124+
local req_id = ngx_var.request_id
121125

122-
res, err = red:eval(script_sliding, 1, key, now, window, limit, c)
126+
res, err = red:eval(script_sliding, 1, key, now, window, limit, c, req_id)
123127
else
124128
local window = self.window
125129
res, err = red:eval(script_fixed, 1, key, limit, window, c)

0 commit comments

Comments
 (0)