3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
17 } ngx_http_limit_zone_node_t;
21 ngx_shm_zone_t *shm_zone;
22 ngx_rbtree_node_t *node;
23 } ngx_http_limit_zone_cleanup_t;
30 } ngx_http_limit_zone_ctx_t;
34 ngx_shm_zone_t *shm_zone;
36 } ngx_http_limit_zone_conf_t;
39 static void ngx_http_limit_zone_cleanup(void *data);
41 static void *ngx_http_limit_zone_create_conf(ngx_conf_t *cf);
42 static char *ngx_http_limit_zone_merge_conf(ngx_conf_t *cf, void *parent,
44 static char *ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd,
46 static char *ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd,
48 static ngx_int_t ngx_http_limit_zone_init(ngx_conf_t *cf);
51 static ngx_command_t ngx_http_limit_zone_commands[] = {
53 { ngx_string("limit_zone"),
54 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3,
60 { ngx_string("limit_conn"),
61 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2,
63 NGX_HTTP_LOC_CONF_OFFSET,
71 static ngx_http_module_t ngx_http_limit_zone_module_ctx = {
72 NULL, /* preconfiguration */
73 ngx_http_limit_zone_init, /* postconfiguration */
75 NULL, /* create main configuration */
76 NULL, /* init main configuration */
78 NULL, /* create server configuration */
79 NULL, /* merge server configuration */
81 ngx_http_limit_zone_create_conf, /* create location configration */
82 ngx_http_limit_zone_merge_conf /* merge location configration */
86 ngx_module_t ngx_http_limit_zone_module = {
88 &ngx_http_limit_zone_module_ctx, /* module context */
89 ngx_http_limit_zone_commands, /* module directives */
90 NGX_HTTP_MODULE, /* module type */
91 NULL, /* init master */
92 NULL, /* init module */
93 NULL, /* init process */
94 NULL, /* init thread */
95 NULL, /* exit thread */
96 NULL, /* exit process */
97 NULL, /* exit master */
103 ngx_http_limit_zone_handler(ngx_http_request_t *r)
108 ngx_slab_pool_t *shpool;
109 ngx_rbtree_node_t *node, *sentinel;
110 ngx_pool_cleanup_t *cln;
111 ngx_http_variable_value_t *vv;
112 ngx_http_limit_zone_ctx_t *ctx;
113 ngx_http_limit_zone_node_t *lz;
114 ngx_http_limit_zone_conf_t *lzcf;
115 ngx_http_limit_zone_cleanup_t *lzcln;
117 if (r->main->limit_zone_set) {
121 lzcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_zone_module);
123 if (lzcf->shm_zone == NULL) {
127 ctx = lzcf->shm_zone->data;
129 vv = ngx_http_get_indexed_variable(r, ctx->index);
131 if (vv == NULL || vv->not_found) {
142 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
143 "the value of the \"%V\" variable "
144 "is more than 255 bytes: \"%v\"",
149 r->main->limit_zone_set = 1;
151 hash = ngx_crc32_short(vv->data, len);
153 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_limit_zone_cleanup_t));
155 return NGX_HTTP_INTERNAL_SERVER_ERROR;
158 shpool = (ngx_slab_pool_t *) lzcf->shm_zone->shm.addr;
160 ngx_shmtx_lock(&shpool->mutex);
162 node = ctx->rbtree->root;
163 sentinel = ctx->rbtree->sentinel;
165 while (node != sentinel) {
167 if (hash < node->key) {
172 if (hash > node->key) {
177 /* hash == node->key */
180 lz = (ngx_http_limit_zone_node_t *) &node->color;
182 rc = ngx_memn2cmp(vv->data, lz->data, len, (size_t) lz->len);
185 if ((ngx_uint_t) lz->conn < lzcf->conn) {
190 ngx_shmtx_unlock(&shpool->mutex);
192 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
193 "limiting connections by zone \"%V\"",
194 &lzcf->shm_zone->name);
196 return NGX_HTTP_SERVICE_UNAVAILABLE;
199 node = (rc < 0) ? node->left : node->right;
201 } while (node != sentinel && hash == node->key);
206 n = offsetof(ngx_rbtree_node_t, color)
207 + offsetof(ngx_http_limit_zone_node_t, data)
210 node = ngx_slab_alloc_locked(shpool, n);
212 ngx_shmtx_unlock(&shpool->mutex);
214 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
215 "could not allocate memory in zone \"%V\"",
216 &lzcf->shm_zone->name);
218 return NGX_HTTP_SERVICE_UNAVAILABLE;
221 lz = (ngx_http_limit_zone_node_t *) &node->color;
224 lz->len = (u_char) len;
226 ngx_memcpy(lz->data, vv->data, len);
228 ngx_rbtree_insert(ctx->rbtree, node);
232 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
233 "limit zone: %08XD %d", node->key, lz->conn);
235 ngx_shmtx_unlock(&shpool->mutex);
237 cln->handler = ngx_http_limit_zone_cleanup;
240 lzcln->shm_zone = lzcf->shm_zone;
248 ngx_http_limit_zone_rbtree_insert_value(ngx_rbtree_node_t *temp,
249 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
251 ngx_rbtree_node_t **p;
252 ngx_http_limit_zone_node_t *lzn, *lznt;
256 if (node->key < temp->key) {
260 } else if (node->key > temp->key) {
264 } else { /* node->key == temp->key */
266 lzn = (ngx_http_limit_zone_node_t *) &node->color;
267 lznt = (ngx_http_limit_zone_node_t *) &temp->color;
269 p = (ngx_memn2cmp(lzn->data, lznt->data, lzn->len, lznt->len) < 0)
270 ? &temp->left : &temp->right;
273 if (*p == sentinel) {
282 node->left = sentinel;
283 node->right = sentinel;
289 ngx_http_limit_zone_cleanup(void *data)
291 ngx_http_limit_zone_cleanup_t *lzcln = data;
293 ngx_slab_pool_t *shpool;
294 ngx_rbtree_node_t *node;
295 ngx_http_limit_zone_ctx_t *ctx;
296 ngx_http_limit_zone_node_t *lz;
298 ctx = lzcln->shm_zone->data;
299 shpool = (ngx_slab_pool_t *) lzcln->shm_zone->shm.addr;
301 lz = (ngx_http_limit_zone_node_t *) &node->color;
303 ngx_shmtx_lock(&shpool->mutex);
305 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, lzcln->shm_zone->shm.log, 0,
306 "limit zone cleanup: %08XD %d", node->key, lz->conn);
311 ngx_rbtree_delete(ctx->rbtree, node);
312 ngx_slab_free_locked(shpool, node);
315 ngx_shmtx_unlock(&shpool->mutex);
320 ngx_http_limit_zone_init_zone(ngx_shm_zone_t *shm_zone, void *data)
322 ngx_http_limit_zone_ctx_t *octx = data;
324 ngx_slab_pool_t *shpool;
325 ngx_rbtree_node_t *sentinel;
326 ngx_http_limit_zone_ctx_t *ctx;
328 ctx = shm_zone->data;
331 if (ngx_strcmp(ctx->var.data, octx->var.data) != 0) {
332 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
333 "limit_zone \"%V\" uses the \"%V\" variable "
334 "while previously it used the \"%V\" variable",
335 &shm_zone->name, &ctx->var, &octx->var);
339 ctx->rbtree = octx->rbtree;
344 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
346 ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t));
347 if (ctx->rbtree == NULL) {
351 sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t));
352 if (sentinel == NULL) {
356 ngx_rbtree_init(ctx->rbtree, sentinel,
357 ngx_http_limit_zone_rbtree_insert_value);
364 ngx_http_limit_zone_create_conf(ngx_conf_t *cf)
366 ngx_http_limit_zone_conf_t *conf;
368 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_conf_t));
370 return NGX_CONF_ERROR;
374 * set by ngx_pcalloc():
376 * conf->shm_zone = NULL;
385 ngx_http_limit_zone_merge_conf(ngx_conf_t *cf, void *parent, void *child)
387 ngx_http_limit_zone_conf_t *prev = parent;
388 ngx_http_limit_zone_conf_t *conf = child;
390 if (conf->shm_zone == NULL) {
399 ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
403 ngx_shm_zone_t *shm_zone;
404 ngx_http_limit_zone_ctx_t *ctx;
406 value = cf->args->elts;
408 if (value[2].data[0] != '$') {
409 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
410 "invalid variable name \"%V\"", &value[2]);
411 return NGX_CONF_ERROR;
417 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_ctx_t));
419 return NGX_CONF_ERROR;
422 ctx->index = ngx_http_get_variable_index(cf, &value[2]);
423 if (ctx->index == NGX_ERROR) {
424 return NGX_CONF_ERROR;
429 n = ngx_parse_size(&value[3]);
431 if (n == NGX_ERROR) {
432 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
433 "invalid size of limit_zone \"%V\"", &value[3]);
434 return NGX_CONF_ERROR;
437 if (n < (ngx_int_t) (8 * ngx_pagesize)) {
438 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
439 "limit_zone \"%V\" is too small", &value[1]);
440 return NGX_CONF_ERROR;
444 shm_zone = ngx_shared_memory_add(cf, &value[1], n,
445 &ngx_http_limit_zone_module);
446 if (shm_zone == NULL) {
447 return NGX_CONF_ERROR;
450 if (shm_zone->data) {
451 ctx = shm_zone->data;
453 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
454 "limit_zone \"%V\" is already bound to variable \"%V\"",
455 &value[1], &ctx->var);
456 return NGX_CONF_ERROR;
459 shm_zone->init = ngx_http_limit_zone_init_zone;
460 shm_zone->data = ctx;
467 ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
469 ngx_http_limit_zone_conf_t *lzcf = conf;
474 if (lzcf->shm_zone) {
475 return "is duplicate";
478 value = cf->args->elts;
480 lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0,
481 &ngx_http_limit_zone_module);
482 if (lzcf->shm_zone == NULL) {
483 return NGX_CONF_ERROR;
486 n = ngx_atoi(value[2].data, value[2].len);
488 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
489 "invalid number of connections \"%V\"", &value[2]);
490 return NGX_CONF_ERROR;
494 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
495 "connection limit must be less 65536");
496 return NGX_CONF_ERROR;
506 ngx_http_limit_zone_init(ngx_conf_t *cf)
508 ngx_http_handler_pt *h;
509 ngx_http_core_main_conf_t *cmcf;
511 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
513 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
518 *h = ngx_http_limit_zone_handler;