Klaus Demo nginx / aedc37f
Limit req: "delay=" parameter. This parameter specifies an additional "soft" burst limit at which requests become delayed (but not yet rejected as it happens if "burst=" limit is exceeded). Defaults to 0, i.e., all excess requests are delayed. Originally inspired by Vladislav Shabanov (http://mailman.nginx.org/pipermail/nginx-devel/2016-April/008126.html). Further improved based on a patch by Peter Shchuchkin (http://mailman.nginx.org/pipermail/nginx-devel/2018-October/011522.html). Maxim Dounin 3 years ago
1 changed file(s) with 25 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
4343 ngx_shm_zone_t *shm_zone;
4444 /* integer value, 1 corresponds to 0.001 r/s */
4545 ngx_uint_t burst;
46 ngx_uint_t nodelay; /* unsigned nodelay:1 */
46 ngx_uint_t delay;
4747 } ngx_http_limit_req_limit_t;
4848
4949
498498
499499 excess = *ep;
500500
501 if (excess == 0 || (*limit)->nodelay) {
501 if ((ngx_uint_t) excess <= (*limit)->delay) {
502502 max_delay = 0;
503503
504504 } else {
505505 ctx = (*limit)->shm_zone->data;
506 max_delay = excess * 1000 / ctx->rate;
506 max_delay = (excess - (*limit)->delay) * 1000 / ctx->rate;
507507 }
508508
509509 while (n--) {
543543
544544 ctx->node = NULL;
545545
546 if (limits[n].nodelay) {
547 continue;
548 }
549
550 delay = excess * 1000 / ctx->rate;
546 if ((ngx_uint_t) excess <= limits[n].delay) {
547 continue;
548 }
549
550 delay = (excess - limits[n].delay) * 1000 / ctx->rate;
551551
552552 if (delay > max_delay) {
553553 max_delay = delay;
874874 {
875875 ngx_http_limit_req_conf_t *lrcf = conf;
876876
877 ngx_int_t burst;
877 ngx_int_t burst, delay;
878878 ngx_str_t *value, s;
879 ngx_uint_t i, nodelay;
879 ngx_uint_t i;
880880 ngx_shm_zone_t *shm_zone;
881881 ngx_http_limit_req_limit_t *limit, *limits;
882882
884884
885885 shm_zone = NULL;
886886 burst = 0;
887 nodelay = 0;
887 delay = 0;
888888
889889 for (i = 1; i < cf->args->nelts; i++) {
890890
914914 continue;
915915 }
916916
917 if (ngx_strncmp(value[i].data, "delay=", 6) == 0) {
918
919 delay = ngx_atoi(value[i].data + 6, value[i].len - 6);
920 if (delay <= 0) {
921 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
922 "invalid delay value \"%V\"", &value[i]);
923 return NGX_CONF_ERROR;
924 }
925
926 continue;
927 }
928
917929 if (ngx_strcmp(value[i].data, "nodelay") == 0) {
918 nodelay = 1;
930 delay = NGX_MAX_INT_T_VALUE / 1000;
919931 continue;
920932 }
921933
955967
956968 limit->shm_zone = shm_zone;
957969 limit->burst = burst * 1000;
958 limit->nodelay = nodelay;
970 limit->delay = delay * 1000;
959971
960972 return NGX_CONF_OK;
961973 }