Skip to content

Commit 788b673

Browse files
feat: use incremental counter #3
feat: use incremental counter
2 parents 7bbf9e5 + d4d385b commit 788b673

File tree

4 files changed

+42
-36
lines changed

4 files changed

+42
-36
lines changed

.github/workflows/test.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ on:
88

99
jobs:
1010
test:
11-
runs-on: "ubuntu-20.04"
11+
runs-on: "buildjet-4vcpu-ubuntu-2004"
1212
env:
1313
OPENRESTY_PREFIX: "/usr/local/openresty"
1414

lib/resty/limit/count.lua

Lines changed: 10 additions & 10 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
@@ -125,16 +125,16 @@ function _M.uncommit(self, key)
125125
local dict = self.dict
126126
local limit = self.limit
127127

128-
local remaining, err = dict:incr(key, 1)
129-
if not remaining then
128+
local consumed, err = dict:incr(key, -1)
129+
if not consumed then
130130
if err == "not found" then
131-
remaining = limit
131+
consumed = limit
132132
else
133133
return nil, err
134134
end
135135
end
136136

137-
return remaining
137+
return consumed
138138
end
139139

140140

t/count.t

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,15 @@ __DATA__
3434
content_by_lua_block {
3535
local limit_count = require "resty.limit.count"
3636
ngx.shared.store:flush_all()
37-
local lim = limit_count.new("store", 10, 100)
37+
local limit = 10
38+
local lim = limit_count.new("store", limit, 100)
3839
local uri = ngx.var.uri
3940
for i = 1, 12 do
4041
local delay, err = lim:incoming(uri, true)
4142
if not delay then
4243
ngx.say(err)
4344
else
44-
local remaining = err
45+
local remaining = limit - err
4546
ngx.say("remaining: ", remaining)
4647
end
4748
end
@@ -75,36 +76,37 @@ rejected
7576
content_by_lua_block {
7677
local limit_count = require "resty.limit.count"
7778
ngx.shared.store:flush_all()
78-
local lim = limit_count.new("store", 1, 10)
79+
local limit = 1
80+
local lim = limit_count.new("store", limit, 10)
7981
local delay1, err1 = lim:incoming("foo", true)
8082
local delay2, err2 = lim:incoming("foo", true)
8183
local delay3, err3 = lim:incoming("bar", true)
8284
local delay4, err4 = lim:incoming("bar", true)
8385
if not delay1 then
8486
ngx.say(err1)
8587
else
86-
local remaining1 = err1
88+
local remaining1 = limit - err1
8789
ngx.say("remaining1: ", remaining1)
8890
end
8991
9092
if not delay2 then
9193
ngx.say(err2)
9294
else
93-
local remaining2 = err2
95+
local remaining2 = limit - err2
9496
ngx.say("remaining2: ", remaining2)
9597
end
9698
9799
if not delay3 then
98100
ngx.say(err3)
99101
else
100-
local remaining3 = err3
102+
local remaining3 = limit -err3
101103
ngx.say("remaining3: ", remaining3)
102104
end
103105
104106
if not delay4 then
105107
ngx.say(err4)
106108
else
107-
local remaining4 = err4
109+
local remaining4 = limit - err4
108110
ngx.say("remaining4: ", remaining4)
109111
end
110112
}
@@ -129,23 +131,24 @@ rejected
129131
content_by_lua_block {
130132
local limit_count = require "resty.limit.count"
131133
ngx.shared.store:flush_all()
132-
local lim = limit_count.new("store", 1, 1)
134+
local limit = 1
135+
local lim = limit_count.new("store", limit, 1)
133136
134137
local uri = ngx.var.uri
135138
for i = 1, 2 do
136139
local delay, err = lim:incoming(uri, true)
137140
if not delay then
138141
ngx.say(err)
139142
else
140-
local remaining = err
143+
local remaining = limit - err
141144
ngx.say("remaining: ", remaining)
142145
end
143146
144147
local delay, err = lim:incoming(uri, true)
145148
if not delay then
146149
ngx.say(err)
147150
else
148-
local remaining = err
151+
local remaining = limit - err
149152
ngx.say("remaining: ", remaining)
150153
end
151154
ngx.sleep(1)
@@ -172,15 +175,16 @@ rejected
172175
content_by_lua_block {
173176
local limit_count = require "resty.limit.count"
174177
ngx.shared.store:flush_all()
175-
local lim = limit_count.new("store", 5, 10)
178+
local limit = 5
179+
local lim = limit_count.new("store", limit, 10)
176180
local begin = ngx.time()
177181
178182
for i = 1, 4 do
179183
local delay, err = lim:incoming("foo", i < 3)
180184
if not delay then
181185
ngx.say(err)
182186
else
183-
local remaining = err
187+
local remaining = limit - err
184188
ngx.say("remaining: ", remaining)
185189
end
186190
end
@@ -205,15 +209,16 @@ remaining: 2
205209
location = /t {
206210
content_by_lua_block {
207211
local limit_count = require "resty.limit.count"
208-
local lim = limit_count.new("store", 2, 10)
212+
local limit = 2
213+
local lim = limit_count.new("store", limit, 10)
209214
ngx.shared.store:flush_all()
210215
local key = "foo"
211216
for i = 1, 3 do
212217
local delay, err = lim:incoming(key, true)
213218
if not delay then
214219
ngx.say("failed to limit count: ", err)
215220
else
216-
local remaining = err
221+
local remaining = limit - err
217222
ngx.say("remaining: ", remaining)
218223
end
219224
local ok, err = lim:uncommit(key)
@@ -242,14 +247,15 @@ remaining: 1
242247
content_by_lua_block {
243248
local limit_count = require "resty.limit.count"
244249
ngx.shared.store:flush_all()
245-
local lim = limit_count.new("store", 10, 100)
250+
local limit = 10
251+
local lim = limit_count.new("store", limit, 100)
246252
local uri = ngx.var.uri
247253
for i = 1, 7 do
248254
local delay, err = lim:incoming(uri, true, 2)
249255
if not delay then
250256
ngx.say(err)
251257
else
252-
local remaining = err
258+
local remaining = limit - err
253259
ngx.say("remaining: ", remaining)
254260
end
255261
end

t/traffic.t

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ $::HttpConfig
7474
--- request
7575
GET /t
7676
--- response_body_like eval
77-
qr/^1: 0, conn committed: true, states: 0, 0, 1, 9
78-
2: 0\.5, conn committed: true, states: 1, 1, 2, 8
79-
3: 1, conn committed: true, states: 2, 2, 3, 7
77+
qr/^1: 0, conn committed: true, states: 0, 0, 1, 1
78+
2: 0\.5, conn committed: true, states: 1, 1, 2, 2
79+
3: 1, conn committed: true, states: 2, 2, 3, 3
8080
failed to limit traffic: rejected
81-
5: 0\.(?:4[6-9]|5|5[0-4])\d*, conn committed: true, states: 0, (?:1|1\.0[0-4]\d*|0\.9[6-9]\d*), 4, 6
81+
5: 0\.(?:4[6-9]|5|5[0-4])\d*, conn committed: true, states: 0, (?:1|1\.0[0-4]\d*|0\.9[6-9]\d*), 4, 4
8282
6: 2, conn committed: true, states: 1, (?:2|2\.0[0-4]\d*|1\.9[6-9]\d*), 5, 5
8383
$/s
8484
--- no_error_log
@@ -193,15 +193,15 @@ $::HttpConfig
193193
GET /t
194194
--- response_body_like eval
195195
qr/^1: 0, conn committed: true, states: 0, 0, 1, 1
196-
2: 0\.5, conn committed: true, states: 1, 1, 2, 0
196+
2: 0\.5, conn committed: true, states: 1, 1, 2, 2
197197
failed to limit traffic: rejected
198-
states: 1, 1, 2, 0
198+
states: 1, 1, 2, 2
199199
failed to limit traffic: rejected
200-
states: 1, 1, 2, 0
200+
states: 1, 1, 2, 2
201201
failed to limit traffic: rejected
202-
states: 1, 1, 2, 0
202+
states: 1, 1, 2, 2
203203
failed to limit traffic: rejected
204-
states: 1, 1, 2, 0
204+
states: 1, 1, 2, 2
205205
$/s
206206
--- no_error_log
207207
[error]

0 commit comments

Comments
 (0)