@@ -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
0 commit comments