Mercurial > hg > nginx-mail
diff src/http/modules/ngx_http_limit_req_module.c @ 665:0b460e61bdcd default tip
Merge with nginx 1.0.0.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 25 Apr 2011 04:22:17 +0400 |
parents | c5122335e41d |
children |
line wrap: on
line diff
--- a/src/http/modules/ngx_http_limit_req_module.c +++ b/src/http/modules/ngx_http_limit_req_module.c @@ -42,13 +42,16 @@ typedef struct { ngx_shm_zone_t *shm_zone; /* integer value, 1 corresponds to 0.001 r/s */ ngx_uint_t burst; - ngx_uint_t nodelay;/* unsigned nodelay:1 */ + ngx_uint_t limit_log_level; + ngx_uint_t delay_log_level; + + ngx_uint_t nodelay; /* unsigned nodelay:1 */ } ngx_http_limit_req_conf_t; static void ngx_http_limit_req_delay(ngx_http_request_t *r); static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, - ngx_uint_t hash, u_char *data, size_t len, ngx_http_limit_req_node_t **lrp); + ngx_uint_t hash, u_char *data, size_t len, ngx_uint_t *ep); static void ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx, ngx_uint_t n); @@ -62,6 +65,15 @@ static char *ngx_http_limit_req(ngx_conf static ngx_int_t ngx_http_limit_req_init(ngx_conf_t *cf); +static ngx_conf_enum_t ngx_http_limit_req_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 } +}; + + static ngx_command_t ngx_http_limit_req_commands[] = { { ngx_string("limit_req_zone"), @@ -78,6 +90,13 @@ static ngx_command_t ngx_http_limit_req 0, NULL }, + { ngx_string("limit_req_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, + offsetof(ngx_http_limit_req_conf_t, limit_log_level), + &ngx_http_limit_req_log_levels }, + ngx_null_command }; @@ -167,96 +186,83 @@ ngx_http_limit_req_handler(ngx_http_requ ngx_http_limit_req_expire(ctx, 1); - rc = ngx_http_limit_req_lookup(lrcf, hash, vv->data, len, &lr); - - if (lr) { - ngx_queue_remove(&lr->queue); - - ngx_queue_insert_head(&ctx->sh->queue, &lr->queue); - - excess = lr->excess; - - } else { - excess = 0; - } + rc = ngx_http_limit_req_lookup(lrcf, hash, vv->data, len, &excess); ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, "limit_req: %i %ui.%03ui", rc, excess / 1000, excess % 1000); - if (rc == NGX_BUSY) { + if (rc == NGX_DECLINED) { + + n = offsetof(ngx_rbtree_node_t, color) + + offsetof(ngx_http_limit_req_node_t, data) + + len; + + node = ngx_slab_alloc_locked(ctx->shpool, n); + if (node == NULL) { + + ngx_http_limit_req_expire(ctx, 0); + + node = ngx_slab_alloc_locked(ctx->shpool, n); + if (node == NULL) { + ngx_shmtx_unlock(&ctx->shpool->mutex); + return NGX_HTTP_SERVICE_UNAVAILABLE; + } + } + + lr = (ngx_http_limit_req_node_t *) &node->color; + + node->key = hash; + lr->len = (u_char) len; + + tp = ngx_timeofday(); + lr->last = (ngx_msec_t) (tp->sec * 1000 + tp->msec); + + lr->excess = 0; + ngx_memcpy(lr->data, vv->data, len); + + ngx_rbtree_insert(&ctx->sh->rbtree, node); + + ngx_queue_insert_head(&ctx->sh->queue, &lr->queue); + ngx_shmtx_unlock(&ctx->shpool->mutex); - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + return NGX_DECLINED; + } + + ngx_shmtx_unlock(&ctx->shpool->mutex); + + if (rc == NGX_OK) { + return NGX_DECLINED; + } + + if (rc == NGX_BUSY) { + ngx_log_error(lrcf->limit_log_level, r->connection->log, 0, "limiting requests, excess: %ui.%03ui by zone \"%V\"", excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name); return NGX_HTTP_SERVICE_UNAVAILABLE; } - if (rc == NGX_AGAIN) { - ngx_shmtx_unlock(&ctx->shpool->mutex); - - if (lrcf->nodelay) { - return NGX_DECLINED; - } - - ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, - "delaying request, excess: %ui.%03ui, by zone \"%V\"", - excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name); + /* rc == NGX_AGAIN */ - if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) { - return NGX_HTTP_INTERNAL_SERVER_ERROR; - } - - r->read_event_handler = ngx_http_test_reading; - r->write_event_handler = ngx_http_limit_req_delay; - ngx_add_timer(r->connection->write, (ngx_msec_t) excess); - - return NGX_AGAIN; - } - - if (rc == NGX_OK) { - goto done; + if (lrcf->nodelay) { + return NGX_DECLINED; } - /* rc == NGX_DECLINED */ - - n = offsetof(ngx_rbtree_node_t, color) - + offsetof(ngx_http_limit_req_node_t, data) - + len; + ngx_log_error(lrcf->delay_log_level, r->connection->log, 0, + "delaying request, excess: %ui.%03ui, by zone \"%V\"", + excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name); - node = ngx_slab_alloc_locked(ctx->shpool, n); - if (node == NULL) { - - ngx_http_limit_req_expire(ctx, 0); - - node = ngx_slab_alloc_locked(ctx->shpool, n); - if (node == NULL) { - ngx_shmtx_unlock(&ctx->shpool->mutex); - return NGX_HTTP_SERVICE_UNAVAILABLE; - } + if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) { + return NGX_HTTP_INTERNAL_SERVER_ERROR; } - lr = (ngx_http_limit_req_node_t *) &node->color; - - node->key = hash; - lr->len = (u_char) len; - - tp = ngx_timeofday(); - lr->last = (ngx_msec_t) (tp->sec * 1000 + tp->msec); + r->read_event_handler = ngx_http_test_reading; + r->write_event_handler = ngx_http_limit_req_delay; + ngx_add_timer(r->connection->write, + (ngx_msec_t) excess * 1000 / ctx->rate); - lr->excess = 0; - ngx_memcpy(lr->data, vv->data, len); - - ngx_rbtree_insert(&ctx->sh->rbtree, node); - - ngx_queue_insert_head(&ctx->sh->queue, &lr->queue); - -done: - - ngx_shmtx_unlock(&ctx->shpool->mutex); - - return NGX_DECLINED; + return NGX_AGAIN; } @@ -336,7 +342,7 @@ ngx_http_limit_req_rbtree_insert_value(n static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, ngx_uint_t hash, - u_char *data, size_t len, ngx_http_limit_req_node_t **lrp) + u_char *data, size_t len, ngx_uint_t *ep) { ngx_int_t rc, excess; ngx_time_t *tp; @@ -371,6 +377,8 @@ ngx_http_limit_req_lookup(ngx_http_limit rc = ngx_memn2cmp(data, lr->data, len, (size_t) lr->len); if (rc == 0) { + ngx_queue_remove(&lr->queue); + ngx_queue_insert_head(&ctx->sh->queue, &lr->queue); tp = ngx_timeofday(); @@ -383,15 +391,15 @@ ngx_http_limit_req_lookup(ngx_http_limit excess = 0; } - lr->excess = excess; - lr->last = now; - - *lrp = lr; + *ep = excess; if ((ngx_uint_t) excess > lrcf->burst) { return NGX_BUSY; } + lr->excess = excess; + lr->last = now; + if (excess) { return NGX_AGAIN; } @@ -406,7 +414,7 @@ ngx_http_limit_req_lookup(ngx_http_limit break; } - *lrp = NULL; + *ep = 0; return NGX_DECLINED; } @@ -548,6 +556,8 @@ ngx_http_limit_req_create_conf(ngx_conf_ * conf->nodelay = 0; */ + conf->limit_log_level = NGX_CONF_UNSET_UINT; + return conf; } @@ -562,6 +572,12 @@ ngx_http_limit_req_merge_conf(ngx_conf_t *conf = *prev; } + ngx_conf_merge_uint_value(conf->limit_log_level, prev->limit_log_level, + NGX_LOG_ERR); + + conf->delay_log_level = (conf->limit_log_level == NGX_LOG_INFO) ? + NGX_LOG_INFO : conf->limit_log_level + 1; + return NGX_CONF_OK; }