2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Igor Sysoev
|
2012-01-18 23:07:43 +08:00
|
|
|
* Copyright (C) Nginx, Inc.
|
2007-01-07 02:52:46 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2014-09-25 01:55:19 +08:00
|
|
|
u_char color;
|
|
|
|
u_char len;
|
|
|
|
u_short conn;
|
|
|
|
u_char data[1];
|
2011-11-14 22:04:42 +08:00
|
|
|
} ngx_http_limit_conn_node_t;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2014-09-25 01:55:19 +08:00
|
|
|
ngx_shm_zone_t *shm_zone;
|
|
|
|
ngx_rbtree_node_t *node;
|
2011-11-14 22:04:42 +08:00
|
|
|
} ngx_http_limit_conn_cleanup_t;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2014-09-25 01:55:19 +08:00
|
|
|
ngx_rbtree_t *rbtree;
|
|
|
|
ngx_http_complex_value_t key;
|
2011-11-14 22:04:42 +08:00
|
|
|
} ngx_http_limit_conn_ctx_t;
|
2007-01-08 02:52:34 +08:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2014-09-25 01:55:19 +08:00
|
|
|
ngx_shm_zone_t *shm_zone;
|
|
|
|
ngx_uint_t conn;
|
2011-11-14 22:04:42 +08:00
|
|
|
} ngx_http_limit_conn_limit_t;
|
2011-11-11 00:08:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2014-09-25 01:55:19 +08:00
|
|
|
ngx_array_t limits;
|
|
|
|
ngx_uint_t log_level;
|
|
|
|
ngx_uint_t status_code;
|
2011-11-14 22:04:42 +08:00
|
|
|
} ngx_http_limit_conn_conf_t;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
static ngx_rbtree_node_t *ngx_http_limit_conn_lookup(ngx_rbtree_t *rbtree,
|
2014-09-25 01:55:19 +08:00
|
|
|
ngx_str_t *key, uint32_t hash);
|
2011-11-14 22:04:42 +08:00
|
|
|
static void ngx_http_limit_conn_cleanup(void *data);
|
|
|
|
static ngx_inline void ngx_http_limit_conn_cleanup_all(ngx_pool_t *pool);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
static void *ngx_http_limit_conn_create_conf(ngx_conf_t *cf);
|
|
|
|
static char *ngx_http_limit_conn_merge_conf(ngx_conf_t *cf, void *parent,
|
2007-01-07 02:52:46 +08:00
|
|
|
void *child);
|
2011-11-11 00:25:08 +08:00
|
|
|
static char *ngx_http_limit_conn_zone(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
|
|
void *conf);
|
2007-01-07 02:52:46 +08:00
|
|
|
static char *ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
|
|
void *conf);
|
2011-11-14 22:04:42 +08:00
|
|
|
static ngx_int_t ngx_http_limit_conn_init(ngx_conf_t *cf);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
|
2009-10-06 18:14:29 +08:00
|
|
|
static ngx_conf_enum_t ngx_http_limit_conn_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 }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-03-18 22:50:29 +08:00
|
|
|
static ngx_conf_num_bounds_t ngx_http_limit_conn_status_bounds = {
|
|
|
|
ngx_conf_check_num_bounds, 400, 599
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
static ngx_command_t ngx_http_limit_conn_commands[] = {
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:25:08 +08:00
|
|
|
{ ngx_string("limit_conn_zone"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2,
|
|
|
|
ngx_http_limit_conn_zone,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
NULL },
|
|
|
|
|
2007-01-07 02:52:46 +08:00
|
|
|
{ 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 },
|
|
|
|
|
2009-10-06 18:14:29 +08:00
|
|
|
{ ngx_string("limit_conn_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,
|
2011-11-14 22:04:42 +08:00
|
|
|
offsetof(ngx_http_limit_conn_conf_t, log_level),
|
2009-10-06 18:14:29 +08:00
|
|
|
&ngx_http_limit_conn_log_levels },
|
|
|
|
|
2013-03-18 22:50:29 +08:00
|
|
|
{ ngx_string("limit_conn_status"),
|
|
|
|
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
|
|
ngx_conf_set_num_slot,
|
|
|
|
NGX_HTTP_LOC_CONF_OFFSET,
|
|
|
|
offsetof(ngx_http_limit_conn_conf_t, status_code),
|
|
|
|
&ngx_http_limit_conn_status_bounds },
|
|
|
|
|
2007-01-07 02:52:46 +08:00
|
|
|
ngx_null_command
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
static ngx_http_module_t ngx_http_limit_conn_module_ctx = {
|
2007-01-07 02:52:46 +08:00
|
|
|
NULL, /* preconfiguration */
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_init, /* postconfiguration */
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
NULL, /* create main configuration */
|
|
|
|
NULL, /* init main configuration */
|
|
|
|
|
|
|
|
NULL, /* create server configuration */
|
|
|
|
NULL, /* merge server configuration */
|
|
|
|
|
2012-02-28 19:31:05 +08:00
|
|
|
ngx_http_limit_conn_create_conf, /* create location configuration */
|
|
|
|
ngx_http_limit_conn_merge_conf /* merge location configuration */
|
2007-01-07 02:52:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_module_t ngx_http_limit_conn_module = {
|
2007-01-07 02:52:46 +08:00
|
|
|
NGX_MODULE_V1,
|
2011-11-14 22:04:42 +08:00
|
|
|
&ngx_http_limit_conn_module_ctx, /* module context */
|
|
|
|
ngx_http_limit_conn_commands, /* module directives */
|
2007-01-07 02:52:46 +08:00
|
|
|
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
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_handler(ngx_http_request_t *r)
|
2007-01-07 02:52:46 +08:00
|
|
|
{
|
2014-09-25 01:55:19 +08:00
|
|
|
size_t n;
|
2007-01-07 02:52:46 +08:00
|
|
|
uint32_t hash;
|
2014-09-25 01:55:19 +08:00
|
|
|
ngx_str_t key;
|
2011-11-11 00:08:13 +08:00
|
|
|
ngx_uint_t i;
|
2007-01-07 02:52:46 +08:00
|
|
|
ngx_slab_pool_t *shpool;
|
2011-11-10 23:51:55 +08:00
|
|
|
ngx_rbtree_node_t *node;
|
2007-01-07 02:52:46 +08:00
|
|
|
ngx_pool_cleanup_t *cln;
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_ctx_t *ctx;
|
|
|
|
ngx_http_limit_conn_node_t *lc;
|
|
|
|
ngx_http_limit_conn_conf_t *lccf;
|
|
|
|
ngx_http_limit_conn_limit_t *limits;
|
|
|
|
ngx_http_limit_conn_cleanup_t *lccln;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
if (r->main->limit_conn_set) {
|
2007-01-08 01:47:17 +08:00
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
lccf = ngx_http_get_module_loc_conf(r, ngx_http_limit_conn_module);
|
|
|
|
limits = lccf->limits.elts;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
for (i = 0; i < lccf->limits.nelts; i++) {
|
2011-11-11 00:08:13 +08:00
|
|
|
ctx = limits[i].shm_zone->data;
|
2007-01-08 02:52:34 +08:00
|
|
|
|
2014-09-25 01:55:19 +08:00
|
|
|
if (ngx_http_complex_value(r, &ctx->key, &key) != NGX_OK) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
2011-11-11 00:08:13 +08:00
|
|
|
}
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2014-09-25 01:55:19 +08:00
|
|
|
if (key.len == 0) {
|
2011-11-11 00:08:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
2007-01-12 00:50:06 +08:00
|
|
|
|
2014-09-25 01:55:19 +08:00
|
|
|
if (key.len > 255) {
|
2011-11-11 00:08:13 +08:00
|
|
|
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
2014-09-25 01:55:19 +08:00
|
|
|
"the value of the \"%V\" key "
|
|
|
|
"is more than 255 bytes: \"%V\"",
|
|
|
|
&ctx->key.value, &key);
|
2011-11-11 00:08:13 +08:00
|
|
|
continue;
|
|
|
|
}
|
2007-01-12 00:50:06 +08:00
|
|
|
|
2012-02-10 18:48:58 +08:00
|
|
|
r->main->limit_conn_set = 1;
|
|
|
|
|
2014-09-25 01:55:19 +08:00
|
|
|
hash = ngx_crc32_short(key.data, key.len);
|
2007-01-12 00:50:06 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
shpool = (ngx_slab_pool_t *) limits[i].shm_zone->shm.addr;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
ngx_shmtx_lock(&shpool->mutex);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2014-09-25 01:55:19 +08:00
|
|
|
node = ngx_http_limit_conn_lookup(ctx->rbtree, &key, hash);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
if (node == NULL) {
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
n = offsetof(ngx_rbtree_node_t, color)
|
2011-11-14 22:04:42 +08:00
|
|
|
+ offsetof(ngx_http_limit_conn_node_t, data)
|
2014-09-25 01:55:19 +08:00
|
|
|
+ key.len;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
node = ngx_slab_alloc_locked(shpool, n);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
if (node == NULL) {
|
|
|
|
ngx_shmtx_unlock(&shpool->mutex);
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_cleanup_all(r->pool);
|
2013-03-18 22:50:29 +08:00
|
|
|
return lccf->status_code;
|
2011-11-11 00:08:13 +08:00
|
|
|
}
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
lc = (ngx_http_limit_conn_node_t *) &node->color;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
node->key = hash;
|
2014-09-25 01:55:19 +08:00
|
|
|
lc->len = (u_char) key.len;
|
2011-11-14 22:04:42 +08:00
|
|
|
lc->conn = 1;
|
2014-09-25 01:55:19 +08:00
|
|
|
ngx_memcpy(lc->data, key.data, key.len);
|
2007-01-12 00:57:30 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
ngx_rbtree_insert(ctx->rbtree, node);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
} else {
|
2007-01-13 04:38:17 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
lc = (ngx_http_limit_conn_node_t *) &node->color;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
if ((ngx_uint_t) lc->conn >= limits[i].conn) {
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
ngx_shmtx_unlock(&shpool->mutex);
|
2008-12-08 22:12:29 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_log_error(lccf->log_level, r->connection->log, 0,
|
2011-11-11 00:08:13 +08:00
|
|
|
"limiting connections by zone \"%V\"",
|
|
|
|
&limits[i].shm_zone->shm.name);
|
2007-01-12 00:57:30 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_cleanup_all(r->pool);
|
2013-03-18 22:50:29 +08:00
|
|
|
return lccf->status_code;
|
2011-11-11 00:08:13 +08:00
|
|
|
}
|
2007-01-12 00:57:30 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
lc->conn++;
|
2011-11-10 23:51:55 +08:00
|
|
|
}
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
2012-07-20 16:21:59 +08:00
|
|
|
"limit conn: %08XD %d", node->key, lc->conn);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
ngx_shmtx_unlock(&shpool->mutex);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
cln = ngx_pool_cleanup_add(r->pool,
|
2011-11-14 22:04:42 +08:00
|
|
|
sizeof(ngx_http_limit_conn_cleanup_t));
|
2011-11-11 00:08:13 +08:00
|
|
|
if (cln == NULL) {
|
|
|
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
|
|
|
}
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
cln->handler = ngx_http_limit_conn_cleanup;
|
|
|
|
lccln = cln->data;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
lccln->shm_zone = limits[i].shm_zone;
|
|
|
|
lccln->node = node;
|
2011-11-11 00:08:13 +08:00
|
|
|
}
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
return NGX_DECLINED;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-13 04:38:17 +08:00
|
|
|
static void
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
2007-01-13 04:38:17 +08:00
|
|
|
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
|
|
{
|
2007-12-17 16:52:00 +08:00
|
|
|
ngx_rbtree_node_t **p;
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_node_t *lcn, *lcnt;
|
2007-01-13 04:38:17 +08:00
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
if (node->key < temp->key) {
|
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
p = &temp->left;
|
2007-01-13 04:38:17 +08:00
|
|
|
|
|
|
|
} else if (node->key > temp->key) {
|
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
p = &temp->right;
|
2007-01-13 04:38:17 +08:00
|
|
|
|
|
|
|
} else { /* node->key == temp->key */
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
lcn = (ngx_http_limit_conn_node_t *) &node->color;
|
|
|
|
lcnt = (ngx_http_limit_conn_node_t *) &temp->color;
|
2007-01-13 04:38:17 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
p = (ngx_memn2cmp(lcn->data, lcnt->data, lcn->len, lcnt->len) < 0)
|
2007-12-17 16:52:00 +08:00
|
|
|
? &temp->left : &temp->right;
|
|
|
|
}
|
2007-01-13 04:38:17 +08:00
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
if (*p == sentinel) {
|
|
|
|
break;
|
2007-01-13 04:38:17 +08:00
|
|
|
}
|
2007-12-17 16:52:00 +08:00
|
|
|
|
|
|
|
temp = *p;
|
2007-01-13 04:38:17 +08:00
|
|
|
}
|
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
*p = node;
|
2007-01-13 04:38:17 +08:00
|
|
|
node->parent = temp;
|
|
|
|
node->left = sentinel;
|
|
|
|
node->right = sentinel;
|
|
|
|
ngx_rbt_red(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-10 23:51:55 +08:00
|
|
|
static ngx_rbtree_node_t *
|
2014-09-25 01:55:19 +08:00
|
|
|
ngx_http_limit_conn_lookup(ngx_rbtree_t *rbtree, ngx_str_t *key, uint32_t hash)
|
2011-11-10 23:51:55 +08:00
|
|
|
{
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_rbtree_node_t *node, *sentinel;
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_node_t *lcn;
|
2011-11-10 23:51:55 +08:00
|
|
|
|
|
|
|
node = rbtree->root;
|
|
|
|
sentinel = rbtree->sentinel;
|
|
|
|
|
|
|
|
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
|
|
|
lcn = (ngx_http_limit_conn_node_t *) &node->color;
|
2011-11-10 23:51:55 +08:00
|
|
|
|
2014-09-25 01:55:19 +08:00
|
|
|
rc = ngx_memn2cmp(key->data, lcn->data, key->len, (size_t) lcn->len);
|
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
if (rc == 0) {
|
|
|
|
return node;
|
|
|
|
}
|
2011-11-10 23:51:55 +08:00
|
|
|
|
2012-02-28 06:15:39 +08:00
|
|
|
node = (rc < 0) ? node->left : node->right;
|
2011-11-10 23:51:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-07 02:52:46 +08:00
|
|
|
static void
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_cleanup(void *data)
|
2007-01-07 02:52:46 +08:00
|
|
|
{
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_cleanup_t *lccln = data;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
ngx_slab_pool_t *shpool;
|
|
|
|
ngx_rbtree_node_t *node;
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_ctx_t *ctx;
|
|
|
|
ngx_http_limit_conn_node_t *lc;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
ctx = lccln->shm_zone->data;
|
|
|
|
shpool = (ngx_slab_pool_t *) lccln->shm_zone->shm.addr;
|
|
|
|
node = lccln->node;
|
|
|
|
lc = (ngx_http_limit_conn_node_t *) &node->color;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
ngx_shmtx_lock(&shpool->mutex);
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, lccln->shm_zone->shm.log, 0,
|
2012-07-20 16:21:59 +08:00
|
|
|
"limit conn cleanup: %08XD %d", node->key, lc->conn);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
lc->conn--;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
if (lc->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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
static ngx_inline void
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_cleanup_all(ngx_pool_t *pool)
|
2011-11-11 00:08:13 +08:00
|
|
|
{
|
|
|
|
ngx_pool_cleanup_t *cln;
|
|
|
|
|
|
|
|
cln = pool->cleanup;
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
while (cln && cln->handler == ngx_http_limit_conn_cleanup) {
|
|
|
|
ngx_http_limit_conn_cleanup(cln->data);
|
2011-11-11 00:08:13 +08:00
|
|
|
cln = cln->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
pool->cleanup = cln;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-07 02:52:46 +08:00
|
|
|
static ngx_int_t
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_init_zone(ngx_shm_zone_t *shm_zone, void *data)
|
2007-01-07 02:52:46 +08:00
|
|
|
{
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_ctx_t *octx = data;
|
2007-01-09 23:59:20 +08:00
|
|
|
|
2009-03-28 01:00:42 +08:00
|
|
|
size_t len;
|
2007-01-08 02:52:34 +08:00
|
|
|
ngx_slab_pool_t *shpool;
|
|
|
|
ngx_rbtree_node_t *sentinel;
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_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) {
|
2014-09-25 01:55:19 +08:00
|
|
|
if (ctx->key.value.len != octx->key.value.len
|
|
|
|
|| ngx_strncmp(ctx->key.value.data, octx->key.value.data,
|
|
|
|
ctx->key.value.len)
|
|
|
|
!= 0)
|
|
|
|
{
|
2007-01-09 23:59:20 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
|
2014-09-25 01:55:19 +08:00
|
|
|
"limit_conn_zone \"%V\" uses the \"%V\" key "
|
|
|
|
"while previously it used the \"%V\" key",
|
|
|
|
&shm_zone->shm.name, &ctx->key.value,
|
|
|
|
&octx->key.value);
|
2007-01-09 23:59:20 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->rbtree = octx->rbtree;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
|
|
|
2009-04-19 03:27:28 +08:00
|
|
|
if (shm_zone->shm.exists) {
|
|
|
|
ctx->rbtree = shpool->data;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-04-19 03:27:28 +08:00
|
|
|
shpool->data = ctx->rbtree;
|
|
|
|
|
2007-01-07 02:52:46 +08:00
|
|
|
sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t));
|
|
|
|
if (sentinel == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-12-17 16:52:00 +08:00
|
|
|
ngx_rbtree_init(ctx->rbtree, sentinel,
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_rbtree_insert_value);
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:25:08 +08:00
|
|
|
len = sizeof(" in limit_conn_zone \"\"") + shm_zone->shm.name.len;
|
2009-03-28 01:00:42 +08:00
|
|
|
|
|
|
|
shpool->log_ctx = ngx_slab_alloc(shpool, len);
|
|
|
|
if (shpool->log_ctx == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-11-11 00:25:08 +08:00
|
|
|
ngx_sprintf(shpool->log_ctx, " in limit_conn_zone \"%V\"%Z",
|
2009-04-17 03:25:09 +08:00
|
|
|
&shm_zone->shm.name);
|
2009-03-28 01:00:42 +08:00
|
|
|
|
2007-01-07 02:52:46 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_create_conf(ngx_conf_t *cf)
|
2007-01-07 02:52:46 +08:00
|
|
|
{
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_conf_t *conf;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_conn_conf_t));
|
2007-01-07 02:52:46 +08:00
|
|
|
if (conf == NULL) {
|
2009-06-03 00:09:44 +08:00
|
|
|
return NULL;
|
2007-01-07 02:52:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set by ngx_pcalloc():
|
|
|
|
*
|
2011-11-11 00:08:13 +08:00
|
|
|
* conf->limits.elts = NULL;
|
2007-01-07 02:52:46 +08:00
|
|
|
*/
|
|
|
|
|
2009-10-06 18:14:29 +08:00
|
|
|
conf->log_level = NGX_CONF_UNSET_UINT;
|
2013-03-18 22:50:29 +08:00
|
|
|
conf->status_code = NGX_CONF_UNSET_UINT;
|
2009-10-06 18:14:29 +08:00
|
|
|
|
2007-01-07 02:52:46 +08:00
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static char *
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
2007-01-07 02:52:46 +08:00
|
|
|
{
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_conf_t *prev = parent;
|
|
|
|
ngx_http_limit_conn_conf_t *conf = child;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
if (conf->limits.elts == NULL) {
|
2011-12-26 03:32:31 +08:00
|
|
|
conf->limits = prev->limits;
|
2007-01-07 02:52:46 +08:00
|
|
|
}
|
|
|
|
|
2009-10-06 18:14:29 +08:00
|
|
|
ngx_conf_merge_uint_value(conf->log_level, prev->log_level, NGX_LOG_ERR);
|
2013-03-18 22:50:29 +08:00
|
|
|
ngx_conf_merge_uint_value(conf->status_code, prev->status_code,
|
|
|
|
NGX_HTTP_SERVICE_UNAVAILABLE);
|
2009-10-06 18:14:29 +08:00
|
|
|
|
2007-01-07 02:52:46 +08:00
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-11 00:25:08 +08:00
|
|
|
static char *
|
|
|
|
ngx_http_limit_conn_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
|
|
{
|
2014-09-25 01:55:19 +08:00
|
|
|
u_char *p;
|
|
|
|
ssize_t size;
|
|
|
|
ngx_str_t *value, name, s;
|
|
|
|
ngx_uint_t i;
|
|
|
|
ngx_shm_zone_t *shm_zone;
|
|
|
|
ngx_http_limit_conn_ctx_t *ctx;
|
|
|
|
ngx_http_compile_complex_value_t ccv;
|
2011-11-11 00:25:08 +08:00
|
|
|
|
|
|
|
value = cf->args->elts;
|
|
|
|
|
2014-09-25 01:55:19 +08:00
|
|
|
ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_conn_ctx_t));
|
|
|
|
if (ctx == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
|
|
|
|
|
|
|
|
ccv.cf = cf;
|
|
|
|
ccv.value = &value[1];
|
|
|
|
ccv.complex_value = &ctx->key;
|
|
|
|
|
|
|
|
if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-11-11 00:25:08 +08:00
|
|
|
size = 0;
|
|
|
|
name.len = 0;
|
|
|
|
|
2014-09-25 01:55:19 +08:00
|
|
|
for (i = 2; i < cf->args->nelts; i++) {
|
2011-11-11 00:25:08 +08:00
|
|
|
|
|
|
|
if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {
|
|
|
|
|
|
|
|
name.data = value[i].data + 5;
|
|
|
|
|
|
|
|
p = (u_char *) ngx_strchr(name.data, ':');
|
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid zone size \"%V\"", &value[i]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
name.len = p - name.data;
|
|
|
|
|
|
|
|
s.data = p + 1;
|
|
|
|
s.len = value[i].data + value[i].len - s.data;
|
|
|
|
|
|
|
|
size = ngx_parse_size(&s);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"invalid parameter \"%V\"", &value[i]);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name.len == 0) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"\"%V\" must have \"zone\" parameter",
|
|
|
|
&cmd->name);
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
shm_zone = ngx_shared_memory_add(cf, &name, size,
|
2011-11-14 22:04:42 +08:00
|
|
|
&ngx_http_limit_conn_module);
|
2011-11-11 00:25:08 +08:00
|
|
|
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,
|
2014-09-25 01:55:19 +08:00
|
|
|
"%V \"%V\" is already bound to key \"%V\"",
|
|
|
|
&cmd->name, &name, &ctx->key.value);
|
2011-11-11 00:25:08 +08:00
|
|
|
return NGX_CONF_ERROR;
|
2007-01-08 02:52:34 +08:00
|
|
|
}
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
shm_zone->init = ngx_http_limit_conn_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)
|
|
|
|
{
|
2011-11-11 00:08:13 +08:00
|
|
|
ngx_shm_zone_t *shm_zone;
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_conf_t *lccf = conf;
|
|
|
|
ngx_http_limit_conn_limit_t *limit, *limits;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
ngx_str_t *value;
|
2011-11-11 00:08:13 +08:00
|
|
|
ngx_int_t n;
|
|
|
|
ngx_uint_t i;
|
2008-12-08 22:08:44 +08:00
|
|
|
|
2007-01-07 02:52:46 +08:00
|
|
|
value = cf->args->elts;
|
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
|
2011-11-14 22:04:42 +08:00
|
|
|
&ngx_http_limit_conn_module);
|
2011-11-11 00:08:13 +08:00
|
|
|
if (shm_zone == NULL) {
|
2007-01-07 02:52:46 +08:00
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
limits = lccf->limits.elts;
|
2011-11-11 00:08:13 +08:00
|
|
|
|
|
|
|
if (limits == NULL) {
|
2011-11-14 22:04:42 +08:00
|
|
|
if (ngx_array_init(&lccf->limits, cf->pool, 1,
|
|
|
|
sizeof(ngx_http_limit_conn_limit_t))
|
2011-11-11 00:08:13 +08:00
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
for (i = 0; i < lccf->limits.nelts; i++) {
|
2011-11-11 00:08:13 +08:00
|
|
|
if (shm_zone == limits[i].shm_zone) {
|
|
|
|
return "is duplicate";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-01-12 00:50:06 +08:00
|
|
|
if (n > 65535) {
|
|
|
|
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
|
|
"connection limit must be less 65536");
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
limit = ngx_array_push(&lccf->limits);
|
2012-08-08 20:03:46 +08:00
|
|
|
if (limit == NULL) {
|
|
|
|
return NGX_CONF_ERROR;
|
|
|
|
}
|
|
|
|
|
2011-11-11 00:08:13 +08:00
|
|
|
limit->conn = n;
|
|
|
|
limit->shm_zone = shm_zone;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
return NGX_CONF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
2011-11-14 22:04:42 +08:00
|
|
|
ngx_http_limit_conn_init(ngx_conf_t *cf)
|
2007-01-07 02:52:46 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-14 22:04:42 +08:00
|
|
|
*h = ngx_http_limit_conn_handler;
|
2007-01-07 02:52:46 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|