src/http/modules/ngx_http_limit_req_module.c - nginx-1.7.10

Global variables defined

Data types defined

Functions defined

Source code


  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */


  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>


  8. typedef struct {
  9.     u_char                       color;
  10.     u_char                       dummy;
  11.     u_short                      len;
  12.     ngx_queue_t                  queue;
  13.     ngx_msec_t                   last;
  14.     /* integer value, 1 corresponds to 0.001 r/s */
  15.     ngx_uint_t                   excess;
  16.     ngx_uint_t                   count;
  17.     u_char                       data[1];
  18. } ngx_http_limit_req_node_t;


  19. typedef struct {
  20.     ngx_rbtree_t                  rbtree;
  21.     ngx_rbtree_node_t             sentinel;
  22.     ngx_queue_t                   queue;
  23. } ngx_http_limit_req_shctx_t;


  24. typedef struct {
  25.     ngx_http_limit_req_shctx_t  *sh;
  26.     ngx_slab_pool_t             *shpool;
  27.     /* integer value, 1 corresponds to 0.001 r/s */
  28.     ngx_uint_t                   rate;
  29.     ngx_http_complex_value_t     key;
  30.     ngx_http_limit_req_node_t   *node;
  31. } ngx_http_limit_req_ctx_t;


  32. typedef struct {
  33.     ngx_shm_zone_t              *shm_zone;
  34.     /* integer value, 1 corresponds to 0.001 r/s */
  35.     ngx_uint_t                   burst;
  36.     ngx_uint_t                   nodelay; /* unsigned  nodelay:1 */
  37. } ngx_http_limit_req_limit_t;


  38. typedef struct {
  39.     ngx_array_t                  limits;
  40.     ngx_uint_t                   limit_log_level;
  41.     ngx_uint_t                   delay_log_level;
  42.     ngx_uint_t                   status_code;
  43. } ngx_http_limit_req_conf_t;


  44. static void ngx_http_limit_req_delay(ngx_http_request_t *r);
  45. static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_limit_t *limit,
  46.     ngx_uint_t hash, ngx_str_t *key, ngx_uint_t *ep, ngx_uint_t account);
  47. static ngx_msec_t ngx_http_limit_req_account(ngx_http_limit_req_limit_t *limits,
  48.     ngx_uint_t n, ngx_uint_t *ep, ngx_http_limit_req_limit_t **limit);
  49. static void ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx,
  50.     ngx_uint_t n);

  51. static void *ngx_http_limit_req_create_conf(ngx_conf_t *cf);
  52. static char *ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent,
  53.     void *child);
  54. static char *ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd,
  55.     void *conf);
  56. static char *ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd,
  57.     void *conf);
  58. static ngx_int_t ngx_http_limit_req_init(ngx_conf_t *cf);


  59. static ngx_conf_enum_t  ngx_http_limit_req_log_levels[] = {
  60.     { ngx_string("info"), NGX_LOG_INFO },
  61.     { ngx_string("notice"), NGX_LOG_NOTICE },
  62.     { ngx_string("warn"), NGX_LOG_WARN },
  63.     { ngx_string("error"), NGX_LOG_ERR },
  64.     { ngx_null_string, 0 }
  65. };


  66. static ngx_conf_num_bounds_t  ngx_http_limit_req_status_bounds = {
  67.     ngx_conf_check_num_bounds, 400, 599
  68. };


  69. static ngx_command_t  ngx_http_limit_req_commands[] = {

  70.     { ngx_string("limit_req_zone"),
  71.       NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3,
  72.       ngx_http_limit_req_zone,
  73.       0,
  74.       0,
  75.       NULL },

  76.     { ngx_string("limit_req"),
  77.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
  78.       ngx_http_limit_req,
  79.       NGX_HTTP_LOC_CONF_OFFSET,
  80.       0,
  81.       NULL },

  82.     { ngx_string("limit_req_log_level"),
  83.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  84.       ngx_conf_set_enum_slot,
  85.       NGX_HTTP_LOC_CONF_OFFSET,
  86.       offsetof(ngx_http_limit_req_conf_t, limit_log_level),
  87.       &ngx_http_limit_req_log_levels },

  88.     { ngx_string("limit_req_status"),
  89.       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
  90.       ngx_conf_set_num_slot,
  91.       NGX_HTTP_LOC_CONF_OFFSET,
  92.       offsetof(ngx_http_limit_req_conf_t, status_code),
  93.       &ngx_http_limit_req_status_bounds },

  94.       ngx_null_command
  95. };


  96. static ngx_http_module_t  ngx_http_limit_req_module_ctx = {
  97.     NULL,                                  /* preconfiguration */
  98.     ngx_http_limit_req_init,               /* postconfiguration */

  99.     NULL,                                  /* create main configuration */
  100.     NULL,                                  /* init main configuration */

  101.     NULL,                                  /* create server configuration */
  102.     NULL,                                  /* merge server configuration */

  103.     ngx_http_limit_req_create_conf,        /* create location configuration */
  104.     ngx_http_limit_req_merge_conf          /* merge location configuration */
  105. };


  106. ngx_module_t  ngx_http_limit_req_module = {
  107.     NGX_MODULE_V1,
  108.     &ngx_http_limit_req_module_ctx,        /* module context */
  109.     ngx_http_limit_req_commands,           /* module directives */
  110.     NGX_HTTP_MODULE,                       /* module type */
  111.     NULL,                                  /* init master */
  112.     NULL,                                  /* init module */
  113.     NULL,                                  /* init process */
  114.     NULL,                                  /* init thread */
  115.     NULL,                                  /* exit thread */
  116.     NULL,                                  /* exit process */
  117.     NULL,                                  /* exit master */
  118.     NGX_MODULE_V1_PADDING
  119. };


  120. static ngx_int_t
  121. ngx_http_limit_req_handler(ngx_http_request_t *r)
  122. {
  123.     uint32_t                     hash;
  124.     ngx_str_t                    key;
  125.     ngx_int_t                    rc;
  126.     ngx_uint_t                   n, excess;
  127.     ngx_msec_t                   delay;
  128.     ngx_http_limit_req_ctx_t    *ctx;
  129.     ngx_http_limit_req_conf_t   *lrcf;
  130.     ngx_http_limit_req_limit_t  *limit, *limits;

  131.     if (r->main->limit_req_set) {
  132.         return NGX_DECLINED;
  133.     }

  134.     lrcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_req_module);
  135.     limits = lrcf->limits.elts;

  136.     excess = 0;

  137.     rc = NGX_DECLINED;

  138. #if (NGX_SUPPRESS_WARN)
  139.     limit = NULL;
  140. #endif

  141.     for (n = 0; n < lrcf->limits.nelts; n++) {

  142.         limit = &limits[n];

  143.         ctx = limit->shm_zone->data;

  144.         if (ngx_http_complex_value(r, &ctx->key, &key) != NGX_OK) {
  145.             return NGX_HTTP_INTERNAL_SERVER_ERROR;
  146.         }

  147.         if (key.len == 0) {
  148.             continue;
  149.         }

  150.         if (key.len > 65535) {
  151.             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
  152.                           "the value of the \"%V\" key "
  153.                           "is more than 65535 bytes: \"%V\"",
  154.                           &ctx->key.value, &key);
  155.             continue;
  156.         }

  157.         hash = ngx_crc32_short(key.data, key.len);

  158.         ngx_shmtx_lock(&ctx->shpool->mutex);

  159.         rc = ngx_http_limit_req_lookup(limit, hash, &key, &excess,
  160.                                        (n == lrcf->limits.nelts - 1));

  161.         ngx_shmtx_unlock(&ctx->shpool->mutex);

  162.         ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  163.                        "limit_req[%ui]: %i %ui.%03ui",
  164.                        n, rc, excess / 1000, excess % 1000);

  165.         if (rc != NGX_AGAIN) {
  166.             break;
  167.         }
  168.     }

  169.     if (rc == NGX_DECLINED) {
  170.         return NGX_DECLINED;
  171.     }

  172.     r->main->limit_req_set = 1;

  173.     if (rc == NGX_BUSY || rc == NGX_ERROR) {

  174.         if (rc == NGX_BUSY) {
  175.             ngx_log_error(lrcf->limit_log_level, r->connection->log, 0,
  176.                           "limiting requests, excess: %ui.%03ui by zone \"%V\"",
  177.                           excess / 1000, excess % 1000,
  178.                           &limit->shm_zone->shm.name);
  179.         }

  180.         while (n--) {
  181.             ctx = limits[n].shm_zone->data;

  182.             if (ctx->node == NULL) {
  183.                 continue;
  184.             }

  185.             ngx_shmtx_lock(&ctx->shpool->mutex);

  186.             ctx->node->count--;

  187.             ngx_shmtx_unlock(&ctx->shpool->mutex);

  188.             ctx->node = NULL;
  189.         }

  190.         return lrcf->status_code;
  191.     }

  192.     /* rc == NGX_AGAIN || rc == NGX_OK */

  193.     if (rc == NGX_AGAIN) {
  194.         excess = 0;
  195.     }

  196.     delay = ngx_http_limit_req_account(limits, n, &excess, &limit);

  197.     if (!delay) {
  198.         return NGX_DECLINED;
  199.     }

  200.     ngx_log_error(lrcf->delay_log_level, r->connection->log, 0,
  201.                   "delaying request, excess: %ui.%03ui, by zone \"%V\"",
  202.                   excess / 1000, excess % 1000, &limit->shm_zone->shm.name);

  203.     if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
  204.         return NGX_HTTP_INTERNAL_SERVER_ERROR;
  205.     }

  206.     r->read_event_handler = ngx_http_test_reading;
  207.     r->write_event_handler = ngx_http_limit_req_delay;
  208.     ngx_add_timer(r->connection->write, delay);

  209.     return NGX_AGAIN;
  210. }


  211. static void
  212. ngx_http_limit_req_delay(ngx_http_request_t *r)
  213. {
  214.     ngx_event_t  *wev;

  215.     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  216.                    "limit_req delay");

  217.     wev = r->connection->write;

  218.     if (!wev->timedout) {

  219.         if (ngx_handle_write_event(wev, 0) != NGX_OK) {
  220.             ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  221.         }

  222.         return;
  223.     }

  224.     wev->timedout = 0;

  225.     if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
  226.         ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  227.         return;
  228.     }

  229.     r->read_event_handler = ngx_http_block_reading;
  230.     r->write_event_handler = ngx_http_core_run_phases;

  231.     ngx_http_core_run_phases(r);
  232. }


  233. static void
  234. ngx_http_limit_req_rbtree_insert_value(ngx_rbtree_node_t *temp,
  235.     ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
  236. {
  237.     ngx_rbtree_node_t          **p;
  238.     ngx_http_limit_req_node_t   *lrn, *lrnt;

  239.     for ( ;; ) {

  240.         if (node->key < temp->key) {

  241.             p = &temp->left;

  242.         } else if (node->key > temp->key) {

  243.             p = &temp->right;

  244.         } else { /* node->key == temp->key */

  245.             lrn = (ngx_http_limit_req_node_t *) &node->color;
  246.             lrnt = (ngx_http_limit_req_node_t *) &temp->color;

  247.             p = (ngx_memn2cmp(lrn->data, lrnt->data, lrn->len, lrnt->len) < 0)
  248.                 ? &temp->left : &temp->right;
  249.         }

  250.         if (*p == sentinel) {
  251.             break;
  252.         }

  253.         temp = *p;
  254.     }

  255.     *p = node;
  256.     node->parent = temp;
  257.     node->left = sentinel;
  258.     node->right = sentinel;
  259.     ngx_rbt_red(node);
  260. }


  261. static ngx_int_t
  262. ngx_http_limit_req_lookup(ngx_http_limit_req_limit_t *limit, ngx_uint_t hash,
  263.     ngx_str_t *key, ngx_uint_t *ep, ngx_uint_t account)
  264. {
  265.     size_t                      size;
  266.     ngx_int_t                   rc, excess;
  267.     ngx_time_t                 *tp;
  268.     ngx_msec_t                  now;
  269.     ngx_msec_int_t              ms;
  270.     ngx_rbtree_node_t          *node, *sentinel;
  271.     ngx_http_limit_req_ctx_t   *ctx;
  272.     ngx_http_limit_req_node_t  *lr;

  273.     tp = ngx_timeofday();
  274.     now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);

  275.     ctx = limit->shm_zone->data;

  276.     node = ctx->sh->rbtree.root;
  277.     sentinel = ctx->sh->rbtree.sentinel;

  278.     while (node != sentinel) {

  279.         if (hash < node->key) {
  280.             node = node->left;
  281.             continue;
  282.         }

  283.         if (hash > node->key) {
  284.             node = node->right;
  285.             continue;
  286.         }

  287.         /* hash == node->key */

  288.         lr = (ngx_http_limit_req_node_t *) &node->color;

  289.         rc = ngx_memn2cmp(key->data, lr->data, key->len, (size_t) lr->len);

  290.         if (rc == 0) {
  291.             ngx_queue_remove(&lr->queue);
  292.             ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);

  293.             ms = (ngx_msec_int_t) (now - lr->last);

  294.             excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;

  295.             if (excess < 0) {
  296.                 excess = 0;
  297.             }

  298.             *ep = excess;

  299.             if ((ngx_uint_t) excess > limit->burst) {
  300.                 return NGX_BUSY;
  301.             }

  302.             if (account) {
  303.                 lr->excess = excess;
  304.                 lr->last = now;
  305.                 return NGX_OK;
  306.             }

  307.             lr->count++;

  308.             ctx->node = lr;

  309.             return NGX_AGAIN;
  310.         }

  311.         node = (rc < 0) ? node->left : node->right;
  312.     }

  313.     *ep = 0;

  314.     size = offsetof(ngx_rbtree_node_t, color)
  315.            + offsetof(ngx_http_limit_req_node_t, data)
  316.            + key->len;

  317.     ngx_http_limit_req_expire(ctx, 1);

  318.     node = ngx_slab_alloc_locked(ctx->shpool, size);

  319.     if (node == NULL) {
  320.         ngx_http_limit_req_expire(ctx, 0);

  321.         node = ngx_slab_alloc_locked(ctx->shpool, size);
  322.         if (node == NULL) {
  323.             ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
  324.                           "could not allocate node%s", ctx->shpool->log_ctx);
  325.             return NGX_ERROR;
  326.         }
  327.     }

  328.     node->key = hash;

  329.     lr = (ngx_http_limit_req_node_t *) &node->color;

  330.     lr->len = (u_short) key->len;
  331.     lr->excess = 0;

  332.     ngx_memcpy(lr->data, key->data, key->len);

  333.     ngx_rbtree_insert(&ctx->sh->rbtree, node);

  334.     ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);

  335.     if (account) {
  336.         lr->last = now;
  337.         lr->count = 0;
  338.         return NGX_OK;
  339.     }

  340.     lr->last = 0;
  341.     lr->count = 1;

  342.     ctx->node = lr;

  343.     return NGX_AGAIN;
  344. }


  345. static ngx_msec_t
  346. ngx_http_limit_req_account(ngx_http_limit_req_limit_t *limits, ngx_uint_t n,
  347.     ngx_uint_t *ep, ngx_http_limit_req_limit_t **limit)
  348. {
  349.     ngx_int_t                   excess;
  350.     ngx_time_t                 *tp;
  351.     ngx_msec_t                  now, delay, max_delay;
  352.     ngx_msec_int_t              ms;
  353.     ngx_http_limit_req_ctx_t   *ctx;
  354.     ngx_http_limit_req_node_t  *lr;

  355.     excess = *ep;

  356.     if (excess == 0 || (*limit)->nodelay) {
  357.         max_delay = 0;

  358.     } else {
  359.         ctx = (*limit)->shm_zone->data;
  360.         max_delay = excess * 1000 / ctx->rate;
  361.     }

  362.     while (n--) {
  363.         ctx = limits[n].shm_zone->data;
  364.         lr = ctx->node;

  365.         if (lr == NULL) {
  366.             continue;
  367.         }

  368.         ngx_shmtx_lock(&ctx->shpool->mutex);

  369.         tp = ngx_timeofday();

  370.         now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
  371.         ms = (ngx_msec_int_t) (now - lr->last);

  372.         excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;

  373.         if (excess < 0) {
  374.             excess = 0;
  375.         }

  376.         lr->last = now;
  377.         lr->excess = excess;
  378.         lr->count--;

  379.         ngx_shmtx_unlock(&ctx->shpool->mutex);

  380.         ctx->node = NULL;

  381.         if (limits[n].nodelay) {
  382.             continue;
  383.         }

  384.         delay = excess * 1000 / ctx->rate;

  385.         if (delay > max_delay) {
  386.             max_delay = delay;
  387.             *ep = excess;
  388.             *limit = &limits[n];
  389.         }
  390.     }

  391.     return max_delay;
  392. }


  393. static void
  394. ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx, ngx_uint_t n)
  395. {
  396.     ngx_int_t                   excess;
  397.     ngx_time_t                 *tp;
  398.     ngx_msec_t                  now;
  399.     ngx_queue_t                *q;
  400.     ngx_msec_int_t              ms;
  401.     ngx_rbtree_node_t          *node;
  402.     ngx_http_limit_req_node_t  *lr;

  403.     tp = ngx_timeofday();

  404.     now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);

  405.     /*
  406.      * n == 1 deletes one or two zero rate entries
  407.      * n == 0 deletes oldest entry by force
  408.      *        and one or two zero rate entries
  409.      */

  410.     while (n < 3) {

  411.         if (ngx_queue_empty(&ctx->sh->queue)) {
  412.             return;
  413.         }

  414.         q = ngx_queue_last(&ctx->sh->queue);

  415.         lr = ngx_queue_data(q, ngx_http_limit_req_node_t, queue);

  416.         if (lr->count) {

  417.             /*
  418.              * There is not much sense in looking further,
  419.              * because we bump nodes on the lookup stage.
  420.              */

  421.             return;
  422.         }

  423.         if (n++ != 0) {

  424.             ms = (ngx_msec_int_t) (now - lr->last);
  425.             ms = ngx_abs(ms);

  426.             if (ms < 60000) {
  427.                 return;
  428.             }

  429.             excess = lr->excess - ctx->rate * ms / 1000;

  430.             if (excess > 0) {
  431.                 return;
  432.             }
  433.         }

  434.         ngx_queue_remove(q);

  435.         node = (ngx_rbtree_node_t *)
  436.                    ((u_char *) lr - offsetof(ngx_rbtree_node_t, color));

  437.         ngx_rbtree_delete(&ctx->sh->rbtree, node);

  438.         ngx_slab_free_locked(ctx->shpool, node);
  439.     }
  440. }


  441. static ngx_int_t
  442. ngx_http_limit_req_init_zone(ngx_shm_zone_t *shm_zone, void *data)
  443. {
  444.     ngx_http_limit_req_ctx_t  *octx = data;

  445.     size_t                     len;
  446.     ngx_http_limit_req_ctx_t  *ctx;

  447.     ctx = shm_zone->data;

  448.     if (octx) {
  449.         if (ctx->key.value.len != octx->key.value.len
  450.             || ngx_strncmp(ctx->key.value.data, octx->key.value.data,
  451.                            ctx->key.value.len)
  452.                != 0)
  453.         {
  454.             ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
  455.                           "limit_req \"%V\" uses the \"%V\" key "
  456.                           "while previously it used the \"%V\" key",
  457.                           &shm_zone->shm.name, &ctx->key.value,
  458.                           &octx->key.value);
  459.             return NGX_ERROR;
  460.         }

  461.         ctx->sh = octx->sh;
  462.         ctx->shpool = octx->shpool;

  463.         return NGX_OK;
  464.     }

  465.     ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;

  466.     if (shm_zone->shm.exists) {
  467.         ctx->sh = ctx->shpool->data;

  468.         return NGX_OK;
  469.     }

  470.     ctx->sh = ngx_slab_alloc(ctx->shpool, sizeof(ngx_http_limit_req_shctx_t));
  471.     if (ctx->sh == NULL) {
  472.         return NGX_ERROR;
  473.     }

  474.     ctx->shpool->data = ctx->sh;

  475.     ngx_rbtree_init(&ctx->sh->rbtree, &ctx->sh->sentinel,
  476.                     ngx_http_limit_req_rbtree_insert_value);

  477.     ngx_queue_init(&ctx->sh->queue);

  478.     len = sizeof(" in limit_req zone \"\"") + shm_zone->shm.name.len;

  479.     ctx->shpool->log_ctx = ngx_slab_alloc(ctx->shpool, len);
  480.     if (ctx->shpool->log_ctx == NULL) {
  481.         return NGX_ERROR;
  482.     }

  483.     ngx_sprintf(ctx->shpool->log_ctx, " in limit_req zone \"%V\"%Z",
  484.                 &shm_zone->shm.name);

  485.     ctx->shpool->log_nomem = 0;

  486.     return NGX_OK;
  487. }


  488. static void *
  489. ngx_http_limit_req_create_conf(ngx_conf_t *cf)
  490. {
  491.     ngx_http_limit_req_conf_t  *conf;

  492.     conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_conf_t));
  493.     if (conf == NULL) {
  494.         return NULL;
  495.     }

  496.     /*
  497.      * set by ngx_pcalloc():
  498.      *
  499.      *     conf->limits.elts = NULL;
  500.      */

  501.     conf->limit_log_level = NGX_CONF_UNSET_UINT;
  502.     conf->status_code = NGX_CONF_UNSET_UINT;

  503.     return conf;
  504. }


  505. static char *
  506. ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent, void *child)
  507. {
  508.     ngx_http_limit_req_conf_t *prev = parent;
  509.     ngx_http_limit_req_conf_t *conf = child;

  510.     if (conf->limits.elts == NULL) {
  511.         conf->limits = prev->limits;
  512.     }

  513.     ngx_conf_merge_uint_value(conf->limit_log_level, prev->limit_log_level,
  514.                               NGX_LOG_ERR);

  515.     conf->delay_log_level = (conf->limit_log_level == NGX_LOG_INFO) ?
  516.                                 NGX_LOG_INFO : conf->limit_log_level + 1;

  517.     ngx_conf_merge_uint_value(conf->status_code, prev->status_code,
  518.                               NGX_HTTP_SERVICE_UNAVAILABLE);

  519.     return NGX_CONF_OK;
  520. }


  521. static char *
  522. ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  523. {
  524.     u_char                            *p;
  525.     size_t                             len;
  526.     ssize_t                            size;
  527.     ngx_str_t                         *value, name, s;
  528.     ngx_int_t                          rate, scale;
  529.     ngx_uint_t                         i;
  530.     ngx_shm_zone_t                    *shm_zone;
  531.     ngx_http_limit_req_ctx_t          *ctx;
  532.     ngx_http_compile_complex_value_t   ccv;

  533.     value = cf->args->elts;

  534.     ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_ctx_t));
  535.     if (ctx == NULL) {
  536.         return NGX_CONF_ERROR;
  537.     }

  538.     ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));

  539.     ccv.cf = cf;
  540.     ccv.value = &value[1];
  541.     ccv.complex_value = &ctx->key;

  542.     if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
  543.         return NGX_CONF_ERROR;
  544.     }

  545.     size = 0;
  546.     rate = 1;
  547.     scale = 1;
  548.     name.len = 0;

  549.     for (i = 2; i < cf->args->nelts; i++) {

  550.         if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {

  551.             name.data = value[i].data + 5;

  552.             p = (u_char *) ngx_strchr(name.data, ':');

  553.             if (p == NULL) {
  554.                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  555.                                    "invalid zone size \"%V\"", &value[i]);
  556.                 return NGX_CONF_ERROR;
  557.             }

  558.             name.len = p - name.data;

  559.             s.data = p + 1;
  560.             s.len = value[i].data + value[i].len - s.data;

  561.             size = ngx_parse_size(&s);

  562.             if (size == NGX_ERROR) {
  563.                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  564.                                    "invalid zone size \"%V\"", &value[i]);
  565.                 return NGX_CONF_ERROR;
  566.             }

  567.             if (size < (ssize_t) (8 * ngx_pagesize)) {
  568.                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  569.                                    "zone \"%V\" is too small", &value[i]);
  570.                 return NGX_CONF_ERROR;
  571.             }

  572.             continue;
  573.         }

  574.         if (ngx_strncmp(value[i].data, "rate=", 5) == 0) {

  575.             len = value[i].len;
  576.             p = value[i].data + len - 3;

  577.             if (ngx_strncmp(p, "r/s", 3) == 0) {
  578.                 scale = 1;
  579.                 len -= 3;

  580.             } else if (ngx_strncmp(p, "r/m", 3) == 0) {
  581.                 scale = 60;
  582.                 len -= 3;
  583.             }

  584.             rate = ngx_atoi(value[i].data + 5, len - 5);
  585.             if (rate <= 0) {
  586.                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  587.                                    "invalid rate \"%V\"", &value[i]);
  588.                 return NGX_CONF_ERROR;
  589.             }

  590.             continue;
  591.         }

  592.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  593.                            "invalid parameter \"%V\"", &value[i]);
  594.         return NGX_CONF_ERROR;
  595.     }

  596.     if (name.len == 0) {
  597.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  598.                            "\"%V\" must have \"zone\" parameter",
  599.                            &cmd->name);
  600.         return NGX_CONF_ERROR;
  601.     }

  602.     ctx->rate = rate * 1000 / scale;

  603.     shm_zone = ngx_shared_memory_add(cf, &name, size,
  604.                                      &ngx_http_limit_req_module);
  605.     if (shm_zone == NULL) {
  606.         return NGX_CONF_ERROR;
  607.     }

  608.     if (shm_zone->data) {
  609.         ctx = shm_zone->data;

  610.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  611.                            "%V \"%V\" is already bound to key \"%V\"",
  612.                            &cmd->name, &name, &ctx->key.value);
  613.         return NGX_CONF_ERROR;
  614.     }

  615.     shm_zone->init = ngx_http_limit_req_init_zone;
  616.     shm_zone->data = ctx;

  617.     return NGX_CONF_OK;
  618. }


  619. static char *
  620. ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  621. {
  622.     ngx_http_limit_req_conf_t  *lrcf = conf;

  623.     ngx_int_t                    burst;
  624.     ngx_str_t                   *value, s;
  625.     ngx_uint_t                   i, nodelay;
  626.     ngx_shm_zone_t              *shm_zone;
  627.     ngx_http_limit_req_limit_t  *limit, *limits;

  628.     value = cf->args->elts;

  629.     shm_zone = NULL;
  630.     burst = 0;
  631.     nodelay = 0;

  632.     for (i = 1; i < cf->args->nelts; i++) {

  633.         if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {

  634.             s.len = value[i].len - 5;
  635.             s.data = value[i].data + 5;

  636.             shm_zone = ngx_shared_memory_add(cf, &s, 0,
  637.                                              &ngx_http_limit_req_module);
  638.             if (shm_zone == NULL) {
  639.                 return NGX_CONF_ERROR;
  640.             }

  641.             continue;
  642.         }

  643.         if (ngx_strncmp(value[i].data, "burst=", 6) == 0) {

  644.             burst = ngx_atoi(value[i].data + 6, value[i].len - 6);
  645.             if (burst <= 0) {
  646.                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  647.                                    "invalid burst rate \"%V\"", &value[i]);
  648.                 return NGX_CONF_ERROR;
  649.             }

  650.             continue;
  651.         }

  652.         if (ngx_strcmp(value[i].data, "nodelay") == 0) {
  653.             nodelay = 1;
  654.             continue;
  655.         }

  656.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  657.                            "invalid parameter \"%V\"", &value[i]);
  658.         return NGX_CONF_ERROR;
  659.     }

  660.     if (shm_zone == NULL) {
  661.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  662.                            "\"%V\" must have \"zone\" parameter",
  663.                            &cmd->name);
  664.         return NGX_CONF_ERROR;
  665.     }

  666.     if (shm_zone->data == NULL) {
  667.         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
  668.                            "unknown limit_req_zone \"%V\"",
  669.                            &shm_zone->shm.name);
  670.         return NGX_CONF_ERROR;
  671.     }

  672.     limits = lrcf->limits.elts;

  673.     if (limits == NULL) {
  674.         if (ngx_array_init(&lrcf->limits, cf->pool, 1,
  675.                            sizeof(ngx_http_limit_req_limit_t))
  676.             != NGX_OK)
  677.         {
  678.             return NGX_CONF_ERROR;
  679.         }
  680.     }

  681.     for (i = 0; i < lrcf->limits.nelts; i++) {
  682.         if (shm_zone == limits[i].shm_zone) {
  683.             return "is duplicate";
  684.         }
  685.     }

  686.     limit = ngx_array_push(&lrcf->limits);
  687.     if (limit == NULL) {
  688.         return NGX_CONF_ERROR;
  689.     }

  690.     limit->shm_zone = shm_zone;
  691.     limit->burst = burst * 1000;
  692.     limit->nodelay = nodelay;

  693.     return NGX_CONF_OK;
  694. }


  695. static ngx_int_t
  696. ngx_http_limit_req_init(ngx_conf_t *cf)
  697. {
  698.     ngx_http_handler_pt        *h;
  699.     ngx_http_core_main_conf_t  *cmcf;

  700.     cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);

  701.     h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
  702.     if (h == NULL) {
  703.         return NGX_ERROR;
  704.     }

  705.     *h = ngx_http_limit_req_handler;

  706.     return NGX_OK;
  707. }