Klaus Demo nginx / 7b9bc5d
Limit req: improved handling of negative times. Negative times can appear since workers only update time on an event loop iteration start. If a worker was blocked for a long time during an event loop iteration, it is possible that another worker already updated the time stored in the node. As such, time since last update of the node (ms) will be negative. Previous code used ngx_abs(ms) in the calculations. That is, negative times were effectively treated as positive ones. As a result, it was not possible to maintain high request rates, where the same node can be updated multiple times from during an event loop iteration. In particular, this affected setups with many SSL handshakes, see http://mailman.nginx.org/pipermail/nginx/2018-May/056291.html. Fix is to only update the last update time stored in the node if the new time is larger than previously stored one. If a future time is stored in the node, we preserve this time as is. To prevent breaking things on platforms without monotonic time available if system time is updated backwards, a safety limit of 60 seconds is used. If the time stored in the node is more than 60 seconds in the future, we assume that the time was changed backwards and update lr->last to the current time. Maxim Dounin 1 year, 8 months ago
1 changed file(s) with 25 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
398398
399399 ms = (ngx_msec_int_t) (now - lr->last);
400400
401 excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
401 if (ms < -60000) {
402 ms = 1;
403
404 } else if (ms < 0) {
405 ms = 0;
406 }
407
408 excess = lr->excess - ctx->rate * ms / 1000 + 1000;
402409
403410 if (excess < 0) {
404411 excess = 0;
412419
413420 if (account) {
414421 lr->excess = excess;
415 lr->last = now;
422
423 if (ms) {
424 lr->last = now;
425 }
426
416427 return NGX_OK;
417428 }
418429
508519 now = ngx_current_msec;
509520 ms = (ngx_msec_int_t) (now - lr->last);
510521
511 excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
522 if (ms < -60000) {
523 ms = 1;
524
525 } else if (ms < 0) {
526 ms = 0;
527 }
528
529 excess = lr->excess - ctx->rate * ms / 1000 + 1000;
512530
513531 if (excess < 0) {
514532 excess = 0;
515533 }
516534
517 lr->last = now;
535 if (ms) {
536 lr->last = now;
537 }
538
518539 lr->excess = excess;
519540 lr->count--;
520541