comparison 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
comparison
equal deleted inserted replaced
572:06419a2298a9 665:0b460e61bdcd
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,
51 ngx_uint_t hash, u_char *data, size_t len, ngx_http_limit_req_node_t **lrp); 54 ngx_uint_t hash, u_char *data, size_t len, ngx_uint_t *ep);
52 static void ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx, 55 static void ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx,
53 ngx_uint_t n); 56 ngx_uint_t n);
54 57
55 static void *ngx_http_limit_req_create_conf(ngx_conf_t *cf); 58 static void *ngx_http_limit_req_create_conf(ngx_conf_t *cf);
56 static char *ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent, 59 static char *ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent,
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
165 184
166 ngx_shmtx_lock(&ctx->shpool->mutex); 185 ngx_shmtx_lock(&ctx->shpool->mutex);
167 186
168 ngx_http_limit_req_expire(ctx, 1); 187 ngx_http_limit_req_expire(ctx, 1);
169 188
170 rc = ngx_http_limit_req_lookup(lrcf, hash, vv->data, len, &lr); 189 rc = ngx_http_limit_req_lookup(lrcf, hash, vv->data, len, &excess);
171
172 if (lr) {
173 ngx_queue_remove(&lr->queue);
174
175 ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
176
177 excess = lr->excess;
178
179 } else {
180 excess = 0;
181 }
182 190
183 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 191 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
184 "limit_req: %i %ui.%03ui", rc, excess / 1000, excess % 1000); 192 "limit_req: %i %ui.%03ui", rc, excess / 1000, excess % 1000);
185 193
194 if (rc == NGX_DECLINED) {
195
196 n = offsetof(ngx_rbtree_node_t, color)
197 + offsetof(ngx_http_limit_req_node_t, data)
198 + len;
199
200 node = ngx_slab_alloc_locked(ctx->shpool, n);
201 if (node == NULL) {
202
203 ngx_http_limit_req_expire(ctx, 0);
204
205 node = ngx_slab_alloc_locked(ctx->shpool, n);
206 if (node == NULL) {
207 ngx_shmtx_unlock(&ctx->shpool->mutex);
208 return NGX_HTTP_SERVICE_UNAVAILABLE;
209 }
210 }
211
212 lr = (ngx_http_limit_req_node_t *) &node->color;
213
214 node->key = hash;
215 lr->len = (u_char) len;
216
217 tp = ngx_timeofday();
218 lr->last = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
219
220 lr->excess = 0;
221 ngx_memcpy(lr->data, vv->data, len);
222
223 ngx_rbtree_insert(&ctx->sh->rbtree, node);
224
225 ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
226
227 ngx_shmtx_unlock(&ctx->shpool->mutex);
228
229 return NGX_DECLINED;
230 }
231
232 ngx_shmtx_unlock(&ctx->shpool->mutex);
233
234 if (rc == NGX_OK) {
235 return NGX_DECLINED;
236 }
237
186 if (rc == NGX_BUSY) { 238 if (rc == NGX_BUSY) {
187 ngx_shmtx_unlock(&ctx->shpool->mutex); 239 ngx_log_error(lrcf->limit_log_level, r->connection->log, 0,
188
189 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
190 "limiting requests, excess: %ui.%03ui by zone \"%V\"", 240 "limiting requests, excess: %ui.%03ui by zone \"%V\"",
191 excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name); 241 excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name);
192 242
193 return NGX_HTTP_SERVICE_UNAVAILABLE; 243 return NGX_HTTP_SERVICE_UNAVAILABLE;
194 } 244 }
195 245
196 if (rc == NGX_AGAIN) { 246 /* rc == NGX_AGAIN */
197 ngx_shmtx_unlock(&ctx->shpool->mutex); 247
198 248 if (lrcf->nodelay) {
199 if (lrcf->nodelay) { 249 return NGX_DECLINED;
200 return NGX_DECLINED; 250 }
201 } 251
202 252 ngx_log_error(lrcf->delay_log_level, r->connection->log, 0,
203 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, 253 "delaying request, excess: %ui.%03ui, by zone \"%V\"",
204 "delaying request, excess: %ui.%03ui, by zone \"%V\"", 254 excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name);
205 excess / 1000, excess % 1000, &lrcf->shm_zone->shm.name); 255
206 256 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
207 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) { 257 return NGX_HTTP_INTERNAL_SERVER_ERROR;
208 return NGX_HTTP_INTERNAL_SERVER_ERROR; 258 }
209 } 259
210 260 r->read_event_handler = ngx_http_test_reading;
211 r->read_event_handler = ngx_http_test_reading; 261 r->write_event_handler = ngx_http_limit_req_delay;
212 r->write_event_handler = ngx_http_limit_req_delay; 262 ngx_add_timer(r->connection->write,
213 ngx_add_timer(r->connection->write, (ngx_msec_t) excess); 263 (ngx_msec_t) excess * 1000 / ctx->rate);
214 264
215 return NGX_AGAIN; 265 return NGX_AGAIN;
216 }
217
218 if (rc == NGX_OK) {
219 goto done;
220 }
221
222 /* rc == NGX_DECLINED */
223
224 n = offsetof(ngx_rbtree_node_t, color)
225 + offsetof(ngx_http_limit_req_node_t, data)
226 + len;
227
228 node = ngx_slab_alloc_locked(ctx->shpool, n);
229 if (node == NULL) {
230
231 ngx_http_limit_req_expire(ctx, 0);
232
233 node = ngx_slab_alloc_locked(ctx->shpool, n);
234 if (node == NULL) {
235 ngx_shmtx_unlock(&ctx->shpool->mutex);
236 return NGX_HTTP_SERVICE_UNAVAILABLE;
237 }
238 }
239
240 lr = (ngx_http_limit_req_node_t *) &node->color;
241
242 node->key = hash;
243 lr->len = (u_char) len;
244
245 tp = ngx_timeofday();
246 lr->last = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
247
248 lr->excess = 0;
249 ngx_memcpy(lr->data, vv->data, len);
250
251 ngx_rbtree_insert(&ctx->sh->rbtree, node);
252
253 ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
254
255 done:
256
257 ngx_shmtx_unlock(&ctx->shpool->mutex);
258
259 return NGX_DECLINED;
260 } 266 }
261 267
262 268
263 static void 269 static void
264 ngx_http_limit_req_delay(ngx_http_request_t *r) 270 ngx_http_limit_req_delay(ngx_http_request_t *r)
334 } 340 }
335 341
336 342
337 static ngx_int_t 343 static ngx_int_t
338 ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, ngx_uint_t hash, 344 ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, ngx_uint_t hash,
339 u_char *data, size_t len, ngx_http_limit_req_node_t **lrp) 345 u_char *data, size_t len, ngx_uint_t *ep)
340 { 346 {
341 ngx_int_t rc, excess; 347 ngx_int_t rc, excess;
342 ngx_time_t *tp; 348 ngx_time_t *tp;
343 ngx_msec_t now; 349 ngx_msec_t now;
344 ngx_msec_int_t ms; 350 ngx_msec_int_t ms;
369 lr = (ngx_http_limit_req_node_t *) &node->color; 375 lr = (ngx_http_limit_req_node_t *) &node->color;
370 376
371 rc = ngx_memn2cmp(data, lr->data, len, (size_t) lr->len); 377 rc = ngx_memn2cmp(data, lr->data, len, (size_t) lr->len);
372 378
373 if (rc == 0) { 379 if (rc == 0) {
380 ngx_queue_remove(&lr->queue);
381 ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
374 382
375 tp = ngx_timeofday(); 383 tp = ngx_timeofday();
376 384
377 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec); 385 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
378 ms = (ngx_msec_int_t) (now - lr->last); 386 ms = (ngx_msec_int_t) (now - lr->last);
381 389
382 if (excess < 0) { 390 if (excess < 0) {
383 excess = 0; 391 excess = 0;
384 } 392 }
385 393
386 lr->excess = excess; 394 *ep = excess;
387 lr->last = now;
388
389 *lrp = lr;
390 395
391 if ((ngx_uint_t) excess > lrcf->burst) { 396 if ((ngx_uint_t) excess > lrcf->burst) {
392 return NGX_BUSY; 397 return NGX_BUSY;
393 } 398 }
394 399
400 lr->excess = excess;
401 lr->last = now;
402
395 if (excess) { 403 if (excess) {
396 return NGX_AGAIN; 404 return NGX_AGAIN;
397 } 405 }
398 406
399 return NGX_OK; 407 return NGX_OK;
404 } while (node != sentinel && hash == node->key); 412 } while (node != sentinel && hash == node->key);
405 413
406 break; 414 break;
407 } 415 }
408 416
409 *lrp = NULL; 417 *ep = 0;
410 418
411 return NGX_DECLINED; 419 return NGX_DECLINED;
412 } 420 }
413 421
414 422
546 * conf->shm_zone = NULL; 554 * conf->shm_zone = NULL;
547 * conf->burst = 0; 555 * conf->burst = 0;
548 * conf->nodelay = 0; 556 * conf->nodelay = 0;
549 */ 557 */
550 558
559 conf->limit_log_level = NGX_CONF_UNSET_UINT;
560
551 return conf; 561 return conf;
552 } 562 }
553 563
554 564
555 static char * 565 static char *
559 ngx_http_limit_req_conf_t *conf = child; 569 ngx_http_limit_req_conf_t *conf = child;
560 570
561 if (conf->shm_zone == NULL) { 571 if (conf->shm_zone == NULL) {
562 *conf = *prev; 572 *conf = *prev;
563 } 573 }
574
575 ngx_conf_merge_uint_value(conf->limit_log_level, prev->limit_log_level,
576 NGX_LOG_ERR);
577
578 conf->delay_log_level = (conf->limit_log_level == NGX_LOG_INFO) ?
579 NGX_LOG_INFO : conf->limit_log_level + 1;
564 580
565 return NGX_CONF_OK; 581 return NGX_CONF_OK;
566 } 582 }
567 583
568 584