Hi
resty.limit.req does not seem to be working for me. I would appreciate some help. Thank you!
nginx.conf
[...]
server {
listen 8004;
proxy_intercept_errors off;
location / {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_connect_timeout 20ms;
proxy_send_timeout 20ms;
proxy_read_timeout 60ms;
access_by_lua_file /usr/local/openresty/nginx/conf/conf.d/lua/access.lua;
proxy_pass http://liverail;
}
[...]
/usr/local/openresty/nginx/conf/conf.d/lua/access.lua
local limit_req = require "resty.limit.req";
local lim, err = limit_req.new("my_limit_req_store", 990, 10);
if not lim then
ngx.log(ngx.ERR,
"failed to instantiate a resty.limit.req object: ", err);
return ngx.exit(500);
end
local key = ngx.var.binary_remote_addr;
local delay, err = lim:incoming(key, true);
if not delay then
if err == "rejected" then
return ngx.exit(503);
end
ngx.log(ngx.ERR, "failed to limit req: ", err);
return ngx.exit(500);
end
if delay > 0 then
local excess = err;
ngx.sleep(delay);
end
Testing the rate limit with wrk (https://github.com/wg/wrk) ...
ubuntu@ip-XXX-XXX-XXX-XXX:~/wrk$ ./wrk -t1 -c1 -d30s http://127.0.0.1:8005
Running 30s test @ http://127.0.0.1:8005
1 threads and 1 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 1.01ms 140.87us 2.16ms 89.43%
Req/Sec 0.99k 8.72 1.01k 74.75%
29799 requests in 30.10s, 3.27MB read
Requests/sec: 990.01
Transfer/sec: 111.14KB
ubuntu@ip-XXX-XXX-XXX-XXX:~/wrk$ ./wrk -t1 -c2 -d30s http://127.0.0.1:8005
Running 30s test @ http://127.0.0.1:8005
1 threads and 2 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 2.01ms 501.88us 4.07ms 66.71%
Req/Sec 1.00k 9.51 1.02k 73.33%
29767 requests in 30.01s, 3.26MB read
Requests/sec: 991.88
Transfer/sec: 111.35KB
ubuntu@ip-XXX-XXX-XXX-XXX:~/wrk$ ./wrk -t1 -c10 -d30s http://127.0.0.1:8005
Running 30s test @ http://127.0.0.1:8005
1 threads and 10 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 2.77ms 3.29ms 11.38ms 77.75%
Req/Sec 10.67k 1.28k 15.24k 71.00%
318583 requests in 30.00s, 113.43MB read
Non-2xx or 3xx responses: 288842
Requests/sec: 10619.05 <================================ I would expect to see no more that 1000 requests/ second. Why do I see 10K?
Transfer/sec: 3.78MB