关于 nginx 中 limit_req_zone 模块的 burst 的疑惑

1次阅读

共计 1193 个字符,预计需要花费 3 分钟才能阅读完成。

代码如下:

http{

   limit_req_zone $binary_remote_addr zone=mylimit:1m rate=6r/m;
   
   server {
      listen 80;
      ......
      location / {
         limit_req zone=mylimit burst=5;
         proxy_pass http://www.baidu.com;
      }
   }
}

对于 burst,nginx 给的解释是:

https://blog.nginx.org/blog/rate-limiting-nginx#:~:text=The%20burst%20parameter%20defines,requests%20go%20over%C2%A020.

摘录如下:

The burst parameter defines how many requests a client can make in excess of the rate specified by the zone (with our sample mylimit zone, the rate limit is 10 requests per second, or 1 every 100ms). A request that arrives sooner than 100ms after the previous one is put in a queue, and here we are setting the queue size to 20.

That means if 21 requests arrive from a given IP address simultaneously, NGINX forwards the first one to the upstream server group immediately and puts the remaining 20 in the queue. It then forwards a queued request every 100ms, and returns 503 to the client only if an incoming request makes the number of queued requests go over 20.

结合代码和这段文字,我对 burst 的理解是:

6r/m:以每 1 分钟为一个时间阶段,将这 1 分钟分为 6 段,即每 10s 最多接收 1 个请求,该 10s 内的多余请求将会被返回 503;

burst=5:突发队列长度 =5。以每 1 分钟为一个时间阶段,在这 1 分钟内,该队列最多只能接受 5 个请求,即使该队列内的请求已经被处理,也不会往队列里新增新的请求,直到下 1 分钟到来。

测试

测试工具:jmeter
测试线程数:120
测试时间 (ramp-up):120s
循环次数:1

预期结果:

在 120s 内,一共发起了 120 个请求,其中请求成功 22 个 (6+6+5+5),其余返回 503;

实际测试结果:

在 120s 内,一共发起了 120 个请求,其中请求成功 17 个 (6+6+5),其余返回 503;

为什么少了 5 个请求,希望大佬们为我指点下,谢谢~

正文完
 0