This is an automated email from the ASF dual-hosted git repository.
nic443 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 64277883e feat(limit-count): upgrade lua-resty-limit-traffic to v1.2.0
(#13212)
64277883e is described below
commit 64277883e22606781e30daf0969b52363bf739a9
Author: Nic <[email protected]>
AuthorDate: Tue Apr 14 10:15:27 2026 +0800
feat(limit-count): upgrade lua-resty-limit-traffic to v1.2.0 (#13212)
---
.requirements | 2 +-
apisix/plugins/limit-count/init.lua | 12 +++++-
apisix/plugins/limit-count/limit-count-local.lua | 12 ++++--
t/plugin/ai-rate-limiting-expression.t | 20 ++++-----
t/plugin/ai-rate-limiting.t | 26 ++++++------
t/plugin/limit-count5.t | 54 ++++++++++++++++++++++++
6 files changed, 98 insertions(+), 28 deletions(-)
diff --git a/.requirements b/.requirements
index a2b0f88c8..952e8c6d4 100644
--- a/.requirements
+++ b/.requirements
@@ -17,5 +17,5 @@
APISIX_PACKAGE_NAME=apisix
-APISIX_RUNTIME=1.3.3
+APISIX_RUNTIME=1.3.4
APISIX_DASHBOARD_COMMIT=bbe05ad95396f90db03d831660f454c2844d906e
diff --git a/apisix/plugins/limit-count/init.lua
b/apisix/plugins/limit-count/init.lua
index 964876e90..ea1d0158e 100644
--- a/apisix/plugins/limit-count/init.lua
+++ b/apisix/plugins/limit-count/init.lua
@@ -400,7 +400,17 @@ local function run_rate_limit(conf, rule, ctx, name, cost,
dry_run)
local delay, remaining, reset
if not conf.policy or conf.policy == "local" then
- delay, remaining, reset = lim:incoming(key, not dry_run, conf, cost)
+ if dry_run then
+ -- peek with cost=0 and commit=false to avoid side effects:
+ -- dict:get reads without creating or incrementing the key
+ delay, remaining, reset = lim:incoming(key, false, conf, 0)
+ if type(remaining) == "number" and remaining - cost < 0 then
+ delay = nil
+ remaining = "rejected"
+ end
+ else
+ delay, remaining, reset = lim:incoming(key, true, conf, cost)
+ end
else
delay, remaining, reset = lim:incoming(key, cost)
end
diff --git a/apisix/plugins/limit-count/limit-count-local.lua
b/apisix/plugins/limit-count/limit-count-local.lua
index 9ad33d89c..87d1059d7 100644
--- a/apisix/plugins/limit-count/limit-count-local.lua
+++ b/apisix/plugins/limit-count/limit-count-local.lua
@@ -17,6 +17,7 @@
local limit_count = require("resty.limit.count")
local ngx = ngx
+local type = type
local ngx_time = ngx.time
local assert = assert
local setmetatable = setmetatable
@@ -66,16 +67,21 @@ function _M.new(plugin_name, limit, window)
end
function _M.incoming(self, key, commit, conf, cost)
- local delay, remaining = self.limit_count:incoming(key, commit, cost)
+ local delay, consumed_or_err = self.limit_count:incoming(key, commit, cost)
local reset
- if remaining == self.limit - cost then
+ local remaining_or_err = consumed_or_err
+ if type(consumed_or_err) == "number" then
+ remaining_or_err = self.limit - consumed_or_err
+ end
+
+ if remaining_or_err == self.limit - cost then
reset = set_endtime(self, key, self.window)
else
reset = read_reset(self, key)
end
- return delay, remaining, reset
+ return delay, remaining_or_err, reset
end
return _M
diff --git a/t/plugin/ai-rate-limiting-expression.t
b/t/plugin/ai-rate-limiting-expression.t
index a0f818406..8b47b84cb 100644
--- a/t/plugin/ai-rate-limiting-expression.t
+++ b/t/plugin/ai-rate-limiting-expression.t
@@ -271,8 +271,8 @@ passed
]
--- response_headers_like eval
[
- "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 499",
- "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 319",
+ "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 500",
+ "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 320",
]
--- no_error_log
[error]
@@ -340,8 +340,8 @@ passed
]
--- response_headers_like eval
[
- "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 499",
- "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 319",
+ "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 500",
+ "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 320",
]
--- no_error_log
[error]
@@ -475,8 +475,8 @@ passed
]
--- response_headers_like eval
[
- "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 999",
- "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 774",
+ "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 1000",
+ "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 775",
]
--- no_error_log
[error]
@@ -544,8 +544,8 @@ passed
]
--- response_headers_like eval
[
- "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 499",
- "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 419",
+ "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 500",
+ "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 420",
]
--- no_error_log
[error]
@@ -613,8 +613,8 @@ passed
]
--- response_headers_like eval
[
- "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 99",
- "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 99",
+ "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 100",
+ "X-AI-RateLimit-Remaining-ai-proxy-anthropic: 100",
]
--- no_error_log
[error]
diff --git a/t/plugin/ai-rate-limiting.t b/t/plugin/ai-rate-limiting.t
index 21283e94a..d8e10d437 100644
--- a/t/plugin/ai-rate-limiting.t
+++ b/t/plugin/ai-rate-limiting.t
@@ -408,7 +408,7 @@ POST /ai
Authorization: Bearer token
--- response_headers
X-AI-RateLimit-Limit-ai-proxy-openai: 30
-X-AI-RateLimit-Remaining-ai-proxy-openai: 29
+X-AI-RateLimit-Remaining-ai-proxy-openai: 30
X-AI-RateLimit-Reset-ai-proxy-openai: 60
@@ -427,9 +427,9 @@ Authorization: Bearer token
[200, 200, 200, 403]
--- response_headers eval
[
- "X-AI-RateLimit-Remaining-ai-proxy-openai: 29",
- "X-AI-RateLimit-Remaining-ai-proxy-openai: 19",
- "X-AI-RateLimit-Remaining-ai-proxy-openai: 9",
+ "X-AI-RateLimit-Remaining-ai-proxy-openai: 30",
+ "X-AI-RateLimit-Remaining-ai-proxy-openai: 20",
+ "X-AI-RateLimit-Remaining-ai-proxy-openai: 10",
"X-AI-RateLimit-Remaining-ai-proxy-openai: 0",
]
@@ -512,7 +512,7 @@ POST /ai2
Authorization: Bearer token
--- response_headers
X-AI-RateLimit-Limit-ai-proxy-openai: 20
-X-AI-RateLimit-Remaining-ai-proxy-openai: 19
+X-AI-RateLimit-Remaining-ai-proxy-openai: 20
X-AI-RateLimit-Reset-ai-proxy-openai: 45
@@ -532,10 +532,10 @@ Authorization: Bearer token
[200, 200, 200, 200, 503]
--- response_headers eval
[
- "X-AI-RateLimit-Remaining-ai-proxy-openai: 19",
- "X-AI-RateLimit-Remaining-ai-proxy-openai: 14",
- "X-AI-RateLimit-Remaining-ai-proxy-openai: 9",
- "X-AI-RateLimit-Remaining-ai-proxy-openai: 4",
+ "X-AI-RateLimit-Remaining-ai-proxy-openai: 20",
+ "X-AI-RateLimit-Remaining-ai-proxy-openai: 15",
+ "X-AI-RateLimit-Remaining-ai-proxy-openai: 10",
+ "X-AI-RateLimit-Remaining-ai-proxy-openai: 5",
"X-AI-RateLimit-Remaining-ai-proxy-openai: 0",
]
@@ -1450,7 +1450,7 @@ POST /ai
user: jack
--- response_headers
X-AI-1-RateLimit-Limit: 20
-X-AI-1-RateLimit-Remaining: 19
+X-AI-1-RateLimit-Remaining: 20
X-AI-1-RateLimit-Reset: 10
@@ -1463,7 +1463,7 @@ POST /ai
project: apisix
--- response_headers
X-AI-2-RateLimit-Limit: 30
-X-AI-2-RateLimit-Remaining: 29
+X-AI-2-RateLimit-Remaining: 30
X-AI-2-RateLimit-Reset: 10
@@ -1544,7 +1544,7 @@ POST /ai
user: jack
--- response_headers
X-AI-User-RateLimit-Limit: 20
-X-AI-User-RateLimit-Remaining: 19
+X-AI-User-RateLimit-Remaining: 20
X-AI-User-RateLimit-Reset: 10
@@ -1557,5 +1557,5 @@ POST /ai
project: apisix
--- response_headers
X-AI-Project-RateLimit-Limit: 30
-X-AI-Project-RateLimit-Remaining: 29
+X-AI-Project-RateLimit-Remaining: 30
X-AI-Project-RateLimit-Reset: 10
diff --git a/t/plugin/limit-count5.t b/t/plugin/limit-count5.t
index eeb2b10e5..15a3e0ba4 100644
--- a/t/plugin/limit-count5.t
+++ b/t/plugin/limit-count5.t
@@ -253,3 +253,57 @@ nginx_config:
--- error_code: 200
--- access_log eval
qr/\{\\x22rate_limiting_key\\x22:\\x22\/apisix\/routes\/1:\d+:test\.com\\x22,\\x22rate_limiting_limit\\x22:2,\\x22rate_limiting_remaining\\x22:1,\\x22rate_limiting_reset\\x22:10}/
+
+
+
+=== TEST 8: cost=0 peek does not consume quota
+--- config
+ location = /t {
+ content_by_lua_block {
+ local limit_count_local = require
"apisix.plugins.limit-count.limit-count-local"
+ local lim = limit_count_local.new("plugin-limit-count", 5, 60)
+ -- use a unique key to avoid collision with TEST 1 which also
+ -- uses the plugin-limit-count shared dict with key "/t"
+ local key = "test8-peek-key"
+ local conf = {
+ time_window = 60,
+ count = 5,
+ }
+
+ -- peek with commit=false cost=0 (matches dry_run path in init.lua)
+ local delay, remaining = lim:incoming(key, false, conf, 0)
+ ngx.say("peek1: ", remaining)
+
+ -- another peek should still show remaining=5
+ local delay, remaining = lim:incoming(key, false, conf, 0)
+ ngx.say("peek2: ", remaining)
+
+ -- commit with cost=2
+ local delay, remaining = lim:incoming(key, true, conf, 2)
+ ngx.say("commit1: ", remaining)
+
+ -- peek again should show remaining=3
+ local delay, remaining = lim:incoming(key, false, conf, 0)
+ ngx.say("peek3: ", remaining)
+
+ -- commit with cost=3 (exhausts quota)
+ local delay, remaining = lim:incoming(key, true, conf, 3)
+ ngx.say("commit2: ", remaining)
+
+ -- peek on exhausted quota should show remaining=0
+ local delay, remaining = lim:incoming(key, false, conf, 0)
+ ngx.say("peek4: ", remaining)
+
+ -- commit should be rejected
+ local delay, err = lim:incoming(key, true, conf, 1)
+ ngx.say("commit3: ", err)
+ }
+ }
+--- response_body
+peek1: 5
+peek2: 5
+commit1: 3
+peek3: 3
+commit2: 0
+peek4: 0
+commit3: rejected