sihyeonn commented on code in PR #12756:
URL: https://github.com/apache/apisix/pull/12756#discussion_r2957547530
##########
apisix/plugins/limit-count/limit-count-redis-cluster.lua:
##########
@@ -39,7 +40,56 @@ local script = core.string.compress_script([=[
]=])
-function _M.new(plugin_name, limit, window, conf)
+local script_sliding = core.string.compress_script([=[
+ assert(tonumber(ARGV[3]) >= 1, "cost must be at least 1")
+
+ local now = tonumber(ARGV[1])
+ local window = tonumber(ARGV[2])
+ local limit = tonumber(ARGV[3])
+ local cost = tonumber(ARGV[4])
+ local req_id = ARGV[5]
+
+ local window_start = now - window
+
+ redis.call('ZREMRANGEBYSCORE', KEYS[1], 0, window_start)
+
+ local current = redis.call('ZCARD', KEYS[1])
+
+ if current + cost > limit then
+ local earliest = redis.call('ZRANGE', KEYS[1], 0, 0, 'WITHSCORES')
+ local reset = 0
+ if #earliest == 2 then
+ reset = earliest[2] + window - now
+ if reset < 0 then
+ reset = 0
+ end
+ end
+ return {-1, reset}
+ end
+
+ for i = 1, cost do
+ local member = req_id .. ':' .. i
+ redis.call('ZADD', KEYS[1], now, member)
+ end
Review Comment:
Sorry for the late response! Pushed a fix addressing the remaining feedback
— CROSSSLOT prevention with hash tags, TTL unit normalization (ms to seconds),
request_id fallback, docs for approximate_sliding, and test improvements.
Please take a look when you get a chance.
##########
apisix/plugins/limit-count/limit-count-redis.lua:
##########
@@ -74,14 +186,15 @@ function _M.incoming(self, key, cost)
local remaining = res[1]
ttl = res[2]
Review Comment:
Good catch, fixed — sliding/approximate_sliding now convert the reset value
from ms to seconds before returning, so X-RateLimit-Reset is consistent across
all modes.
##########
apisix/plugins/limit-count/limit-count-redis-cluster.lua:
##########
@@ -39,7 +40,101 @@ local script = core.string.compress_script([=[
]=])
-function _M.new(plugin_name, limit, window, conf)
+local script_sliding = core.string.compress_script([=[
+ assert(tonumber(ARGV[3]) >= 1, "cost must be at least 1")
+
+ local now = tonumber(ARGV[1])
+ local window = tonumber(ARGV[2])
+ local limit = tonumber(ARGV[3])
+ local cost = tonumber(ARGV[4])
+ local req_id = ARGV[5]
+
+ local window_start = now - window
+
+ redis.call('ZREMRANGEBYSCORE', KEYS[1], 0, window_start)
+
+ local current = redis.call('ZCARD', KEYS[1])
+
+ if current + cost > limit then
+ local earliest = redis.call('ZRANGE', KEYS[1], 0, 0, 'WITHSCORES')
+ local reset = 0
+ if #earliest == 2 then
+ reset = earliest[2] + window - now
+ if reset < 0 then
+ reset = 0
+ end
+ end
+ return {-1, reset}
+ end
+
+ for i = 1, cost do
+ local member = req_id .. ':' .. i
+ redis.call('ZADD', KEYS[1], now, member)
+ end
+
+ redis.call('PEXPIRE', KEYS[1], window)
+
+ local remaining = limit - (current + cost)
+
+ local earliest = redis.call('ZRANGE', KEYS[1], 0, 0, 'WITHSCORES')
+ local reset = 0
+ if #earliest == 2 then
+ reset = earliest[2] + window - now
+ if reset < 0 then
+ reset = 0
+ end
+ end
+
+ return {remaining, reset}
+]=])
+
+
+local script_approximate_sliding = core.string.compress_script([=[
+ assert(tonumber(ARGV[3]) >= 1, "cost must be at least 1")
+
+ local now = tonumber(ARGV[1])
+ local window = tonumber(ARGV[2])
+ local limit = tonumber(ARGV[3])
+ local cost = tonumber(ARGV[4])
+
+ -- Calculate window IDs
+ local window_id = math.floor(now / window)
+ local prev_window_id = window_id - 1
+
+ -- Get counts from current and previous windows
+ local curr_key = KEYS[1] .. ':' .. window_id
+ local prev_key = KEYS[1] .. ':' .. prev_window_id
+
+ local curr_count = tonumber(redis.call('GET', curr_key) or 0)
+ local prev_count = tonumber(redis.call('GET', prev_key) or 0)
Review Comment:
Fixed — wrapping the base key with hash tags ({KEYS[1]}:window_id) so
derived keys always land in the same slot.
##########
apisix/plugins/limit-count/limit-count-redis.lua:
##########
@@ -59,13 +156,28 @@ function _M.incoming(self, key, cost)
return red, err, 0
end
- local limit = self.limit
- local window = self.window
- local res
key = self.plugin_name .. tostring(key)
local ttl = 0
- res, err = red:eval(script, 1, key, limit, window, cost or 1)
+ local limit = self.limit
+ local c = cost or 1
+ local res
+
+ if self.window_type == "sliding" then
+ local now = ngx.now() * 1000
+ local window = self.window * 1000
+ local req_id = ngx_var.request_id
+
+ res, err = red:eval(script_sliding, 1, key, now, window, limit, c,
req_id)
+ elseif self.window_type == "approximate_sliding" then
+ local now = ngx.now() * 1000
+ local window = self.window * 1000
+
+ res, err = red:eval(script_approximate_sliding, 1, key, now, window,
limit, c)
+ else
Review Comment:
Added a fallback using worker id + timestamp + random number when request_id
isn't available.
##########
docs/en/latest/plugins/limit-count.md:
##########
@@ -46,6 +51,7 @@ You may see the following rate limiting headers in the
response:
| ----------------------- | ------- |
----------------------------------------- | ------------- |
-------------------------------------- |
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
| count | integer | True
| | > 0 | The maximum number of
requests allowed within a given time interval.
|
| time_window | integer | True
| | > 0 | The time interval corresponding
to the rate limiting `count` in seconds.
|
+| window_type | string | False
| fixed | ["fixed","sliding"] | The window behavior type.
`fixed` uses a fixed window algorithm. `sliding` uses a sliding window
algorithm to enforce an exact number of requests per rolling window. `sliding`
is only supported when `policy` is `redis` or `redis-cluster`.
|
Review Comment:
Added — both English and Chinese docs now include approximate_sliding with a
description.
##########
apisix/plugins/limit-count/limit-count-redis.lua:
##########
@@ -40,12 +41,108 @@ local script = core.string.compress_script([=[
]=])
-function _M.new(plugin_name, limit, window, conf)
+local script_sliding = core.string.compress_script([=[
+ assert(tonumber(ARGV[3]) >= 1, "cost must be at least 1")
+
+ local now = tonumber(ARGV[1])
+ local window = tonumber(ARGV[2])
+ local limit = tonumber(ARGV[3])
+ local cost = tonumber(ARGV[4])
Review Comment:
In the fixed script, ARGV order is (limit, window, cost), so ARGV[3] is
actually cost — the assert is correct as-is.
##########
t/plugin/limit-count-redis-sliding.t:
##########
@@ -0,0 +1,501 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ if (!$block->request) {
+ $block->set_value("request", "GET /t");
+ }
+
+ if (!$block->error_log && !$block->no_error_log) {
+ $block->set_value("no_error_log", "[error]\n[alert]");
+ }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: redis policy with sliding window - basic N per window
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/hello",
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 2,
+ "window_type": "sliding",
+ "rejected_code": 503,
+ "key": "remote_addr",
+ "policy": "redis",
+ "redis_host": "127.0.0.1",
+ "redis_port": 6379,
+ "redis_timeout": 1000
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- response_body
+passed
+
+
+=== TEST 2: redis policy with sliding window - enforce N per window
+--- pipelined_requests eval
+["GET /hello", "GET /hello", "GET /hello"]
+--- error_code eval
+[200, 200, 503]
+
+
+=== TEST 3: redis policy with sliding window - remaining header on reject
+--- config
+ location /t {
+ content_by_lua_block {
+ local json = require "t.toolkit.json"
+ local http = require "resty.http"
+ local uri = "http://127.0.0.1:" .. ngx.var.server_port
+ .. "/hello"
+ local ress = {}
+
+ -- ensure previous windows are expired before starting this test
+ ngx.sleep(2.2)
+
+ -- first request: allowed, remaining should be 1
+ do
+ local httpc = http.new()
+ local res, err = httpc:request_uri(uri)
+ if not res then
+ ngx.say(err)
+ return
+ end
+ table.insert(ress, {res.status,
res.headers["X-RateLimit-Remaining"]})
+ end
+
+ -- second request: allowed, remaining should be 0
+ do
+ local httpc = http.new()
+ local res, err = httpc:request_uri(uri)
+ if not res then
+ ngx.say(err)
+ return
+ end
+ table.insert(ress, {res.status,
res.headers["X-RateLimit-Remaining"]})
+ end
+
+ -- third request: rejected, remaining header should stay at 0
+ do
+ local httpc = http.new()
+ local res, err = httpc:request_uri(uri)
+ if not res then
+ ngx.say(err)
+ return
+ end
+ table.insert(ress, {res.status,
res.headers["X-RateLimit-Remaining"]})
+ end
+
+ ngx.say(json.encode(ress))
+ }
+ }
+--- response_body
+[[200,"1"],[200,"0"],[503,"0"]]
+
+
+=== TEST 4: redis policy with sliding window - allow after window passes
+--- config
+ location /t {
+ content_by_lua_block {
+ local json = require "t.toolkit.json"
+ local http = require "resty.http"
+ local uri = "http://127.0.0.1:" .. ngx.var.server_port
+ .. "/hello"
+ local codes = {}
+
+ -- ensure previous windows are expired before starting this test
+ ngx.sleep(2.2)
+
+ -- consume full quota
+ for i = 1, 2 do
+ local httpc = http.new()
+ local res, err = httpc:request_uri(uri)
+ if not res then
+ ngx.say(err)
+ return
+ end
+ table.insert(codes, res.status)
+ end
+
+ -- wait longer than the sliding window (2s)
+ ngx.sleep(2.2)
+
+ -- should be allowed again after window has passed
+ do
+ local httpc = http.new()
+ local res, err = httpc:request_uri(uri)
+ if not res then
+ ngx.say(err)
+ return
+ end
+ table.insert(codes, res.status)
+ end
+
+ ngx.say(json.encode(codes))
+ }
+ }
+--- response_body
+[200,200,200]
+
+
+=== TEST 5: setup route with fixed window for boundary burst comparison
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/2',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/fixed",
+ "plugins": {
+ "limit-count": {
+ "count": 4,
+ "time_window": 4,
+ "window_type": "fixed",
+ "rejected_code": 503,
+ "key": "remote_addr",
+ "policy": "redis",
+ "redis_host": "127.0.0.1",
+ "redis_port": 6379,
+ "redis_timeout": 1000
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- response_body
+passed
+
+
+
+=== TEST 6: setup route with sliding window for boundary burst comparison
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/3',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/sliding",
+ "plugins": {
+ "limit-count": {
+ "count": 4,
+ "time_window": 4,
+ "window_type": "sliding",
+ "rejected_code": 503,
+ "key": "remote_addr",
+ "policy": "redis",
+ "redis_host": "127.0.0.1",
+ "redis_port": 6379,
+ "redis_timeout": 1000
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- response_body
+passed
+
+
+
+=== TEST 7: sliding window - cost parameter support
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/4',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/sliding-cost",
+ "plugins": {
+ "limit-count": {
+ "count": 10,
+ "time_window": 3,
+ "window_type": "sliding",
+ "rejected_code": 503,
+ "key": "remote_addr",
+ "policy": "redis",
+ "redis_host": "127.0.0.1",
+ "redis_port": 6379,
+ "redis_timeout": 1000
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- response_body
+passed
+
+
+
+=== TEST 8: sliding window - verify X-RateLimit headers accuracy
+--- config
+ location /t {
+ content_by_lua_block {
+ local json = require "t.toolkit.json"
+ local http = require "resty.http"
+ local uri = "http://127.0.0.1:" .. ngx.var.server_port
+ .. "/sliding-cost"
+ local results = {}
+
+ -- ensure previous windows are expired
+ ngx.sleep(3.5)
+
+ -- Send requests and check headers
+ for i = 1, 5 do
+ local httpc = http.new()
+ local res, err = httpc:request_uri(uri)
+ if not res then
+ ngx.say("error: " .. err)
+ return
+ end
+
+ local limit = res.headers["X-RateLimit-Limit"]
+ local remaining = res.headers["X-RateLimit-Remaining"]
+ local reset = res.headers["X-RateLimit-Reset"]
+
+ table.insert(results, {
+ req = i,
+ status = res.status,
+ limit = limit,
+ remaining = remaining,
+ has_reset = reset ~= nil
+ })
+ end
+
+ for _, r in ipairs(results) do
+ ngx.say(string.format("req %d: status=%d, limit=%s,
remaining=%s, has_reset=%s",
+ r.req, r.status, r.limit or "nil", r.remaining or "nil",
tostring(r.has_reset)))
+ end
+ }
+ }
+--- response_body_like
+req 1: status=404, limit=10, remaining=9, has_reset=true
+req 2: status=404, limit=10, remaining=8, has_reset=true
+req 3: status=404, limit=10, remaining=7, has_reset=true
+req 4: status=404, limit=10, remaining=6, has_reset=true
+req 5: status=404, limit=10, remaining=5, has_reset=true
+
+
+
+=== TEST 9: verify local policy rejects sliding window
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/5',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/local-sliding",
+ "plugins": {
+ "limit-count": {
+ "count": 10,
+ "time_window": 60,
+ "window_type": "sliding",
+ "rejected_code": 503,
+ "key": "remote_addr",
+ "policy": "local"
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ ngx.say(body)
+ else
+ ngx.say("ERROR: should have been rejected")
+ end
+ }
+ }
+--- error_code: 400
+
+
+
+=== TEST 10: sliding window with redis-cluster policy
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ -- Note: This test requires redis-cluster to be available
+ -- It validates schema but may fail at runtime if cluster
unavailable
+ local code, body = t('/apisix/admin/routes/6',
Review Comment:
Moved Redis Cluster test cases behind a skip_all check that verifies cluster
availability before running.
##########
t/plugin/limit-count-redis-sliding.t:
##########
@@ -0,0 +1,501 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+use t::APISIX 'no_plan';
+
+repeat_each(1);
+no_long_string();
+no_shuffle();
+no_root_location();
+
+add_block_preprocessor(sub {
+ my ($block) = @_;
+
+ if (!$block->request) {
+ $block->set_value("request", "GET /t");
+ }
+
+ if (!$block->error_log && !$block->no_error_log) {
+ $block->set_value("no_error_log", "[error]\n[alert]");
+ }
+});
+
+run_tests;
+
+__DATA__
+
+=== TEST 1: redis policy with sliding window - basic N per window
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/1',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/hello",
+ "plugins": {
+ "limit-count": {
+ "count": 2,
+ "time_window": 2,
+ "window_type": "sliding",
+ "rejected_code": 503,
+ "key": "remote_addr",
+ "policy": "redis",
+ "redis_host": "127.0.0.1",
+ "redis_port": 6379,
+ "redis_timeout": 1000
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- response_body
+passed
+
+
+=== TEST 2: redis policy with sliding window - enforce N per window
+--- pipelined_requests eval
+["GET /hello", "GET /hello", "GET /hello"]
+--- error_code eval
+[200, 200, 503]
+
+
+=== TEST 3: redis policy with sliding window - remaining header on reject
+--- config
+ location /t {
+ content_by_lua_block {
+ local json = require "t.toolkit.json"
+ local http = require "resty.http"
+ local uri = "http://127.0.0.1:" .. ngx.var.server_port
+ .. "/hello"
+ local ress = {}
+
+ -- ensure previous windows are expired before starting this test
+ ngx.sleep(2.2)
+
+ -- first request: allowed, remaining should be 1
+ do
+ local httpc = http.new()
+ local res, err = httpc:request_uri(uri)
+ if not res then
+ ngx.say(err)
+ return
+ end
+ table.insert(ress, {res.status,
res.headers["X-RateLimit-Remaining"]})
+ end
+
+ -- second request: allowed, remaining should be 0
+ do
+ local httpc = http.new()
+ local res, err = httpc:request_uri(uri)
+ if not res then
+ ngx.say(err)
+ return
+ end
+ table.insert(ress, {res.status,
res.headers["X-RateLimit-Remaining"]})
+ end
+
+ -- third request: rejected, remaining header should stay at 0
+ do
+ local httpc = http.new()
+ local res, err = httpc:request_uri(uri)
+ if not res then
+ ngx.say(err)
+ return
+ end
+ table.insert(ress, {res.status,
res.headers["X-RateLimit-Remaining"]})
+ end
+
+ ngx.say(json.encode(ress))
+ }
+ }
+--- response_body
+[[200,"1"],[200,"0"],[503,"0"]]
+
+
+=== TEST 4: redis policy with sliding window - allow after window passes
+--- config
+ location /t {
+ content_by_lua_block {
+ local json = require "t.toolkit.json"
+ local http = require "resty.http"
+ local uri = "http://127.0.0.1:" .. ngx.var.server_port
+ .. "/hello"
+ local codes = {}
+
+ -- ensure previous windows are expired before starting this test
+ ngx.sleep(2.2)
+
+ -- consume full quota
+ for i = 1, 2 do
+ local httpc = http.new()
+ local res, err = httpc:request_uri(uri)
+ if not res then
+ ngx.say(err)
+ return
+ end
+ table.insert(codes, res.status)
+ end
+
+ -- wait longer than the sliding window (2s)
+ ngx.sleep(2.2)
+
+ -- should be allowed again after window has passed
+ do
+ local httpc = http.new()
+ local res, err = httpc:request_uri(uri)
+ if not res then
+ ngx.say(err)
+ return
+ end
+ table.insert(codes, res.status)
+ end
+
+ ngx.say(json.encode(codes))
+ }
+ }
+--- response_body
+[200,200,200]
+
+
+=== TEST 5: setup route with fixed window for boundary burst comparison
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/2',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/fixed",
+ "plugins": {
+ "limit-count": {
+ "count": 4,
+ "time_window": 4,
+ "window_type": "fixed",
+ "rejected_code": 503,
+ "key": "remote_addr",
+ "policy": "redis",
+ "redis_host": "127.0.0.1",
+ "redis_port": 6379,
+ "redis_timeout": 1000
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- response_body
+passed
+
+
+
+=== TEST 6: setup route with sliding window for boundary burst comparison
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/3',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/sliding",
+ "plugins": {
+ "limit-count": {
+ "count": 4,
+ "time_window": 4,
+ "window_type": "sliding",
+ "rejected_code": 503,
+ "key": "remote_addr",
+ "policy": "redis",
+ "redis_host": "127.0.0.1",
+ "redis_port": 6379,
+ "redis_timeout": 1000
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- response_body
+passed
+
+
+
+=== TEST 7: sliding window - cost parameter support
+--- config
+ location /t {
+ content_by_lua_block {
+ local t = require("lib.test_admin").test
+ local code, body = t('/apisix/admin/routes/4',
+ ngx.HTTP_PUT,
+ [[{
+ "uri": "/sliding-cost",
+ "plugins": {
+ "limit-count": {
+ "count": 10,
+ "time_window": 3,
+ "window_type": "sliding",
+ "rejected_code": 503,
+ "key": "remote_addr",
+ "policy": "redis",
+ "redis_host": "127.0.0.1",
+ "redis_port": 6379,
+ "redis_timeout": 1000
+ }
+ },
+ "upstream": {
+ "nodes": {
+ "127.0.0.1:1980": 1
+ },
+ "type": "roundrobin"
+ }
+ }]]
+ )
+
+ if code >= 300 then
+ ngx.status = code
+ end
+ ngx.say(body)
+ }
+ }
+--- response_body
+passed
+
+
+
+=== TEST 8: sliding window - verify X-RateLimit headers accuracy
+--- config
+ location /t {
+ content_by_lua_block {
+ local json = require "t.toolkit.json"
+ local http = require "resty.http"
+ local uri = "http://127.0.0.1:" .. ngx.var.server_port
+ .. "/sliding-cost"
+ local results = {}
+
+ -- ensure previous windows are expired
+ ngx.sleep(3.5)
+
+ -- Send requests and check headers
+ for i = 1, 5 do
+ local httpc = http.new()
+ local res, err = httpc:request_uri(uri)
+ if not res then
+ ngx.say("error: " .. err)
+ return
+ end
+
+ local limit = res.headers["X-RateLimit-Limit"]
+ local remaining = res.headers["X-RateLimit-Remaining"]
+ local reset = res.headers["X-RateLimit-Reset"]
+
+ table.insert(results, {
+ req = i,
+ status = res.status,
+ limit = limit,
+ remaining = remaining,
+ has_reset = reset ~= nil
+ })
+ end
+
+ for _, r in ipairs(results) do
+ ngx.say(string.format("req %d: status=%d, limit=%s,
remaining=%s, has_reset=%s",
+ r.req, r.status, r.limit or "nil", r.remaining or "nil",
tostring(r.has_reset)))
+ end
+ }
+ }
+--- response_body_like
+req 1: status=404, limit=10, remaining=9, has_reset=true
+req 2: status=404, limit=10, remaining=8, has_reset=true
+req 3: status=404, limit=10, remaining=7, has_reset=true
+req 4: status=404, limit=10, remaining=6, has_reset=true
+req 5: status=404, limit=10, remaining=5, has_reset=true
Review Comment:
Changed to \d+ pattern so the test focuses on rate limit headers.
--
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]