comparison src/http/modules/ngx_http_limit_req_module.c @ 3240:39f82eb3d0f2 stable-0.7

merge r2973, r2974, r3184, r3192, r3186, r3187: various limit_req and limit_conn fixes: *) fix client write event handling in ngx_http_limit_req_module *) make limit_req to conform to the leaky bucket algorithm *) limit_req_log_level *) limit_conn_log_level
author Igor Sysoev <igor@sysoev.ru>
date Mon, 26 Oct 2009 16:30:34 +0000
parents 2efa8d2fcde1
children
comparison
equal deleted inserted replaced
3239:8254055b6693 3240:39f82eb3d0f2
40 40
41 typedef struct { 41 typedef struct {
42 ngx_shm_zone_t *shm_zone; 42 ngx_shm_zone_t *shm_zone;
43 /* integer value, 1 corresponds to 0.001 r/s */ 43 /* integer value, 1 corresponds to 0.001 r/s */
44 ngx_uint_t burst; 44 ngx_uint_t burst;
45 ngx_uint_t nodelay;/* unsigned nodelay:1 */ 45 ngx_uint_t limit_log_level;
46 ngx_uint_t delay_log_level;
47
48 ngx_uint_t nodelay; /* unsigned nodelay:1 */
46 } ngx_http_limit_req_conf_t; 49 } ngx_http_limit_req_conf_t;
47 50
48 51
49 static void ngx_http_limit_req_delay(ngx_http_request_t *r); 52 static void ngx_http_limit_req_delay(ngx_http_request_t *r);
50 static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, 53 static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf,
60 static char *ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, 63 static char *ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd,
61 void *conf); 64 void *conf);
62 static ngx_int_t ngx_http_limit_req_init(ngx_conf_t *cf); 65 static ngx_int_t ngx_http_limit_req_init(ngx_conf_t *cf);
63 66
64 67
68 static ngx_conf_enum_t ngx_http_limit_req_log_levels[] = {
69 { ngx_string("info"), NGX_LOG_INFO },
70 { ngx_string("notice"), NGX_LOG_NOTICE },
71 { ngx_string("warn"), NGX_LOG_WARN },
72 { ngx_string("error"), NGX_LOG_ERR },
73 { ngx_null_string, 0 }
74 };
75
76
65 static ngx_command_t ngx_http_limit_req_commands[] = { 77 static ngx_command_t ngx_http_limit_req_commands[] = {
66 78
67 { ngx_string("limit_req_zone"), 79 { ngx_string("limit_req_zone"),
68 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3, 80 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3,
69 ngx_http_limit_req_zone, 81 ngx_http_limit_req_zone,
75 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123, 87 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
76 ngx_http_limit_req, 88 ngx_http_limit_req,
77 NGX_HTTP_LOC_CONF_OFFSET, 89 NGX_HTTP_LOC_CONF_OFFSET,
78 0, 90 0,
79 NULL }, 91 NULL },
92
93 { ngx_string("limit_req_log_level"),
94 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
95 ngx_conf_set_enum_slot,
96 NGX_HTTP_LOC_CONF_OFFSET,
97 offsetof(ngx_http_limit_req_conf_t, limit_log_level),
98 &ngx_http_limit_req_log_levels },
80 99
81 ngx_null_command 100 ngx_null_command
82 }; 101 };
83 102
84 103
179 } else { 198 } else {
180 excess = 0; 199 excess = 0;
181 } 200 }
182 201
183 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 202 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
184 "limit_req: %i %ui.%03ui", rc, excess / 1000, excess % 1000); 203 "limit_req: %i %ui.%03ui", rc, excess / 1000, excess % 1000);
185 204
186 if (rc == NGX_BUSY) { 205 if (rc == NGX_BUSY) {
187 ngx_shmtx_unlock(&ctx->shpool->mutex); 206 ngx_shmtx_unlock(&ctx->shpool->mutex);
188 207
189 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, 208 ngx_log_error(lrcf->limit_log_level, r->connection->log, 0,
190 "limiting requests, excess: %ui.%03ui by zone \"%V\"", 209 "limiting requests, excess: %ui.%03ui by zone \"%V\"",
191 excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name); 210 excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name);
192 211
193 return NGX_HTTP_SERVICE_UNAVAILABLE; 212 return NGX_HTTP_SERVICE_UNAVAILABLE;
194 } 213 }
198 217
199 if (lrcf->nodelay) { 218 if (lrcf->nodelay) {
200 return NGX_DECLINED; 219 return NGX_DECLINED;
201 } 220 }
202 221
203 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, 222 ngx_log_error(lrcf->delay_log_level, r->connection->log, 0,
204 "delaying request, excess: %ui.%03ui, by zone \"%V\"", 223 "delaying request, excess: %ui.%03ui, by zone \"%V\"",
205 excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name); 224 excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name);
206 225
207 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) { 226 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
208 return NGX_HTTP_INTERNAL_SERVER_ERROR; 227 return NGX_HTTP_INTERNAL_SERVER_ERROR;
261 280
262 281
263 static void 282 static void
264 ngx_http_limit_req_delay(ngx_http_request_t *r) 283 ngx_http_limit_req_delay(ngx_http_request_t *r)
265 { 284 {
285 ngx_event_t *wev;
286
266 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 287 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
267 "limit_req delay"); 288 "limit_req delay");
289
290 wev = r->connection->write;
291
292 if (!wev->timedout) {
293
294 if (ngx_handle_write_event(wev, 0) != NGX_OK) {
295 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
296 }
297
298 return;
299 }
300
301 wev->timedout = 0;
268 302
269 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) { 303 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
270 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 304 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
271 return; 305 return;
272 } 306 }
366 400
367 if (excess < 0) { 401 if (excess < 0) {
368 excess = 0; 402 excess = 0;
369 } 403 }
370 404
405 if ((ngx_uint_t) excess > lrcf->burst) {
406 *lrp = lr;
407 return NGX_BUSY;
408 }
409
371 lr->excess = excess; 410 lr->excess = excess;
372 lr->last = now; 411 lr->last = now;
373 412
374 *lrp = lr; 413 *lrp = lr;
375
376 if ((ngx_uint_t) excess > lrcf->burst) {
377 return NGX_BUSY;
378 }
379 414
380 if (excess) { 415 if (excess) {
381 return NGX_AGAIN; 416 return NGX_AGAIN;
382 } 417 }
383 418
531 * conf->shm_zone = NULL; 566 * conf->shm_zone = NULL;
532 * conf->burst = 0; 567 * conf->burst = 0;
533 * conf->nodelay = 0; 568 * conf->nodelay = 0;
534 */ 569 */
535 570
571 conf->limit_log_level = NGX_CONF_UNSET_UINT;
572
536 return conf; 573 return conf;
537 } 574 }
538 575
539 576
540 static char * 577 static char *
544 ngx_http_limit_req_conf_t *conf = child; 581 ngx_http_limit_req_conf_t *conf = child;
545 582
546 if (conf->shm_zone == NULL) { 583 if (conf->shm_zone == NULL) {
547 *conf = *prev; 584 *conf = *prev;
548 } 585 }
586
587 ngx_conf_merge_uint_value(conf->limit_log_level, prev->limit_log_level,
588 NGX_LOG_ERR);
589
590 conf->delay_log_level = (conf->limit_log_level == NGX_LOG_INFO) ?
591 NGX_LOG_INFO : conf->limit_log_level + 1;
549 592
550 return NGX_CONF_OK; 593 return NGX_CONF_OK;
551 } 594 }
552 595
553 596