Skip to content

Commit 600d50c

Browse files
committed
feature: resty.limit.count does not increment when rejected
1 parent c53f224 commit 600d50c

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

lib/resty/limit/count.lua

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ function _M.incoming(self, key, commit)
5454
end
5555

5656
if count > limit then
57+
if commit then
58+
self:uncommit(key)
59+
end
5760
return nil, "rejected"
5861
end
5962

t/count.t

+47
Original file line numberDiff line numberDiff line change
@@ -276,3 +276,50 @@ remaining: 8
276276
--- no_error_log
277277
[error]
278278
[lua]
279+
280+
281+
282+
=== TEST 7: over incrementing when over limit
283+
--- http_config eval: $::HttpConfig
284+
--- config
285+
location = /t {
286+
content_by_lua_block {
287+
local limit_count = require "resty.limit.count"
288+
local lim = limit_count.new("store", 1, 10)
289+
ngx.shared.store:flush_all()
290+
local key = "foo"
291+
292+
local delay, err = lim:incoming(key, true)
293+
if not delay then
294+
ngx.say("failed to limit count: ", err)
295+
else
296+
local remaining = err
297+
ngx.say("remaining: ", remaining)
298+
end
299+
300+
local delay, err = lim:incoming(key, true)
301+
if not delay then
302+
ngx.say("failed to limit count: ", err)
303+
else
304+
local remaining = err
305+
ngx.say("remaining: ", remaining)
306+
end
307+
308+
local remaining, err = lim:uncommit(key)
309+
310+
if not remaining then
311+
ngx.say("failed to uncommit count: ", err)
312+
else
313+
ngx.say("remaining: ", remaining)
314+
end
315+
}
316+
}
317+
--- request
318+
GET /t
319+
--- response_body
320+
remaining: 0
321+
failed to limit count: rejected
322+
remaining: 1
323+
--- no_error_log
324+
[error]
325+
[lua]

0 commit comments

Comments
 (0)