Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(limit-count): X-RateLimit-Reset shouldn't be set to 0 after request be rejected #9978

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions apisix/plugins/limit-count/limit-count-local.lua
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ end

function _M.incoming(self, key, commit, conf, cost)
local delay, remaining = self.limit_count:incoming(key, commit, cost)
local reset = 0
if not delay then
return delay, remaining, reset
end
local reset

if remaining == conf.count - cost then
reset = set_endtime(self, key, conf.time_window)
Expand Down
50 changes: 50 additions & 0 deletions t/plugin/limit-count-redis-cluster2.t
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,53 @@ OK
OK
OK
Done



=== TEST 2: test header X-RateLimit-Reset shouldn't be set to 0 after request be rejected
--- 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": "/hello2",
"plugins": {
"limit-count": {
"count": 2,
"time_window": 60,
"key": "remote_addr",
"policy": "redis-cluster",
"redis_cluster_nodes": [
"127.0.0.1:5000",
"127.0.0.1:5001"
],
"redis_cluster_name": "redis-cluster-1"
}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
}
}]]
)
for i = 1, 3 do
local _, _, headers = t('/hello2', ngx.HTTP_GET)
ngx.sleep(1)
if tonumber(headers["X-RateLimit-Reset"]) > 0 then
ngx.say("OK")
else
ngx.say("WRONG")
end
end
ngx.say("Done")
}
}
--- response_body
OK
OK
OK
Done
71 changes: 71 additions & 0 deletions t/plugin/limit-count-redis3.t
Original file line number Diff line number Diff line change
Expand Up @@ -301,3 +301,74 @@ GET /hello
hello world
--- error_log
failed to do ssl handshake



=== TEST 10: set router
--- 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": 60,
"policy": "redis",
"redis_host": "127.0.0.1",
"redis_port": 6379,
"redis_database": 1,
"redis_timeout": 1001
}
},
"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 11: test header X-RateLimit-Reset shouldn't be set to 0 after request be rejected
--- 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 = {}
for i = 1, 3 do
local httpc = http.new()
local res, err = httpc:request_uri(uri)
if not res then
ngx.say(err)
return
end
ngx.sleep(1)
local reset = res.headers["X-RateLimit-Reset"]
if tonumber(reset) <= 0 then
ngx.say("failed")
end

end
ngx.say("success")
}
}
--- response_body
success
31 changes: 31 additions & 0 deletions t/plugin/limit-count4.t
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,34 @@ passed
}
--- response_body
["1","0","0"]



=== TEST 5: test header X-RateLimit-Reset shouldn't be set to 0 after request be rejected
--- 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 = {}
for i = 1, 3 do
local httpc = http.new()
local res, err = httpc:request_uri(uri)
if not res then
ngx.say(err)
return
end
ngx.sleep(1)
local reset = res.headers["X-RateLimit-Reset"]
if tonumber(reset) <= 0 then
ngx.say("failed")
end

end
ngx.say("success")
}
}
--- response_body
success