staticngx_int_tngx_http_limit_req_handler(ngx_http_request_t*r){uint32_thash;ngx_str_tkey;ngx_int_trc;ngx_uint_tn,excess;ngx_msec_tdelay;ngx_http_limit_req_ctx_t*ctx;ngx_http_limit_req_conf_t*lrcf;ngx_http_limit_req_limit_t*limit,*limits;if(r->main->limit_req_status){returnNGX_DECLINED;}lrcf=ngx_http_get_module_loc_conf(r,ngx_http_limit_req_module);limits=lrcf->limits.elts;excess=0;rc=NGX_DECLINED;#if(NGX_SUPPRESS_WARN)limit=NULL;#endiffor(n=0;n<lrcf->limits.nelts;n++){limit=&limits[n];ctx=limit->shm_zone->data;if(ngx_http_complex_value(r,&ctx->key,&key)!=NGX_OK){ngx_http_limit_req_unlock(limits,n);returnNGX_HTTP_INTERNAL_SERVER_ERROR;}if(key.len==0){continue;}if(key.len>65535){ngx_log_error(NGX_LOG_ERR,r->connection->log,0,"the value of the \"%V\" key ""is more than 65535 bytes: \"%V\"",&ctx->key.value,&key);continue;}hash=ngx_crc32_short(key.data,key.len);ngx_shmtx_lock(&ctx->shpool->mutex);rc=ngx_http_limit_req_lookup(limit,hash,&key,&excess,(n==lrcf->limits.nelts-1));ngx_shmtx_unlock(&ctx->shpool->mutex);ngx_log_debug4(NGX_LOG_DEBUG_HTTP,r->connection->log,0,"limit_req[%ui]: %i %ui.%03ui",n,rc,excess/1000,excess%1000);if(rc!=NGX_AGAIN){break;}}if(rc==NGX_DECLINED){returnNGX_DECLINED;}if(rc==NGX_BUSY||rc==NGX_ERROR){if(rc==NGX_BUSY){ngx_log_error(lrcf->limit_log_level,r->connection->log,0,"limiting requests%s, excess: %ui.%03ui by zone \"%V\"",lrcf->dry_run?", dry run":"",excess/1000,excess%1000,&limit->shm_zone->shm.name);}ngx_http_limit_req_unlock(limits,n);if(lrcf->dry_run){r->main->limit_req_status=NGX_HTTP_LIMIT_REQ_REJECTED_DRY_RUN;returnNGX_DECLINED;}r->main->limit_req_status=NGX_HTTP_LIMIT_REQ_REJECTED;returnlrcf->status_code;}/* rc == NGX_AGAIN || rc == NGX_OK */if(rc==NGX_AGAIN){excess=0;}delay=ngx_http_limit_req_account(limits,n,&excess,&limit);if(!delay){r->main->limit_req_status=NGX_HTTP_LIMIT_REQ_PASSED;returnNGX_DECLINED;}ngx_log_error(lrcf->delay_log_level,r->connection->log,0,"delaying request%s, excess: %ui.%03ui, by zone \"%V\"",lrcf->dry_run?", dry run":"",excess/1000,excess%1000,&limit->shm_zone->shm.name);if(lrcf->dry_run){r->main->limit_req_status=NGX_HTTP_LIMIT_REQ_DELAYED_DRY_RUN;returnNGX_DECLINED;}r->main->limit_req_status=NGX_HTTP_LIMIT_REQ_DELAYED;if(r->connection->read->ready){ngx_post_event(r->connection->read,&ngx_posted_events);}else{if(ngx_handle_read_event(r->connection->read,0)!=NGX_OK){returnNGX_HTTP_INTERNAL_SERVER_ERROR;}}r->read_event_handler=ngx_http_test_reading;r->write_event_handler=ngx_http_limit_req_delay;r->connection->write->delayed=1;ngx_add_timer(r->connection->write,delay);returnNGX_AGAIN;}
for(n=0;n<lrcf->limits.nelts;n++){limit=&limits[n];ctx=limit->shm_zone->data;if(ngx_http_complex_value(r,&ctx->key,&key)!=NGX_OK){ngx_http_limit_req_unlock(limits,n);returnNGX_HTTP_INTERNAL_SERVER_ERROR;}if(key.len==0){continue;}if(key.len>65535){ngx_log_error(NGX_LOG_ERR,r->connection->log,0,"the value of the \"%V\" key ""is more than 65535 bytes: \"%V\"",&ctx->key.value,&key);continue;}hash=ngx_crc32_short(key.data,key.len);ngx_shmtx_lock(&ctx->shpool->mutex);rc=ngx_http_limit_req_lookup(limit,hash,&key,&excess,(n==lrcf->limits.nelts-1));ngx_shmtx_unlock(&ctx->shpool->mutex);ngx_log_debug4(NGX_LOG_DEBUG_HTTP,r->connection->log,0,"limit_req[%ui]: %i %ui.%03ui",n,rc,excess/1000,excess%1000);if(rc!=NGX_AGAIN){break;}}
循环遍历配置的所有限速规则
6 所有规则均未命中
if(rc==NGX_DECLINED){returnNGX_DECLINED;}
如果最终 rc 仍为 NGX_DECLINED,说明没有任何规则限制此请求,直接放行
7 限流拦截或异常
if(rc==NGX_BUSY||rc==NGX_ERROR){if(rc==NGX_BUSY){ngx_log_error(lrcf->limit_log_level,r->connection->log,0,"limiting requests%s, excess: %ui.%03ui by zone \"%V\"",lrcf->dry_run?", dry run":"",excess/1000,excess%1000,&limit->shm_zone->shm.name);}ngx_http_limit_req_unlock(limits,n);if(lrcf->dry_run){r->main->limit_req_status=NGX_HTTP_LIMIT_REQ_REJECTED_DRY_RUN;returnNGX_DECLINED;}r->main->limit_req_status=NGX_HTTP_LIMIT_REQ_REJECTED;returnlrcf->status_code;}
ComfyUI:重新定义AI创作流程的模块化图形界面引擎 【免费下载链接】ComfyUI The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. 项目地址: https://gitcode.com/GitHub_Trending/co/ComfyUI
在当今AI内容创…