File tree Expand file tree Collapse file tree 4 files changed +22
-22
lines changed
Expand file tree Collapse file tree 4 files changed +22
-22
lines changed Original file line number Diff line number Diff line change @@ -44,22 +44,22 @@ add_block_preprocessor(sub {
4444 socket_timeout = 1000
4545 }
4646 local red = redis_cluster:new(config)
47-
47+
4848 -- make a request to /access
4949 local httpc = require("resty.http").new()
5050 local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/access"
5151 local res, err = httpc:request_uri(uri, {
5252 method = "GET"
5353 })
54-
54+
5555 if not res then
5656 ngx.say("failed to request: ", err)
5757 return
5858 end
59-
59+
6060 local key = "limit_conn:127.0.0.1"
6161 local ttl, err = red:ttl(key)
62-
62+
6363 if not ttl then
6464 ngx.say("failed to get ttl: ", err)
6565 return
@@ -232,21 +232,21 @@ passed
232232 socket_timeout = 1000
233233 }
234234 local red = redis_cluster:new(config)
235-
235+
236236 local httpc = require("resty.http").new()
237237 local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/access"
238238 local res, err = httpc:request_uri(uri, {
239239 method = "GET"
240240 })
241-
241+
242242 local key = "limit_conn:127.0.0.1"
243243 local ttl, err = red:ttl(key)
244-
244+
245245 if not ttl then
246246 ngx.say("failed to get ttl: ", err)
247247 return
248248 end
249-
249+
250250 if ttl > 5 and ttl <= 10 then
251251 ngx.say("ttl is 10")
252252 else
Original file line number Diff line number Diff line change @@ -31,25 +31,25 @@ add_block_preprocessor(sub {
3131 red:connect("127.0.0.1", 6379)
3232 -- clear keys first
3333 red:flushall()
34-
34+
3535 -- make a request to /access
3636 local httpc = require("resty.http").new()
3737 local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/access"
3838 local res, err = httpc:request_uri(uri, {
3939 method = "GET"
4040 })
41-
41+
4242 if not res then
4343 ngx.say("failed to request: ", err)
4444 return
4545 end
46-
46+
4747 local keys, err = red:keys("limit_conn:*")
4848 if not keys or #keys == 0 then
4949 ngx.say("no keys found")
5050 return
5151 end
52-
52+
5353 -- Key format: limit_conn:ip
5454 local ttl = red:ttl(keys[1])
5555 -- Expected 60
@@ -189,20 +189,20 @@ passed
189189 local red = redis:new()
190190 red:connect("127.0.0.1", 6379)
191191 red:flushall()
192-
192+
193193 local httpc = require("resty.http").new()
194194 local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/access"
195195 local res, err = httpc:request_uri(uri, {
196196 method = "GET"
197197 })
198-
198+
199199 local keys, err = red:keys("limit_conn:*")
200200 if not keys or #keys == 0 then
201201 ngx.say("no keys found")
202202 return
203203 end
204204 local ttl = red:ttl(keys[1])
205-
205+
206206 if ttl > 5 and ttl <= 10 then
207207 ngx.say("ttl is 10")
208208 else
Original file line number Diff line number Diff line change @@ -44,23 +44,23 @@ add_block_preprocessor(sub {
4444 socket_timeout = 1000
4545 }
4646 local red = redis_cluster:new(config)
47-
47+
4848 -- make a request to /access
4949 local httpc = require("resty.http").new()
5050 local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/access"
5151 local res, err = httpc:request_uri(uri, {
5252 method = "GET"
5353 })
54-
54+
5555 if not res then
5656 ngx.say("failed to request: ", err)
5757 return
5858 end
59-
59+
6060 -- key format: limit_req:remote_addr:excess or limit_req:remote_addr:last
6161 local key = "limit_req:127.0.0.1:excess"
6262 local ttl, err = red:ttl(key)
63-
63+
6464 if not ttl or ttl == -2 then -- -2 means key does not exist
6565 -- try the 'last' key
6666 key = "limit_req:127.0.0.1:last"
Original file line number Diff line number Diff line change @@ -31,14 +31,14 @@ add_block_preprocessor(sub {
3131 local red = redis:new()
3232 red:connect("127.0.0.1", 6379)
3333 red:flushall()
34-
34+
3535 -- make a request to /access
3636 local httpc = require("resty.http").new()
3737 local uri = "http://127.0.0.1:" .. ngx.var.server_port .. "/access"
3838 local res, err = httpc:request_uri(uri, {
3939 method = "GET"
4040 })
41-
41+
4242 if not res then
4343 ngx.say("failed to request: ", err)
4444 return
@@ -51,7 +51,7 @@ add_block_preprocessor(sub {
5151 ngx.say("no keys found")
5252 return
5353 end
54-
54+
5555 -- check ttl
5656 -- for rate=1, burst=10 -> ttl = ceil(10/1)+1 = 11.
5757 local ttl = red:ttl(keys[1])
You can’t perform that action at this time.
0 commit comments