mirror of
https://github.com/nginx/nginx.git
synced 2025-01-20 21:33:20 -06:00
*) correct leaky bucket implementation
*) now burst is not per second *) remove delay= parameter *) add nodelay parameter
This commit is contained in:
parent
05215534e7
commit
40cc949c7b
@ -33,7 +33,7 @@ typedef struct {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
ngx_shm_zone_t *shm_zone;
|
ngx_shm_zone_t *shm_zone;
|
||||||
float burst;
|
float burst;
|
||||||
ngx_msec_t delay;
|
ngx_uint_t nodelay; /* unsigned nodelay:1 */
|
||||||
} ngx_http_limit_req_conf_t;
|
} ngx_http_limit_req_conf_t;
|
||||||
|
|
||||||
|
|
||||||
@ -178,7 +178,7 @@ ngx_http_limit_req_handler(ngx_http_request_t *r)
|
|||||||
ngx_shmtx_unlock(&ctx->shpool->mutex);
|
ngx_shmtx_unlock(&ctx->shpool->mutex);
|
||||||
|
|
||||||
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
||||||
"limiting requests, %.3f r/s", rate);
|
"limiting requests, excess: %.3f", rate);
|
||||||
|
|
||||||
return NGX_HTTP_SERVICE_UNAVAILABLE;
|
return NGX_HTTP_SERVICE_UNAVAILABLE;
|
||||||
}
|
}
|
||||||
@ -186,9 +186,12 @@ ngx_http_limit_req_handler(ngx_http_request_t *r)
|
|||||||
if (rc == NGX_AGAIN) {
|
if (rc == NGX_AGAIN) {
|
||||||
ngx_shmtx_unlock(&ctx->shpool->mutex);
|
ngx_shmtx_unlock(&ctx->shpool->mutex);
|
||||||
|
|
||||||
if (lzcf->delay) {
|
if (lzcf->nodelay) {
|
||||||
|
return NGX_DECLINED;
|
||||||
|
}
|
||||||
|
|
||||||
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
|
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
|
||||||
"delaying requests, %.3f r/s", rate);
|
"delaying request, excess: %.3f", rate);
|
||||||
|
|
||||||
if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
|
if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
|
||||||
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
||||||
@ -196,14 +199,11 @@ ngx_http_limit_req_handler(ngx_http_request_t *r)
|
|||||||
|
|
||||||
r->read_event_handler = ngx_http_test_reading;
|
r->read_event_handler = ngx_http_test_reading;
|
||||||
r->write_event_handler = ngx_http_limit_req_delay;
|
r->write_event_handler = ngx_http_limit_req_delay;
|
||||||
ngx_add_timer(r->connection->write, lzcf->delay);
|
ngx_add_timer(r->connection->write, (ngx_msec_t) (rate * 1000));
|
||||||
|
|
||||||
return NGX_AGAIN;
|
return NGX_AGAIN;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NGX_DECLINED;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (rc == NGX_OK) {
|
if (rc == NGX_OK) {
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
@ -365,7 +365,7 @@ ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lzcf, ngx_uint_t hash,
|
|||||||
return NGX_BUSY;
|
return NGX_BUSY;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lz->rate > ctx->rate) {
|
if (lz->rate > 0.0) {
|
||||||
return NGX_AGAIN;
|
return NGX_AGAIN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -511,7 +511,7 @@ ngx_http_limit_req_create_conf(ngx_conf_t *cf)
|
|||||||
*
|
*
|
||||||
* conf->shm_zone = NULL;
|
* conf->shm_zone = NULL;
|
||||||
* conf->burst = 0.0;
|
* conf->burst = 0.0;
|
||||||
* conf->delay = 0;
|
* conf->nodelay = 0;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
return conf;
|
return conf;
|
||||||
@ -670,12 +670,9 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|||||||
{
|
{
|
||||||
ngx_http_limit_req_conf_t *lzcf = conf;
|
ngx_http_limit_req_conf_t *lzcf = conf;
|
||||||
|
|
||||||
u_char *p;
|
ngx_int_t burst;
|
||||||
size_t len;
|
|
||||||
ngx_int_t burst, scale, delay;
|
|
||||||
ngx_str_t *value, s;
|
ngx_str_t *value, s;
|
||||||
ngx_uint_t i;
|
ngx_uint_t i;
|
||||||
ngx_http_limit_req_ctx_t *ctx;
|
|
||||||
|
|
||||||
if (lzcf->shm_zone) {
|
if (lzcf->shm_zone) {
|
||||||
return "is duplicate";
|
return "is duplicate";
|
||||||
@ -684,8 +681,6 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|||||||
value = cf->args->elts;
|
value = cf->args->elts;
|
||||||
|
|
||||||
burst = 0;
|
burst = 0;
|
||||||
scale = 1;
|
|
||||||
delay = 0;
|
|
||||||
|
|
||||||
for (i = 1; i < cf->args->nelts; i++) {
|
for (i = 1; i < cf->args->nelts; i++) {
|
||||||
|
|
||||||
@ -705,19 +700,7 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|||||||
|
|
||||||
if (ngx_strncmp(value[i].data, "burst=", 6) == 0) {
|
if (ngx_strncmp(value[i].data, "burst=", 6) == 0) {
|
||||||
|
|
||||||
len = value[i].len;
|
burst = ngx_atoi(value[i].data + 6, value[i].len - 6);
|
||||||
p = value[i].data + len - 3;
|
|
||||||
|
|
||||||
if (ngx_strncmp(p, "r/s", 3) == 0) {
|
|
||||||
scale = 1;
|
|
||||||
len -= 3;
|
|
||||||
|
|
||||||
} else if (ngx_strncmp(p, "r/m", 3) == 0) {
|
|
||||||
scale = 60;
|
|
||||||
len -= 3;
|
|
||||||
}
|
|
||||||
|
|
||||||
burst = ngx_atoi(value[i].data + 6, len - 6);
|
|
||||||
if (burst <= 0) {
|
if (burst <= 0) {
|
||||||
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
||||||
"invalid burst rate \"%V\"", &value[i]);
|
"invalid burst rate \"%V\"", &value[i]);
|
||||||
@ -727,18 +710,8 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ngx_strncmp(value[i].data, "delay=", 6) == 0) {
|
if (ngx_strncmp(value[i].data, "nodelay", 7) == 0) {
|
||||||
|
lzcf->nodelay = 1;
|
||||||
s.len = value[i].len - 6;
|
|
||||||
s.data = value[i].data + 6;
|
|
||||||
|
|
||||||
delay = ngx_parse_time(&s, 0);
|
|
||||||
if (delay < 0) {
|
|
||||||
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
||||||
"invalid clean_time value \"%V\"", &value[i]);
|
|
||||||
return NGX_CONF_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -761,15 +734,7 @@ ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|||||||
return NGX_CONF_ERROR;
|
return NGX_CONF_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (burst) {
|
lzcf->burst = (float) burst;
|
||||||
lzcf->burst = (float) burst / scale;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
ctx = lzcf->shm_zone->data;
|
|
||||||
lzcf->burst = ctx->rate;
|
|
||||||
}
|
|
||||||
|
|
||||||
lzcf->delay = (ngx_msec_t) delay;
|
|
||||||
|
|
||||||
return NGX_CONF_OK;
|
return NGX_CONF_OK;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user