3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
18 ngx_uint_t excess; /* integer value, 1 corresponds to 0.001 r/s */
20 } ngx_http_limit_req_node_t;
26 ngx_slab_pool_t *shpool;
27 ngx_uint_t rate; /* integer value, 1 corresponds to 0.001 r/s */
30 } ngx_http_limit_req_ctx_t;
34 ngx_shm_zone_t *shm_zone;
35 ngx_uint_t burst; /* integer value, 1 corresponds to 0.001 r/s */
36 ngx_uint_t nodelay;/* unsigned nodelay:1 */
37 } ngx_http_limit_req_conf_t;
40 static void ngx_http_limit_req_delay(ngx_http_request_t *r);
41 static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf,
42 ngx_uint_t hash, u_char *data, size_t len, ngx_http_limit_req_node_t **lrp);
43 static void ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx,
46 static void *ngx_http_limit_req_create_conf(ngx_conf_t *cf);
47 static char *ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent,
49 static char *ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd,
51 static char *ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd,
53 static ngx_int_t ngx_http_limit_req_init(ngx_conf_t *cf);
56 static ngx_command_t ngx_http_limit_req_commands[] = {
58 { ngx_string("limit_req_zone"),
59 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3,
60 ngx_http_limit_req_zone,
65 { ngx_string("limit_req"),
66 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
68 NGX_HTTP_LOC_CONF_OFFSET,
76 static ngx_http_module_t ngx_http_limit_req_module_ctx = {
77 NULL, /* preconfiguration */
78 ngx_http_limit_req_init, /* postconfiguration */
80 NULL, /* create main configuration */
81 NULL, /* init main configuration */
83 NULL, /* create server configuration */
84 NULL, /* merge server configuration */
86 ngx_http_limit_req_create_conf, /* create location configration */
87 ngx_http_limit_req_merge_conf /* merge location configration */
91 ngx_module_t ngx_http_limit_req_module = {
93 &ngx_http_limit_req_module_ctx, /* module context */
94 ngx_http_limit_req_commands, /* module directives */
95 NGX_HTTP_MODULE, /* module type */
96 NULL, /* init master */
97 NULL, /* init module */
98 NULL, /* init process */
99 NULL, /* init thread */
100 NULL, /* exit thread */
101 NULL, /* exit process */
102 NULL, /* exit master */
103 NGX_MODULE_V1_PADDING
108 ngx_http_limit_req_handler(ngx_http_request_t *r)
115 ngx_rbtree_node_t *node;
116 ngx_http_variable_value_t *vv;
117 ngx_http_limit_req_ctx_t *ctx;
118 ngx_http_limit_req_node_t *lr;
119 ngx_http_limit_req_conf_t *lrcf;
121 if (r->main->limit_req_set) {
125 lrcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_req_module);
127 if (lrcf->shm_zone == NULL) {
131 ctx = lrcf->shm_zone->data;
133 vv = ngx_http_get_indexed_variable(r, ctx->index);
135 if (vv == NULL || vv->not_found) {
146 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
147 "the value of the \"%V\" variable "
148 "is more than 65535 bytes: \"%v\"",
153 r->main->limit_req_set = 1;
155 hash = ngx_crc32_short(vv->data, len);
157 ngx_shmtx_lock(&ctx->shpool->mutex);
159 ngx_http_limit_req_expire(ctx, 1);
161 rc = ngx_http_limit_req_lookup(lrcf, hash, vv->data, len, &lr);
164 ngx_queue_remove(&lr->queue);
166 ngx_queue_insert_head(ctx->queue, &lr->queue);
174 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
175 "limit_req: %i %ui.%03ui", rc, excess / 1000, excess % 1000);
177 if (rc == NGX_BUSY) {
178 ngx_shmtx_unlock(&ctx->shpool->mutex);
180 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
181 "limiting requests, excess: %ui.%03ui by zone \"%V\"",
182 excess / 1000, excess % 1000, &lrcf->shm_zone->name);
184 return NGX_HTTP_SERVICE_UNAVAILABLE;
187 if (rc == NGX_AGAIN) {
188 ngx_shmtx_unlock(&ctx->shpool->mutex);
194 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
195 "delaying request, excess: %ui.%03ui, by zone \"%V\"",
196 excess / 1000, excess % 1000, &lrcf->shm_zone->name);
198 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
199 return NGX_HTTP_INTERNAL_SERVER_ERROR;
202 r->read_event_handler = ngx_http_test_reading;
203 r->write_event_handler = ngx_http_limit_req_delay;
204 ngx_add_timer(r->connection->write, (ngx_msec_t) excess);
213 /* rc == NGX_DECLINED */
215 n = offsetof(ngx_rbtree_node_t, color)
216 + offsetof(ngx_http_limit_req_node_t, data)
219 node = ngx_slab_alloc_locked(ctx->shpool, n);
222 ngx_http_limit_req_expire(ctx, 0);
224 node = ngx_slab_alloc_locked(ctx->shpool, n);
226 ngx_shmtx_unlock(&ctx->shpool->mutex);
228 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
229 "could not allocate memory in zone \"%V\"",
230 &lrcf->shm_zone->name);
232 return NGX_HTTP_SERVICE_UNAVAILABLE;
236 lr = (ngx_http_limit_req_node_t *) &node->color;
239 lr->len = (u_char) len;
241 tp = ngx_timeofday();
242 lr->last = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
245 ngx_memcpy(lr->data, vv->data, len);
247 ngx_rbtree_insert(ctx->rbtree, node);
249 ngx_queue_insert_head(ctx->queue, &lr->queue);
253 ngx_shmtx_unlock(&ctx->shpool->mutex);
260 ngx_http_limit_req_delay(ngx_http_request_t *r)
262 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
265 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
266 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
270 r->read_event_handler = ngx_http_block_reading;
271 r->write_event_handler = ngx_http_core_run_phases;
273 ngx_http_core_run_phases(r);
278 ngx_http_limit_req_rbtree_insert_value(ngx_rbtree_node_t *temp,
279 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
281 ngx_rbtree_node_t **p;
282 ngx_http_limit_req_node_t *lrn, *lrnt;
286 if (node->key < temp->key) {
290 } else if (node->key > temp->key) {
294 } else { /* node->key == temp->key */
296 lrn = (ngx_http_limit_req_node_t *) &node->color;
297 lrnt = (ngx_http_limit_req_node_t *) &temp->color;
299 p = (ngx_memn2cmp(lrn->data, lrnt->data, lrn->len, lrnt->len) < 0)
300 ? &temp->left : &temp->right;
303 if (*p == sentinel) {
312 node->left = sentinel;
313 node->right = sentinel;
319 ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, ngx_uint_t hash,
320 u_char *data, size_t len, ngx_http_limit_req_node_t **lrp)
322 ngx_int_t rc, excess;
326 ngx_rbtree_node_t *node, *sentinel;
327 ngx_http_limit_req_ctx_t *ctx;
328 ngx_http_limit_req_node_t *lr;
330 ctx = lrcf->shm_zone->data;
332 node = ctx->rbtree->root;
333 sentinel = ctx->rbtree->sentinel;
335 while (node != sentinel) {
337 if (hash < node->key) {
342 if (hash > node->key) {
347 /* hash == node->key */
350 lr = (ngx_http_limit_req_node_t *) &node->color;
352 rc = ngx_memn2cmp(data, lr->data, len, (size_t) lr->len);
356 tp = ngx_timeofday();
358 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
359 ms = (ngx_msec_int_t) (now - lr->last);
361 excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
372 if ((ngx_uint_t) excess > lrcf->burst) {
383 node = (rc < 0) ? node->left : node->right;
385 } while (node != sentinel && hash == node->key);
397 ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx, ngx_uint_t n)
404 ngx_rbtree_node_t *node;
405 ngx_http_limit_req_node_t *lr;
407 tp = ngx_timeofday();
409 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
412 * n == 1 deletes one or two zero rate entries
413 * n == 0 deletes oldest entry by force
414 * and one or two zero rate entries
419 if (ngx_queue_empty(ctx->queue)) {
423 q = ngx_queue_last(ctx->queue);
425 lr = ngx_queue_data(q, ngx_http_limit_req_node_t, queue);
429 ms = (ngx_msec_int_t) (now - lr->last);
436 excess = lr->excess - ctx->rate * ms / 1000;
445 node = (ngx_rbtree_node_t *)
446 ((u_char *) lr - offsetof(ngx_rbtree_node_t, color));
448 ngx_rbtree_delete(ctx->rbtree, node);
450 ngx_slab_free_locked(ctx->shpool, node);
456 ngx_http_limit_req_init_zone(ngx_shm_zone_t *shm_zone, void *data)
458 ngx_http_limit_req_ctx_t *octx = data;
460 ngx_rbtree_node_t *sentinel;
461 ngx_http_limit_req_ctx_t *ctx;
463 ctx = shm_zone->data;
466 if (ngx_strcmp(ctx->var.data, octx->var.data) != 0) {
467 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
468 "limit_req \"%V\" uses the \"%V\" variable "
469 "while previously it used the \"%V\" variable",
470 &shm_zone->name, &ctx->var, &octx->var);
474 ctx->rbtree = octx->rbtree;
475 ctx->queue = octx->queue;
476 ctx->shpool = octx->shpool;
481 ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
483 ctx->rbtree = ngx_slab_alloc(ctx->shpool, sizeof(ngx_rbtree_t));
484 if (ctx->rbtree == NULL) {
488 sentinel = ngx_slab_alloc(ctx->shpool, sizeof(ngx_rbtree_node_t));
489 if (sentinel == NULL) {
493 ngx_rbtree_init(ctx->rbtree, sentinel,
494 ngx_http_limit_req_rbtree_insert_value);
496 ctx->queue = ngx_slab_alloc(ctx->shpool, sizeof(ngx_queue_t));
497 if (ctx->queue == NULL) {
501 ngx_queue_init(ctx->queue);
508 ngx_http_limit_req_create_conf(ngx_conf_t *cf)
510 ngx_http_limit_req_conf_t *conf;
512 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_conf_t));
514 return NGX_CONF_ERROR;
518 * set by ngx_pcalloc():
520 * conf->shm_zone = NULL;
530 ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent, void *child)
532 ngx_http_limit_req_conf_t *prev = parent;
533 ngx_http_limit_req_conf_t *conf = child;
535 if (conf->shm_zone == NULL) {
544 ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
548 ngx_str_t *value, name, s;
549 ngx_int_t rate, scale;
551 ngx_shm_zone_t *shm_zone;
552 ngx_http_limit_req_ctx_t *ctx;
554 value = cf->args->elts;
562 for (i = 1; i < cf->args->nelts; i++) {
564 if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {
566 name.data = value[i].data + 5;
568 p = (u_char *) ngx_strchr(name.data, ':');
571 name.len = p - name.data;
575 s.len = value[i].data + value[i].len - p;
578 size = ngx_parse_size(&s);
584 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
585 "invalid zone size \"%V\"", &value[i]);
586 return NGX_CONF_ERROR;
589 if (ngx_strncmp(value[i].data, "rate=", 5) == 0) {
592 p = value[i].data + len - 3;
594 if (ngx_strncmp(p, "r/s", 3) == 0) {
598 } else if (ngx_strncmp(p, "r/m", 3) == 0) {
603 rate = ngx_atoi(value[i].data + 5, len - 5);
604 if (rate <= NGX_ERROR) {
605 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
606 "invalid rate \"%V\"", &value[i]);
607 return NGX_CONF_ERROR;
613 if (value[i].data[0] == '$') {
618 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_ctx_t));
620 return NGX_CONF_ERROR;
623 ctx->index = ngx_http_get_variable_index(cf, &value[i]);
624 if (ctx->index == NGX_ERROR) {
625 return NGX_CONF_ERROR;
633 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
634 "invalid parameter \"%V\"", &value[i]);
635 return NGX_CONF_ERROR;
638 if (name.len == 0 || size == 0) {
639 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
640 "\"%V\" must have \"zone\" parameter",
642 return NGX_CONF_ERROR;
646 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
647 "no variable is defined for limit_req_zone \"%V\"",
649 return NGX_CONF_ERROR;
652 ctx->rate = rate * 1000 / scale;
654 shm_zone = ngx_shared_memory_add(cf, &name, size,
655 &ngx_http_limit_req_module);
656 if (shm_zone == NULL) {
657 return NGX_CONF_ERROR;
660 if (shm_zone->data) {
661 ctx = shm_zone->data;
663 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
664 "limit_req_zone \"%V\" is already bound to variable \"%V\"",
665 &value[1], &ctx->var);
666 return NGX_CONF_ERROR;
669 shm_zone->init = ngx_http_limit_req_init_zone;
670 shm_zone->data = ctx;
677 ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
679 ngx_http_limit_req_conf_t *lrcf = conf;
685 if (lrcf->shm_zone) {
686 return "is duplicate";
689 value = cf->args->elts;
693 for (i = 1; i < cf->args->nelts; i++) {
695 if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {
697 s.len = value[i].len - 5;
698 s.data = value[i].data + 5;
700 lrcf->shm_zone = ngx_shared_memory_add(cf, &s, 0,
701 &ngx_http_limit_req_module);
702 if (lrcf->shm_zone == NULL) {
703 return NGX_CONF_ERROR;
709 if (ngx_strncmp(value[i].data, "burst=", 6) == 0) {
711 burst = ngx_atoi(value[i].data + 6, value[i].len - 6);
713 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
714 "invalid burst rate \"%V\"", &value[i]);
715 return NGX_CONF_ERROR;
721 if (ngx_strncmp(value[i].data, "nodelay", 7) == 0) {
726 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
727 "invalid parameter \"%V\"", &value[i]);
728 return NGX_CONF_ERROR;
731 if (lrcf->shm_zone == NULL) {
732 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
733 "\"%V\" must have \"zone\" parameter",
735 return NGX_CONF_ERROR;
738 if (lrcf->shm_zone->data == NULL) {
739 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
740 "unknown limit_req_zone \"%V\"",
741 &lrcf->shm_zone->name);
742 return NGX_CONF_ERROR;
745 lrcf->burst = burst * 1000;
752 ngx_http_limit_req_init(ngx_conf_t *cf)
754 ngx_http_handler_pt *h;
755 ngx_http_core_main_conf_t *cmcf;
757 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
759 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
764 *h = ngx_http_limit_req_handler;