2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Igor Sysoev
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2007-01-07 17:18:26 +08:00
|
|
|
u_char color;
|
|
|
|
u_char len;
|
2007-01-07 02:52:46 +08:00
|
|
|
u_short conn;
|
|
|
|
u_char data[1];
|
|
|
|
} ngx_http_limit_zone_node_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ngx_shm_zone_t *shm_zone;
|
|
|
|
ngx_rbtree_node_t *node;
|
|
|
|
} ngx_http_limit_zone_cleanup_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2007-01-08 02:52:34 +08:00
|
|
|
ngx_rbtree_t *rbtree;
|
2007-01-07 02:52:46 +08:00
|
|
|
ngx_int_t index;
|
2007-01-08 02:52:34 +08:00
|
|
|
ngx_str_t var;
|
|
|
|
} ngx_http_limit_zone_ctx_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
ngx_shm_zone_t *shm_zone;
|
2007-01-07 02:52:46 +08:00
|
|
|
ngx_uint_t conn;
|
|
|
|
} ngx_http_limit_zone_conf_t;
|
|
|
|
|
|
|
|
|
|
|
|
static void ngx_http_limit_zone_cleanup(void *data);
|
|
|
|
|
|
|
|
static void *ngx_http_limit_zone_create_conf(ngx_conf_t *cf);
|
|
|
|
static char *ngx_http_limit_zone_merge_conf(ngx_conf_t *cf, void *parent,
|
|
|
|
void *child);
|
|
|
|
static char *ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
|
|
void *conf);
|
|
|
|
static char *ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
|
|
void *conf);
|
|
|
|
static ngx_int_t ngx_http_limit_zone_init(ngx_conf_t *cf);
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_command_t ngx_http_limit_zone_commands[] = {
|
|
|
|
|
|
|
|
{ ngx_string("limit_zone"),
|
2007-01-08 02:52:34 +08:00
|
|
|
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3,
|
2007-01-07 02:52:46 +08:00
|
|
|
ngx_http_limit_zone,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
{ ngx_string("limit_conn"),
|
2007-01-08 02:52:34 +08:00
|
|
|
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
|
2007-01-07 02:52:46 +08:00
|
|
|
ngx_http_limit_conn,
|
|
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
|
|
|
0,
|
|
|
|
NULL },
|
|
|
|
|
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_http_module_t ngx_http_limit_zone_module_ctx = {
|
|
|
|
NULL, /* preconfiguration */
|
|
|
|
ngx_http_limit_zone_init, /* postconfiguration */
|
|
|
|
|
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
|
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
|
|
|
|
|
|
|
ngx_http_limit_zone_create_conf, /* create location configration */
|
|
|
|
ngx_http_limit_zone_merge_conf /* merge location configration */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
ngx_module_t ngx_http_limit_zone_module = {
|
|
|
|
NGX_MODULE_V1,
|
|
|
|
&ngx_http_limit_zone_module_ctx, /* module context */
|
|
|
|
ngx_http_limit_zone_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_zone_handler(ngx_http_request_t *r)
|
|
|
|
{
|
|
|
|
size_t len, n;
|
|
|
|
uint32_t hash;
|
|
|
|
ngx_slab_pool_t *shpool;
|
|
|
|
ngx_rbtree_node_t *node, *sentinel;
|
|
|
|
ngx_pool_cleanup_t *cln;
|
|
|
|
ngx_http_variable_value_t *vv;
|
2007-01-08 02:52:34 +08:00
|
|
|
ngx_http_limit_zone_ctx_t *ctx;
|
2007-01-07 02:52:46 +08:00
|
|
|
ngx_http_limit_zone_node_t *lz;
|
|
|
|
ngx_http_limit_zone_conf_t *lzcf;
|
|
|
|
ngx_http_limit_zone_cleanup_t *lzcln;
|
|
|
|
|
2007-01-10 00:26:53 +08:00
|
|
|
if (r->main->limit_zone_set) {
|
2007-01-08 01:47:17 +08:00
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
|
|
|
|
2007-01-07 02:52:46 +08:00
|
|
|
lzcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_zone_module);
|
|
|
|
|
|
|
|
if (lzcf->shm_zone == NULL) {
|
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
|
|
|
|
2007-01-08 02:52:34 +08:00
|
|
|
ctx = lzcf->shm_zone->data;
|
|
|
|
|
|
|
|
vv = ngx_http_get_indexed_variable(r, ctx->index);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
if (vv == NULL || vv->not_found) {
|
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
|
|
|
|
2007-01-08 01:47:17 +08:00
|
|
|
r->limit_zone_set = 1;
|
|
|
|
|
2007-01-07 02:52:46 +08:00
|
|
|
len = vv->len;
|
|
|
|
|
|
|
|
hash = ngx_crc32_short(vv->data, len);
|
|
|
|
|
|
|
|
cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_limit_zone_cleanup_t));
|
|
|
|
if (cln == NULL) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
shpool = (ngx_slab_pool_t *) lzcf->shm_zone->shm.addr;
|
|
|
|
|
|
|
|
ngx_shmtx_lock(&shpool->mutex);
|
|
|
|
|
2007-01-08 02:52:34 +08:00
|
|
|
node = ctx->rbtree->root;
|
|
|
|
sentinel = ctx->rbtree->sentinel;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
while (node != sentinel) {
|
|
|
|
|
|
|
|
if (hash < node->key) {
|
|
|
|
node = node->left;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hash > node->key) {
|
|
|
|
node = node->right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hash == node->key ){
|
2007-01-07 17:18:26 +08:00
|
|
|
lz = (ngx_http_limit_zone_node_t *) &node->color;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
if (len == (size_t) lz->len
|
|
|
|
&& ngx_strncmp(lz->data, vv->data, len) == 0)
|
|
|
|
{
|
2007-01-07 17:18:26 +08:00
|
|
|
if ((ngx_uint_t) lz->conn < lzcf->conn) {
|
2007-01-07 02:52:46 +08:00
|
|
|
lz->conn++;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_shmtx_unlock(&shpool->mutex);
|
|
|
|
|
|
|
|
return NGX_HTTP_SERVICE_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-07 17:18:26 +08:00
|
|
|
n = offsetof(ngx_rbtree_node_t, color)
|
2007-01-07 02:52:46 +08:00
|
|
|
+ offsetof(ngx_http_limit_zone_node_t, data)
|
|
|
|
+ len;
|
|
|
|
|
|
|
|
node = ngx_slab_alloc_locked(shpool, n);
|
|
|
|
if (node == NULL) {
|
|
|
|
ngx_shmtx_unlock(&shpool->mutex);
|
|
|
|
return NGX_HTTP_SERVICE_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
2007-01-07 17:18:26 +08:00
|
|
|
lz = (ngx_http_limit_zone_node_t *) &node->color;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
node->key = hash;
|
2007-01-07 17:18:26 +08:00
|
|
|
lz->len = (u_char) len;
|
2007-01-07 02:52:46 +08:00
|
|
|
lz->conn = 1;
|
|
|
|
ngx_memcpy(lz->data, vv->data, len);
|
|
|
|
|
2007-01-08 02:52:34 +08:00
|
|
|
ngx_rbtree_insert(ctx->rbtree, node);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
done:
|
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
|
|
"limit zone: %08XD %d", node->key, lz->conn);
|
|
|
|
|
|
|
|
ngx_shmtx_unlock(&shpool->mutex);
|
|
|
|
|
|
|
|
cln->handler = ngx_http_limit_zone_cleanup;
|
|
|
|
lzcln = cln->data;
|
|
|
|
|
|
|
|
lzcln->shm_zone = lzcf->shm_zone;
|
|
|
|
lzcln->node = node;
|
|
|
|
|
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_http_limit_zone_cleanup(void *data)
|
|
|
|
{
|
|
|
|
ngx_http_limit_zone_cleanup_t *lzcln = data;
|
|
|
|
|
|
|
|
ngx_slab_pool_t *shpool;
|
|
|
|
ngx_rbtree_node_t *node;
|
2007-01-08 02:52:34 +08:00
|
|
|
ngx_http_limit_zone_ctx_t *ctx;
|
2007-01-07 02:52:46 +08:00
|
|
|
ngx_http_limit_zone_node_t *lz;
|
|
|
|
|
2007-01-08 02:52:34 +08:00
|
|
|
ctx = lzcln->shm_zone->data;
|
2007-01-07 02:52:46 +08:00
|
|
|
shpool = (ngx_slab_pool_t *) lzcln->shm_zone->shm.addr;
|
|
|
|
node = lzcln->node;
|
2007-01-07 17:18:26 +08:00
|
|
|
lz = (ngx_http_limit_zone_node_t *) &node->color;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
ngx_shmtx_lock(&shpool->mutex);
|
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, lzcln->shm_zone->shm.log, 0,
|
|
|
|
"limit zone cleanup: %08XD %d", node->key, lz->conn);
|
|
|
|
|
|
|
|
lz->conn--;
|
|
|
|
|
|
|
|
if (lz->conn == 0) {
|
2007-01-08 02:52:34 +08:00
|
|
|
ngx_rbtree_delete(ctx->rbtree, node);
|
2007-01-07 02:52:46 +08:00
|
|
|
ngx_slab_free_locked(shpool, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_shmtx_unlock(&shpool->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
2007-01-09 23:59:20 +08:00
|
|
|
ngx_http_limit_zone_init_zone(ngx_shm_zone_t *shm_zone, void *data)
|
2007-01-07 02:52:46 +08:00
|
|
|
{
|
2007-01-09 23:59:20 +08:00
|
|
|
ngx_http_limit_zone_ctx_t *octx = data;
|
|
|
|
|
2007-01-08 02:52:34 +08:00
|
|
|
ngx_slab_pool_t *shpool;
|
|
|
|
ngx_rbtree_node_t *sentinel;
|
|
|
|
ngx_http_limit_zone_ctx_t *ctx;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2007-01-08 02:52:34 +08:00
|
|
|
ctx = shm_zone->data;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2007-01-09 23:59:20 +08:00
|
|
|
if (octx) {
|
|
|
|
if (ngx_strcmp(ctx->var.data, octx->var.data) != 0) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
|
2007-01-10 04:57:49 +08:00
|
|
|
"limit_zone \"%V\" uses the \"%V\" variable "
|
2007-01-09 23:59:20 +08:00
|
|
|
"while previously it used the \"%V\" variable",
|
|
|
|
&shm_zone->name, &ctx->var, &octx->var);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->rbtree = octx->rbtree;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
|
|
|
2007-01-08 02:52:34 +08:00
|
|
|
ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t));
|
|
|
|
if (ctx->rbtree == NULL) {
|
2007-01-07 02:52:46 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t));
|
|
|
|
if (sentinel == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_rbtree_sentinel_init(sentinel);
|
|
|
|
|
2007-01-08 02:52:34 +08:00
|
|
|
ctx->rbtree->root = sentinel;
|
|
|
|
ctx->rbtree->sentinel = sentinel;
|
|
|
|
ctx->rbtree->insert = ngx_rbtree_insert_value;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
|
|
|
ngx_http_limit_zone_create_conf(ngx_conf_t *cf)
|
|
|
|
{
|
|
|
|
ngx_http_limit_zone_conf_t *conf;
|
|
|
|
|
|
|
|
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_conf_t));
|
|
|
|
if (conf == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set by ngx_pcalloc():
|
|
|
|
*
|
|
|
|
* conf->shm_zone = NULL;
|
|
|
|
* conf->conn = 0;
|
|
|
|
*/
|
|
|
|
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_limit_zone_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
|
|
{
|
|
|
|
ngx_http_limit_zone_conf_t *prev = parent;
|
|
|
|
ngx_http_limit_zone_conf_t *conf = child;
|
|
|
|
|
|
|
|
if (conf->shm_zone == NULL) {
|
|
|
|
*conf = *prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
2007-01-08 02:52:34 +08:00
|
|
|
ssize_t n;
|
|
|
|
ngx_str_t *value;
|
|
|
|
ngx_shm_zone_t *shm_zone;
|
|
|
|
ngx_http_limit_zone_ctx_t *ctx;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
2007-01-08 02:52:34 +08:00
|
|
|
if (value[2].data[0] != '$') {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid variable name \"%V\"", &value[2]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
value[2].len--;
|
|
|
|
value[2].data++;
|
|
|
|
|
|
|
|
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_ctx_t));
|
|
|
|
if (ctx == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->index = ngx_http_get_variable_index(cf, &value[2]);
|
|
|
|
if (ctx->index == NGX_ERROR) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->var = value[2];
|
|
|
|
|
|
|
|
n = ngx_parse_size(&value[3]);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
if (n == NGX_ERROR) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
2007-01-08 02:52:34 +08:00
|
|
|
"invalid size of limit_zone \"%V\"", &value[3]);
|
2007-01-07 02:52:46 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n < (ngx_int_t) (8 * ngx_pagesize)) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"limit_zone \"%V\" is too small", &value[1]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
shm_zone = ngx_shared_memory_add(cf, &value[1], n,
|
|
|
|
&ngx_http_limit_zone_module);
|
|
|
|
if (shm_zone == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-01-08 02:52:34 +08:00
|
|
|
if (shm_zone->data) {
|
|
|
|
ctx = shm_zone->data;
|
|
|
|
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"limit_zone \"%V\" is already bound to variable \"%V\"",
|
|
|
|
&value[1], &ctx->var);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-01-07 02:52:46 +08:00
|
|
|
shm_zone->init = ngx_http_limit_zone_init_zone;
|
2007-01-08 02:52:34 +08:00
|
|
|
shm_zone->data = ctx;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
|
|
|
ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
|
|
|
ngx_http_limit_zone_conf_t *lzcf = conf;
|
|
|
|
|
|
|
|
ngx_int_t n;
|
|
|
|
ngx_str_t *value;
|
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
2007-01-08 02:52:34 +08:00
|
|
|
lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
|
|
|
|
&ngx_http_limit_zone_module);
|
|
|
|
if (lzcf->shm_zone == NULL) {
|
2007-01-07 02:52:46 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-01-08 02:52:34 +08:00
|
|
|
n = ngx_atoi(value[2].data, value[2].len);
|
|
|
|
if (n <= 0) {
|
2007-01-07 02:52:46 +08:00
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
2007-01-08 02:52:34 +08:00
|
|
|
"invalid number of connections \"%V\"", &value[2]);
|
2007-01-07 02:52:46 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
lzcf->conn = n;
|
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_limit_zone_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_zone_handler;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|