Copilot commented on code in PR #13212:
URL: https://github.com/apache/apisix/pull/13212#discussion_r3071590032
##########
t/plugin/limit-count5.t:
##########
@@ -253,3 +253,55 @@ 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)
+ local uri = ngx.var.uri
+ local conf = {
+ time_window = 60,
+ count = 5,
+ }
+
+ -- peek with cost=0, should show remaining=5 (full quota)
+ local delay, remaining = lim:incoming(uri, true, conf, 0)
+ ngx.say("peek1: ", remaining)
+
+ -- another peek should still show remaining=5
+ local delay, remaining = lim:incoming(uri, true, conf, 0)
+ ngx.say("peek2: ", remaining)
+
+ -- commit with cost=2
+ local delay, remaining = lim:incoming(uri, true, conf, 2)
+ ngx.say("commit1: ", remaining)
+
+ -- peek again should show remaining=3
+ local delay, remaining = lim:incoming(uri, true, conf, 0)
+ ngx.say("peek3: ", remaining)
+
+ -- commit with cost=3 (exhausts quota)
+ local delay, remaining = lim:incoming(uri, true, conf, 3)
+ ngx.say("commit2: ", remaining)
+
+ -- peek on exhausted quota should show remaining=0
+ local delay, remaining = lim:incoming(uri, true, conf, 0)
+ ngx.say("peek4: ", remaining)
Review Comment:
TEST 8 is meant to validate the dry-run "peek" behavior, but the peek calls
pass `commit=true` (e.g. `lim:incoming(uri, true, conf, 0)`). With
`commit=true`, the limiter can still create/update state (even when `cost=0`),
so this test won’t actually exercise the `commit=false` peek path used by
`dry_run` in `apisix/plugins/limit-count/init.lua`. Update the peek calls
(peek1/peek2/peek3/peek4) to use `commit=false` so the test catches
side-effect-free peek regressions.
##########
apisix/plugins/limit-count/limit-count-local.lua:
##########
@@ -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
Review Comment:
In dry-run mode you call `lim:incoming(..., commit=false, cost=0)` to avoid
side effects, but `limit-count-local.incoming()` will still call
`set_endtime()` whenever `remaining_or_err == self.limit - cost`. With
`commit=false` and `cost=0` on a new key this condition is true, so the peek
will write into the `*-reset-header` shared dict (creating entries and
affecting `reset`), which defeats the "side-effect-free peek" goal and can
cause unnecessary dict growth under high-cardinality keys. Consider guarding
the `set_endtime()` path with `commit == true` (or otherwise skipping
reset-header writes when peeking).
--
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]