Skip to content

Commit c37f07c

Browse files
feat: use incremental counter
Signed-off-by: Abhishek Choudhary <[email protected]>
1 parent 7f9f33e commit c37f07c

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

lib/resty/limit/count.lua

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,22 @@ local function incoming_new(self, key, commit, cost)
5555
local limit = self.limit
5656
local window = self.window
5757

58-
local remaining, ok, err
58+
local consumed, ok, err
5959

6060
if commit then
61-
remaining, err = dict:incr(key, -cost, limit, window)
62-
if not remaining then
61+
consumed, err = dict:incr(key, cost, 0, window)
62+
if not consumed then
6363
return nil, err
6464
end
6565
else
66-
remaining = (dict:get(key) or limit) - cost
66+
consumed = (dict:get(key) or 0) + cost
6767
end
6868

69-
if remaining < 0 then
69+
if consumed > limit then
7070
return nil, "rejected"
7171
end
7272

73-
return 0, remaining
73+
return 0, consumed
7474
end
7575

7676
-- incoming function using incr and expire

0 commit comments

Comments
 (0)