2008-11-10 23:22:33 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Igor Sysoev
|
2012-01-18 23:07:43 +08:00
|
|
|
* Copyright (C) Nginx, Inc.
|
2008-11-10 23:22:33 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2009-04-19 03:27:28 +08:00
|
|
|
u_char color;
|
|
|
|
u_char dummy;
|
|
|
|
u_short len;
|
|
|
|
ngx_queue_t queue;
|
|
|
|
ngx_msec_t last;
|
|
|
|
/* integer value, 1 corresponds to 0.001 r/s */
|
|
|
|
ngx_uint_t excess;
|
2012-01-30 18:17:56 +08:00
|
|
|
ngx_uint_t count;
|
2009-04-19 03:27:28 +08:00
|
|
|
u_char data[1];
|
2008-11-10 23:22:33 +08:00
|
|
|
} ngx_http_limit_req_node_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2009-04-19 03:27:28 +08:00
|
|
|
ngx_rbtree_t rbtree;
|
|
|
|
ngx_rbtree_node_t sentinel;
|
|
|
|
ngx_queue_t queue;
|
|
|
|
} ngx_http_limit_req_shctx_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ngx_http_limit_req_shctx_t *sh;
|
|
|
|
ngx_slab_pool_t *shpool;
|
|
|
|
/* integer value, 1 corresponds to 0.001 r/s */
|
|
|
|
ngx_uint_t rate;
|
|
|
|
ngx_int_t index;
|
|
|
|
ngx_str_t var;
|
2012-01-30 18:17:56 +08:00
|
|
|
ngx_http_limit_req_node_t *node;
|
2008-11-10 23:22:33 +08:00
|
|
|
} ngx_http_limit_req_ctx_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2009-04-19 03:27:28 +08:00
|
|
|
ngx_shm_zone_t *shm_zone;
|
|
|
|
/* integer value, 1 corresponds to 0.001 r/s */
|
|
|
|
ngx_uint_t burst;
|
2012-01-30 18:17:56 +08:00
|
|
|
ngx_uint_t nodelay; /* unsigned nodelay:1 */
|
|
|
|
} ngx_http_limit_req_limit_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ngx_array_t limits;
|
2009-10-06 18:14:21 +08:00
|
|
|
ngx_uint_t limit_log_level;
|
|
|
|
ngx_uint_t delay_log_level;
|
2008-11-10 23:22:33 +08:00
|
|
|
} ngx_http_limit_req_conf_t;
|
|
|
|
|
|
|
|
|
|
|
|
static void ngx_http_limit_req_delay(ngx_http_request_t *r);
|
2012-01-30 18:17:56 +08:00
|
|
|
static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_limit_t *limit,
|
|
|
|
ngx_uint_t hash, u_char *data, size_t len, ngx_uint_t *ep,
|
|
|
|
ngx_uint_t account);
|
|
|
|
static ngx_msec_t ngx_http_limit_req_account(ngx_http_limit_req_limit_t *limits,
|
|
|
|
ngx_uint_t n, ngx_uint_t *ep, ngx_http_limit_req_limit_t **limit);
|
2008-11-10 23:22:33 +08:00
|
|
|
static void ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx,
|
|
|
|
ngx_uint_t n);
|
|
|
|
|
|
|
|
static void *ngx_http_limit_req_create_conf(ngx_conf_t *cf);
|
|
|
|
static char *ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent,
|
|
|
|
void *child);
|
|
|
|
static char *ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
|
|
void *conf);
|
|
|
|
static char *ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
|
|
void *conf);
|
|
|
|
static ngx_int_t ngx_http_limit_req_init(ngx_conf_t *cf);
|
|
|
|
|
|
|
|
|
2009-10-06 18:14:21 +08:00
|
|
|
static ngx_conf_enum_t ngx_http_limit_req_log_levels[] = {
|
|
|
|
{ ngx_string("info"), NGX_LOG_INFO },
|
|
|
|
{ ngx_string("notice"), NGX_LOG_NOTICE },
|
|
|
|
{ ngx_string("warn"), NGX_LOG_WARN },
|
|
|
|
{ ngx_string("error"), NGX_LOG_ERR },
|
|
|
|
{ ngx_null_string, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2008-11-10 23:22:33 +08:00
|
|
|
static ngx_command_t ngx_http_limit_req_commands[] = {
|
|
|
|
|
|
|
|
{ ngx_string("limit_req_zone"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3,
|
|
|
|
ngx_http_limit_req_zone,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ ngx_string("limit_req"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
|
|
|
|
ngx_http_limit_req,
|
|
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
|
|
|
0,
|
|
|
|
NULL },
|
|
|
|
|
2009-10-06 18:14:21 +08:00
|
|
|
{ ngx_string("limit_req_log_level"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_enum_slot,
|
|
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
|
|
|
offsetof(ngx_http_limit_req_conf_t, limit_log_level),
|
|
|
|
&ngx_http_limit_req_log_levels },
|
|
|
|
|
2008-11-10 23:22:33 +08:00
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_http_module_t ngx_http_limit_req_module_ctx = {
|
|
|
|
NULL, /* preconfiguration */
|
|
|
|
ngx_http_limit_req_init, /* postconfiguration */
|
|
|
|
|
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
|
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
|
|
|
|
|
|
|
ngx_http_limit_req_create_conf, /* create location configration */
|
|
|
|
ngx_http_limit_req_merge_conf /* merge location configration */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_http_limit_req_module = {
|
|
|
|
NGX_MODULE_V1,
|
|
|
|
&ngx_http_limit_req_module_ctx, /* module context */
|
|
|
|
ngx_http_limit_req_commands, /* module directives */
|
|
|
|
NGX_HTTP_MODULE, /* module type */
|
|
|
|
NULL, /* init master */
|
|
|
|
NULL, /* init module */
|
|
|
|
NULL, /* init process */
|
|
|
|
NULL, /* init thread */
|
|
|
|
NULL, /* exit thread */
|
|
|
|
NULL, /* exit process */
|
|
|
|
NULL, /* exit master */
|
|
|
|
NGX_MODULE_V1_PADDING
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_limit_req_handler(ngx_http_request_t *r)
|
|
|
|
{
|
2012-01-30 18:17:56 +08:00
|
|
|
size_t len;
|
|
|
|
uint32_t hash;
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_uint_t n, excess;
|
|
|
|
ngx_msec_t delay;
|
|
|
|
ngx_http_variable_value_t *vv;
|
|
|
|
ngx_http_limit_req_ctx_t *ctx;
|
|
|
|
ngx_http_limit_req_conf_t *lrcf;
|
|
|
|
ngx_http_limit_req_limit_t *limit, *limits;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
|
|
|
if (r->main->limit_req_set) {
|
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
|
|
|
|
2008-12-08 22:13:36 +08:00
|
|
|
lrcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_req_module);
|
2012-01-30 18:17:56 +08:00
|
|
|
limits = lrcf->limits.elts;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
excess = 0;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
rc = NGX_DECLINED;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 21:19:25 +08:00
|
|
|
#if (NGX_SUPPRESS_WARN)
|
|
|
|
limit = NULL;
|
|
|
|
#endif
|
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
for (n = 0; n < lrcf->limits.nelts; n++) {
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
limit = &limits[n];
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
ctx = limit->shm_zone->data;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
vv = ngx_http_get_indexed_variable(r, ctx->index);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
if (vv == NULL || vv->not_found) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
len = vv->len;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
if (len == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
if (len > 65535) {
|
|
|
|
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
|
|
"the value of the \"%V\" variable "
|
|
|
|
"is more than 65535 bytes: \"%v\"",
|
|
|
|
&ctx->var, vv);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash = ngx_crc32_short(vv->data, len);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
ngx_shmtx_lock(&ctx->shpool->mutex);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
rc = ngx_http_limit_req_lookup(limit, hash, vv->data, len, &excess,
|
|
|
|
(n == lrcf->limits.nelts - 1));
|
2012-01-30 17:41:49 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
ngx_shmtx_unlock(&ctx->shpool->mutex);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
|
|
"limit_req[%ui]: %i %ui.%03ui",
|
|
|
|
n, rc, excess / 1000, excess % 1000);
|
|
|
|
|
|
|
|
if (rc != NGX_AGAIN) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc == NGX_DECLINED) {
|
2010-10-14 17:20:01 +08:00
|
|
|
return NGX_DECLINED;
|
2008-11-10 23:22:33 +08:00
|
|
|
}
|
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
r->main->limit_req_set = 1;
|
|
|
|
|
2012-01-30 17:41:49 +08:00
|
|
|
if (rc == NGX_BUSY || rc == NGX_ERROR) {
|
|
|
|
|
|
|
|
if (rc == NGX_BUSY) {
|
|
|
|
ngx_log_error(lrcf->limit_log_level, r->connection->log, 0,
|
|
|
|
"limiting requests, excess: %ui.%03ui by zone \"%V\"",
|
|
|
|
excess / 1000, excess % 1000,
|
2012-01-30 18:17:56 +08:00
|
|
|
&limit->shm_zone->shm.name);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (n--) {
|
|
|
|
ctx = limits[n].shm_zone->data;
|
|
|
|
|
|
|
|
if (ctx->node == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_shmtx_lock(&ctx->shpool->mutex);
|
|
|
|
|
|
|
|
ctx->node->count--;
|
|
|
|
|
|
|
|
ngx_shmtx_unlock(&ctx->shpool->mutex);
|
|
|
|
|
|
|
|
ctx->node = NULL;
|
2012-01-30 17:41:49 +08:00
|
|
|
}
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2010-10-14 17:20:01 +08:00
|
|
|
return NGX_HTTP_SERVICE_UNAVAILABLE;
|
|
|
|
}
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
/* rc == NGX_AGAIN || rc == NGX_OK */
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
if (rc == NGX_AGAIN) {
|
|
|
|
excess = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
delay = ngx_http_limit_req_account(limits, n, &excess, &limit);
|
|
|
|
|
|
|
|
if (!delay) {
|
2010-10-14 17:20:01 +08:00
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2010-10-14 17:20:01 +08:00
|
|
|
ngx_log_error(lrcf->delay_log_level, r->connection->log, 0,
|
|
|
|
"delaying request, excess: %ui.%03ui, by zone \"%V\"",
|
2012-01-30 18:17:56 +08:00
|
|
|
excess / 1000, excess % 1000, &limit->shm_zone->shm.name);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2010-10-14 17:20:01 +08:00
|
|
|
if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2010-10-14 17:20:01 +08:00
|
|
|
r->read_event_handler = ngx_http_test_reading;
|
|
|
|
r->write_event_handler = ngx_http_limit_req_delay;
|
2012-01-30 18:17:56 +08:00
|
|
|
ngx_add_timer(r->connection->write, delay);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2010-10-14 17:20:01 +08:00
|
|
|
return NGX_AGAIN;
|
2008-11-10 23:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_http_limit_req_delay(ngx_http_request_t *r)
|
|
|
|
{
|
2009-07-09 22:02:09 +08:00
|
|
|
ngx_event_t *wev;
|
|
|
|
|
2008-11-10 23:22:33 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
2009-07-09 22:03:12 +08:00
|
|
|
"limit_req delay");
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2009-07-09 22:02:09 +08:00
|
|
|
wev = r->connection->write;
|
|
|
|
|
|
|
|
if (!wev->timedout) {
|
|
|
|
|
|
|
|
if (ngx_handle_write_event(wev, 0) != NGX_OK) {
|
|
|
|
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
wev->timedout = 0;
|
|
|
|
|
2008-11-10 23:22:33 +08:00
|
|
|
if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
|
|
|
|
ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->read_event_handler = ngx_http_block_reading;
|
|
|
|
r->write_event_handler = ngx_http_core_run_phases;
|
|
|
|
|
|
|
|
ngx_http_core_run_phases(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_http_limit_req_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
|
|
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
|
|
{
|
|
|
|
ngx_rbtree_node_t **p;
|
2008-12-08 22:13:36 +08:00
|
|
|
ngx_http_limit_req_node_t *lrn, *lrnt;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
if (node->key < temp->key) {
|
|
|
|
|
|
|
|
p = &temp->left;
|
|
|
|
|
|
|
|
} else if (node->key > temp->key) {
|
|
|
|
|
|
|
|
p = &temp->right;
|
|
|
|
|
|
|
|
} else { /* node->key == temp->key */
|
|
|
|
|
2008-12-08 22:13:36 +08:00
|
|
|
lrn = (ngx_http_limit_req_node_t *) &node->color;
|
|
|
|
lrnt = (ngx_http_limit_req_node_t *) &temp->color;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2008-12-08 22:13:36 +08:00
|
|
|
p = (ngx_memn2cmp(lrn->data, lrnt->data, lrn->len, lrnt->len) < 0)
|
2008-11-10 23:22:33 +08:00
|
|
|
? &temp->left : &temp->right;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*p == sentinel) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = node;
|
|
|
|
node->parent = temp;
|
|
|
|
node->left = sentinel;
|
|
|
|
node->right = sentinel;
|
|
|
|
ngx_rbt_red(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
2012-01-30 18:17:56 +08:00
|
|
|
ngx_http_limit_req_lookup(ngx_http_limit_req_limit_t *limit, ngx_uint_t hash,
|
|
|
|
u_char *data, size_t len, ngx_uint_t *ep, ngx_uint_t account)
|
2008-11-10 23:22:33 +08:00
|
|
|
{
|
2012-01-30 17:41:49 +08:00
|
|
|
size_t size;
|
2008-11-14 21:25:44 +08:00
|
|
|
ngx_int_t rc, excess;
|
2008-11-10 23:22:33 +08:00
|
|
|
ngx_time_t *tp;
|
|
|
|
ngx_msec_t now;
|
|
|
|
ngx_msec_int_t ms;
|
|
|
|
ngx_rbtree_node_t *node, *sentinel;
|
|
|
|
ngx_http_limit_req_ctx_t *ctx;
|
2008-12-08 22:13:36 +08:00
|
|
|
ngx_http_limit_req_node_t *lr;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 17:41:49 +08:00
|
|
|
tp = ngx_timeofday();
|
|
|
|
now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
|
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
ctx = limit->shm_zone->data;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2009-04-19 03:27:28 +08:00
|
|
|
node = ctx->sh->rbtree.root;
|
|
|
|
sentinel = ctx->sh->rbtree.sentinel;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
|
|
|
while (node != sentinel) {
|
|
|
|
|
|
|
|
if (hash < node->key) {
|
|
|
|
node = node->left;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hash > node->key) {
|
|
|
|
node = node->right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hash == node->key */
|
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
lr = (ngx_http_limit_req_node_t *) &node->color;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
rc = ngx_memn2cmp(data, lr->data, len, (size_t) lr->len);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
if (rc == 0) {
|
|
|
|
ngx_queue_remove(&lr->queue);
|
|
|
|
ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
ms = (ngx_msec_int_t) (now - lr->last);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
if (excess < 0) {
|
|
|
|
excess = 0;
|
|
|
|
}
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
*ep = excess;
|
2012-01-30 18:17:56 +08:00
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
if ((ngx_uint_t) excess > limit->burst) {
|
|
|
|
return NGX_BUSY;
|
|
|
|
}
|
2012-01-30 18:17:56 +08:00
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
if (account) {
|
|
|
|
lr->excess = excess;
|
|
|
|
lr->last = now;
|
|
|
|
return NGX_OK;
|
2008-11-10 23:22:33 +08:00
|
|
|
}
|
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
lr->count++;
|
|
|
|
|
|
|
|
ctx->node = lr;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
node = (rc < 0) ? node->left : node->right;
|
2008-11-10 23:22:33 +08:00
|
|
|
}
|
|
|
|
|
2010-10-14 04:58:41 +08:00
|
|
|
*ep = 0;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 17:41:49 +08:00
|
|
|
size = offsetof(ngx_rbtree_node_t, color)
|
|
|
|
+ offsetof(ngx_http_limit_req_node_t, data)
|
|
|
|
+ len;
|
|
|
|
|
2012-01-30 18:01:39 +08:00
|
|
|
ngx_http_limit_req_expire(ctx, 1);
|
|
|
|
|
2012-01-30 17:41:49 +08:00
|
|
|
node = ngx_slab_alloc_locked(ctx->shpool, size);
|
|
|
|
|
|
|
|
if (node == NULL) {
|
|
|
|
ngx_http_limit_req_expire(ctx, 0);
|
|
|
|
|
|
|
|
node = ngx_slab_alloc_locked(ctx->shpool, size);
|
|
|
|
if (node == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node->key = hash;
|
|
|
|
|
|
|
|
ngx_rbtree_insert(&ctx->sh->rbtree, node);
|
|
|
|
|
|
|
|
lr = (ngx_http_limit_req_node_t *) &node->color;
|
|
|
|
|
|
|
|
ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
|
|
|
|
|
|
|
|
lr->len = (u_char) len;
|
|
|
|
lr->excess = 0;
|
|
|
|
|
|
|
|
ngx_memcpy(lr->data, data, len);
|
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
if (account) {
|
|
|
|
lr->last = now;
|
|
|
|
lr->count = 0;
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
lr->last = 0;
|
|
|
|
lr->count = 1;
|
|
|
|
|
|
|
|
ctx->node = lr;
|
|
|
|
|
|
|
|
return NGX_AGAIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_msec_t
|
|
|
|
ngx_http_limit_req_account(ngx_http_limit_req_limit_t *limits, ngx_uint_t n,
|
|
|
|
ngx_uint_t *ep, ngx_http_limit_req_limit_t **limit)
|
|
|
|
{
|
|
|
|
ngx_int_t excess;
|
|
|
|
ngx_time_t *tp;
|
|
|
|
ngx_msec_t now, delay, max_delay;
|
|
|
|
ngx_msec_int_t ms;
|
|
|
|
ngx_http_limit_req_ctx_t *ctx;
|
|
|
|
ngx_http_limit_req_node_t *lr;
|
|
|
|
|
|
|
|
excess = *ep;
|
|
|
|
|
|
|
|
if (excess == 0 || (*limit)->nodelay) {
|
|
|
|
max_delay = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
ctx = (*limit)->shm_zone->data;
|
|
|
|
max_delay = excess * 1000 / ctx->rate;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (n--) {
|
|
|
|
ctx = limits[n].shm_zone->data;
|
|
|
|
lr = ctx->node;
|
|
|
|
|
|
|
|
if (lr == NULL) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_shmtx_lock(&ctx->shpool->mutex);
|
|
|
|
|
|
|
|
tp = ngx_timeofday();
|
|
|
|
|
|
|
|
now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
|
|
|
|
ms = (ngx_msec_int_t) (now - lr->last);
|
|
|
|
|
|
|
|
excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
|
|
|
|
|
|
|
|
if (excess < 0) {
|
|
|
|
excess = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
lr->last = now;
|
|
|
|
lr->excess = excess;
|
|
|
|
lr->count--;
|
|
|
|
|
|
|
|
ngx_shmtx_unlock(&ctx->shpool->mutex);
|
|
|
|
|
|
|
|
ctx->node = NULL;
|
|
|
|
|
|
|
|
if (limits[n].nodelay) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
delay = excess * 1000 / ctx->rate;
|
|
|
|
|
|
|
|
if (delay > max_delay) {
|
|
|
|
max_delay = delay;
|
|
|
|
*ep = excess;
|
|
|
|
*limit = &limits[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return max_delay;
|
2008-11-10 23:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx, ngx_uint_t n)
|
|
|
|
{
|
2008-11-14 21:25:44 +08:00
|
|
|
ngx_int_t excess;
|
2008-11-10 23:22:33 +08:00
|
|
|
ngx_time_t *tp;
|
|
|
|
ngx_msec_t now;
|
|
|
|
ngx_queue_t *q;
|
|
|
|
ngx_msec_int_t ms;
|
|
|
|
ngx_rbtree_node_t *node;
|
2008-12-08 22:13:36 +08:00
|
|
|
ngx_http_limit_req_node_t *lr;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
|
|
|
tp = ngx_timeofday();
|
|
|
|
|
|
|
|
now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* n == 1 deletes one or two zero rate entries
|
|
|
|
* n == 0 deletes oldest entry by force
|
|
|
|
* and one or two zero rate entries
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (n < 3) {
|
|
|
|
|
2009-04-19 03:27:28 +08:00
|
|
|
if (ngx_queue_empty(&ctx->sh->queue)) {
|
2008-11-10 23:22:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-04-19 03:27:28 +08:00
|
|
|
q = ngx_queue_last(&ctx->sh->queue);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2008-12-08 22:13:36 +08:00
|
|
|
lr = ngx_queue_data(q, ngx_http_limit_req_node_t, queue);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
if (lr->count) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There is not much sense in looking further,
|
|
|
|
* because we bump nodes on the lookup stage.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-10 23:22:33 +08:00
|
|
|
if (n++ != 0) {
|
|
|
|
|
2008-12-08 22:13:36 +08:00
|
|
|
ms = (ngx_msec_int_t) (now - lr->last);
|
2008-11-10 23:22:33 +08:00
|
|
|
ms = ngx_abs(ms);
|
|
|
|
|
|
|
|
if (ms < 60000) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-12-08 22:13:36 +08:00
|
|
|
excess = lr->excess - ctx->rate * ms / 1000;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2008-11-14 21:25:44 +08:00
|
|
|
if (excess > 0) {
|
2008-11-10 23:22:33 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_queue_remove(q);
|
|
|
|
|
|
|
|
node = (ngx_rbtree_node_t *)
|
2008-12-08 22:13:36 +08:00
|
|
|
((u_char *) lr - offsetof(ngx_rbtree_node_t, color));
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2009-04-19 03:27:28 +08:00
|
|
|
ngx_rbtree_delete(&ctx->sh->rbtree, node);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
|
|
|
ngx_slab_free_locked(ctx->shpool, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_limit_req_init_zone(ngx_shm_zone_t *shm_zone, void *data)
|
|
|
|
{
|
|
|
|
ngx_http_limit_req_ctx_t *octx = data;
|
|
|
|
|
2009-03-28 01:00:42 +08:00
|
|
|
size_t len;
|
2008-11-10 23:22:33 +08:00
|
|
|
ngx_http_limit_req_ctx_t *ctx;
|
|
|
|
|
|
|
|
ctx = shm_zone->data;
|
|
|
|
|
|
|
|
if (octx) {
|
|
|
|
if (ngx_strcmp(ctx->var.data, octx->var.data) != 0) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
|
|
|
|
"limit_req \"%V\" uses the \"%V\" variable "
|
|
|
|
"while previously it used the \"%V\" variable",
|
2009-04-17 03:25:09 +08:00
|
|
|
&shm_zone->shm.name, &ctx->var, &octx->var);
|
2008-11-10 23:22:33 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-04-19 03:27:28 +08:00
|
|
|
ctx->sh = octx->sh;
|
2008-11-10 23:22:33 +08:00
|
|
|
ctx->shpool = octx->shpool;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
|
|
|
2009-04-19 03:27:28 +08:00
|
|
|
if (shm_zone->shm.exists) {
|
|
|
|
ctx->sh = ctx->shpool->data;
|
|
|
|
|
|
|
|
return NGX_OK;
|
2008-11-10 23:22:33 +08:00
|
|
|
}
|
|
|
|
|
2009-04-19 03:27:28 +08:00
|
|
|
ctx->sh = ngx_slab_alloc(ctx->shpool, sizeof(ngx_http_limit_req_shctx_t));
|
|
|
|
if (ctx->sh == NULL) {
|
2008-11-10 23:22:33 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2009-04-19 03:27:28 +08:00
|
|
|
ctx->shpool->data = ctx->sh;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2009-04-19 03:27:28 +08:00
|
|
|
ngx_rbtree_init(&ctx->sh->rbtree, &ctx->sh->sentinel,
|
|
|
|
ngx_http_limit_req_rbtree_insert_value);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2009-04-19 03:27:28 +08:00
|
|
|
ngx_queue_init(&ctx->sh->queue);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2009-04-17 03:25:09 +08:00
|
|
|
len = sizeof(" in limit_req zone \"\"") + shm_zone->shm.name.len;
|
2009-03-28 01:00:42 +08:00
|
|
|
|
|
|
|
ctx->shpool->log_ctx = ngx_slab_alloc(ctx->shpool, len);
|
|
|
|
if (ctx->shpool->log_ctx == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_sprintf(ctx->shpool->log_ctx, " in limit_req zone \"%V\"%Z",
|
2009-04-17 03:25:09 +08:00
|
|
|
&shm_zone->shm.name);
|
2009-03-28 01:00:42 +08:00
|
|
|
|
2008-11-10 23:22:33 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
|
|
|
ngx_http_limit_req_create_conf(ngx_conf_t *cf)
|
|
|
|
{
|
|
|
|
ngx_http_limit_req_conf_t *conf;
|
|
|
|
|
|
|
|
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_conf_t));
|
|
|
|
if (conf == NULL) {
|
2009-06-03 00:09:44 +08:00
|
|
|
return NULL;
|
2008-11-10 23:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set by ngx_pcalloc():
|
|
|
|
*
|
2012-01-30 18:17:56 +08:00
|
|
|
* conf->limits.elts = NULL;
|
2008-11-10 23:22:33 +08:00
|
|
|
*/
|
|
|
|
|
2009-10-06 18:14:21 +08:00
|
|
|
conf->limit_log_level = NGX_CONF_UNSET_UINT;
|
|
|
|
|
2008-11-10 23:22:33 +08:00
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
|
|
{
|
|
|
|
ngx_http_limit_req_conf_t *prev = parent;
|
|
|
|
ngx_http_limit_req_conf_t *conf = child;
|
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
if (conf->limits.elts == NULL) {
|
|
|
|
conf->limits = prev->limits;
|
2008-11-10 23:22:33 +08:00
|
|
|
}
|
|
|
|
|
2009-10-06 18:14:21 +08:00
|
|
|
ngx_conf_merge_uint_value(conf->limit_log_level, prev->limit_log_level,
|
|
|
|
NGX_LOG_ERR);
|
|
|
|
|
|
|
|
conf->delay_log_level = (conf->limit_log_level == NGX_LOG_INFO) ?
|
|
|
|
NGX_LOG_INFO : conf->limit_log_level + 1;
|
|
|
|
|
2008-11-10 23:22:33 +08:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
|
|
|
u_char *p;
|
2012-01-30 17:26:08 +08:00
|
|
|
size_t len;
|
|
|
|
ssize_t size;
|
2008-11-10 23:22:33 +08:00
|
|
|
ngx_str_t *value, name, s;
|
|
|
|
ngx_int_t rate, scale;
|
|
|
|
ngx_uint_t i;
|
|
|
|
ngx_shm_zone_t *shm_zone;
|
|
|
|
ngx_http_limit_req_ctx_t *ctx;
|
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
|
|
|
ctx = NULL;
|
|
|
|
size = 0;
|
|
|
|
rate = 1;
|
|
|
|
scale = 1;
|
|
|
|
name.len = 0;
|
|
|
|
|
|
|
|
for (i = 1; i < cf->args->nelts; i++) {
|
|
|
|
|
|
|
|
if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {
|
|
|
|
|
|
|
|
name.data = value[i].data + 5;
|
|
|
|
|
|
|
|
p = (u_char *) ngx_strchr(name.data, ':');
|
|
|
|
|
2012-01-30 17:26:08 +08:00
|
|
|
if (p == NULL) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid zone size \"%V\"", &value[i]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
2009-04-17 03:25:09 +08:00
|
|
|
|
2012-01-30 17:26:08 +08:00
|
|
|
name.len = p - name.data;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 17:26:08 +08:00
|
|
|
s.data = p + 1;
|
|
|
|
s.len = value[i].data + value[i].len - s.data;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 17:26:08 +08:00
|
|
|
size = ngx_parse_size(&s);
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 17:26:08 +08:00
|
|
|
if (size == NGX_ERROR) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid zone size \"%V\"", &value[i]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (size < (ssize_t) (8 * ngx_pagesize)) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"zone \"%V\" is too small", &value[i]);
|
|
|
|
return NGX_CONF_ERROR;
|
2008-11-10 23:22:33 +08:00
|
|
|
}
|
|
|
|
|
2012-01-30 17:26:08 +08:00
|
|
|
continue;
|
2008-11-10 23:22:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strncmp(value[i].data, "rate=", 5) == 0) {
|
|
|
|
|
|
|
|
len = value[i].len;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
rate = ngx_atoi(value[i].data + 5, len - 5);
|
|
|
|
if (rate <= NGX_ERROR) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid rate \"%V\"", &value[i]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value[i].data[0] == '$') {
|
|
|
|
|
|
|
|
value[i].len--;
|
|
|
|
value[i].data++;
|
|
|
|
|
|
|
|
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_ctx_t));
|
|
|
|
if (ctx == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->index = ngx_http_get_variable_index(cf, &value[i]);
|
|
|
|
if (ctx->index == NGX_ERROR) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->var = value[i];
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid parameter \"%V\"", &value[i]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-30 17:26:08 +08:00
|
|
|
if (name.len == 0) {
|
2008-11-10 23:22:33 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"\"%V\" must have \"zone\" parameter",
|
|
|
|
&cmd->name);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctx == NULL) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
2012-01-30 17:02:29 +08:00
|
|
|
"no variable is defined for %V \"%V\"",
|
|
|
|
&cmd->name, &name);
|
2008-11-10 23:22:33 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2008-11-14 21:25:44 +08:00
|
|
|
ctx->rate = rate * 1000 / scale;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
|
|
|
shm_zone = ngx_shared_memory_add(cf, &name, size,
|
|
|
|
&ngx_http_limit_req_module);
|
|
|
|
if (shm_zone == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shm_zone->data) {
|
|
|
|
ctx = shm_zone->data;
|
|
|
|
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
2012-01-30 17:02:29 +08:00
|
|
|
"%V \"%V\" is already bound to variable \"%V\"",
|
|
|
|
&cmd->name, &name, &ctx->var);
|
2008-11-10 23:22:33 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
shm_zone->init = ngx_http_limit_req_init_zone;
|
|
|
|
shm_zone->data = ctx;
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
2008-12-08 22:13:36 +08:00
|
|
|
ngx_http_limit_req_conf_t *lrcf = conf;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
ngx_int_t burst;
|
|
|
|
ngx_str_t *value, s;
|
|
|
|
ngx_uint_t i, nodelay;
|
|
|
|
ngx_shm_zone_t *shm_zone;
|
|
|
|
ngx_http_limit_req_limit_t *limit, *limits;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
shm_zone = NULL;
|
2008-11-10 23:22:33 +08:00
|
|
|
burst = 0;
|
2012-01-30 18:17:56 +08:00
|
|
|
nodelay = 0;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
|
|
|
for (i = 1; i < cf->args->nelts; i++) {
|
|
|
|
|
|
|
|
if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {
|
|
|
|
|
|
|
|
s.len = value[i].len - 5;
|
|
|
|
s.data = value[i].data + 5;
|
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
shm_zone = ngx_shared_memory_add(cf, &s, 0,
|
|
|
|
&ngx_http_limit_req_module);
|
|
|
|
if (shm_zone == NULL) {
|
2008-11-10 23:22:33 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_strncmp(value[i].data, "burst=", 6) == 0) {
|
|
|
|
|
2008-11-11 23:38:16 +08:00
|
|
|
burst = ngx_atoi(value[i].data + 6, value[i].len - 6);
|
2008-11-10 23:22:33 +08:00
|
|
|
if (burst <= 0) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid burst rate \"%V\"", &value[i]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-11-11 23:38:16 +08:00
|
|
|
if (ngx_strncmp(value[i].data, "nodelay", 7) == 0) {
|
2012-01-30 18:17:56 +08:00
|
|
|
nodelay = 1;
|
2008-11-10 23:22:33 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid parameter \"%V\"", &value[i]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
if (shm_zone == NULL) {
|
2008-11-10 23:22:33 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"\"%V\" must have \"zone\" parameter",
|
|
|
|
&cmd->name);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
if (shm_zone->data == NULL) {
|
2008-11-10 23:22:33 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"unknown limit_req_zone \"%V\"",
|
2012-01-30 18:17:56 +08:00
|
|
|
&shm_zone->shm.name);
|
2008-11-10 23:22:33 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2012-01-30 18:17:56 +08:00
|
|
|
limits = lrcf->limits.elts;
|
|
|
|
|
|
|
|
if (limits == NULL) {
|
|
|
|
if (ngx_array_init(&lrcf->limits, cf->pool, 1,
|
|
|
|
sizeof(ngx_http_limit_req_limit_t))
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < lrcf->limits.nelts; i++) {
|
|
|
|
if (shm_zone == limits[i].shm_zone) {
|
|
|
|
return "is duplicate";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
limit = ngx_array_push(&lrcf->limits);
|
|
|
|
|
|
|
|
limit->shm_zone = shm_zone;
|
|
|
|
limit->burst = burst * 1000;
|
|
|
|
limit->nodelay = nodelay;
|
2008-11-10 23:22:33 +08:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_limit_req_init(ngx_conf_t *cf)
|
|
|
|
{
|
|
|
|
ngx_http_handler_pt *h;
|
|
|
|
ngx_http_core_main_conf_t *cmcf;
|
|
|
|
|
|
|
|
cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
|
|
|
|
|
|
h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
|
|
|
|
if (h == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*h = ngx_http_limit_req_handler;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|