Unexpected result with limit_req module
Maxim Dounin
mdounin at mdounin.ru
Mon Sep 21 13:27:32 UTC 2015
Hello!
On Sun, Sep 20, 2015 at 05:51:26PM +0800, flygoast wrote:
> Hi, all,
>
>
> I use limit_req module to limit QPS to my upstream, conf like this:
> http {
> limit_req_zone $request_uri zone=req_one:10m rate=10000r/s;
> server {
> ...
> location / {
> limit_req zone=req_one;
> }
> }
> }
>
>
> I use benchmarking tool to stress on one url, but only about 1000 request.
>
>
> I checked source code, found that:
> ms = (ngx_msec_int_t) (now - lr->last);
>
>
> excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
>
>
> if (excess < 0) {
> excess = 0;
> }
>
>
> *ep = excess;
>
>
> if ((ngx_uint_t) excess > limit->burst) {
> return NGX_BUSY;
> }
>
>
> At here, ms can be '0', so in a millisecond, only the first
> request can pass the checking. After I set burst value to (rate
> / 1000) in configuration file, the QPS result is expected.
>
>
> I'm not sure whether this is a bug or It's my fault in configuration.
That's expected behaviour. As nginx tracks time with millisecond
resolution, two requests within the same millisecond essentially
mean infinite rate - unless nginx can pass these requests and
count them on a longer period of time. That is, if you use high
rates, you have to configure burst as well.
--
Maxim Dounin
http://nginx.org/
More information about the nginx-devel
mailing list