416
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
660
|
4 * Copyright (C) Nginx, Inc.
|
416
|
5 */
|
|
6
|
|
7
|
|
8 #include <ngx_config.h>
|
|
9 #include <ngx_core.h>
|
|
10 #include <ngx_http.h>
|
|
11
|
|
12
|
|
13 typedef struct {
|
480
|
14 u_char color;
|
|
15 u_char dummy;
|
|
16 u_short len;
|
|
17 ngx_queue_t queue;
|
|
18 ngx_msec_t last;
|
|
19 /* integer value, 1 corresponds to 0.001 r/s */
|
|
20 ngx_uint_t excess;
|
660
|
21 ngx_uint_t count;
|
480
|
22 u_char data[1];
|
416
|
23 } ngx_http_limit_req_node_t;
|
|
24
|
|
25
|
|
26 typedef struct {
|
480
|
27 ngx_rbtree_t rbtree;
|
|
28 ngx_rbtree_node_t sentinel;
|
|
29 ngx_queue_t queue;
|
|
30 } ngx_http_limit_req_shctx_t;
|
|
31
|
|
32
|
|
33 typedef struct {
|
|
34 ngx_http_limit_req_shctx_t *sh;
|
|
35 ngx_slab_pool_t *shpool;
|
|
36 /* integer value, 1 corresponds to 0.001 r/s */
|
|
37 ngx_uint_t rate;
|
|
38 ngx_int_t index;
|
|
39 ngx_str_t var;
|
660
|
40 ngx_http_limit_req_node_t *node;
|
416
|
41 } ngx_http_limit_req_ctx_t;
|
|
42
|
|
43
|
|
44 typedef struct {
|
480
|
45 ngx_shm_zone_t *shm_zone;
|
|
46 /* integer value, 1 corresponds to 0.001 r/s */
|
|
47 ngx_uint_t burst;
|
660
|
48 ngx_uint_t nodelay; /* unsigned nodelay:1 */
|
|
49 } ngx_http_limit_req_limit_t;
|
|
50
|
|
51
|
|
52 typedef struct {
|
|
53 ngx_array_t limits;
|
532
|
54 ngx_uint_t limit_log_level;
|
|
55 ngx_uint_t delay_log_level;
|
416
|
56 } ngx_http_limit_req_conf_t;
|
|
57
|
|
58
|
|
59 static void ngx_http_limit_req_delay(ngx_http_request_t *r);
|
660
|
60 static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_limit_t *limit,
|
|
61 ngx_uint_t hash, u_char *data, size_t len, ngx_uint_t *ep,
|
|
62 ngx_uint_t account);
|
|
63 static ngx_msec_t ngx_http_limit_req_account(ngx_http_limit_req_limit_t *limits,
|
|
64 ngx_uint_t n, ngx_uint_t *ep, ngx_http_limit_req_limit_t **limit);
|
416
|
65 static void ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx,
|
|
66 ngx_uint_t n);
|
|
67
|
|
68 static void *ngx_http_limit_req_create_conf(ngx_conf_t *cf);
|
|
69 static char *ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent,
|
|
70 void *child);
|
|
71 static char *ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
72 void *conf);
|
|
73 static char *ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd,
|
|
74 void *conf);
|
|
75 static ngx_int_t ngx_http_limit_req_init(ngx_conf_t *cf);
|
|
76
|
|
77
|
532
|
78 static ngx_conf_enum_t ngx_http_limit_req_log_levels[] = {
|
|
79 { ngx_string("info"), NGX_LOG_INFO },
|
|
80 { ngx_string("notice"), NGX_LOG_NOTICE },
|
|
81 { ngx_string("warn"), NGX_LOG_WARN },
|
|
82 { ngx_string("error"), NGX_LOG_ERR },
|
|
83 { ngx_null_string, 0 }
|
|
84 };
|
|
85
|
|
86
|
416
|
87 static ngx_command_t ngx_http_limit_req_commands[] = {
|
|
88
|
|
89 { ngx_string("limit_req_zone"),
|
|
90 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3,
|
|
91 ngx_http_limit_req_zone,
|
|
92 0,
|
|
93 0,
|
|
94 NULL },
|
|
95
|
|
96 { ngx_string("limit_req"),
|
|
97 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123,
|
|
98 ngx_http_limit_req,
|
|
99 NGX_HTTP_LOC_CONF_OFFSET,
|
|
100 0,
|
|
101 NULL },
|
|
102
|
532
|
103 { ngx_string("limit_req_log_level"),
|
|
104 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
|
|
105 ngx_conf_set_enum_slot,
|
|
106 NGX_HTTP_LOC_CONF_OFFSET,
|
|
107 offsetof(ngx_http_limit_req_conf_t, limit_log_level),
|
|
108 &ngx_http_limit_req_log_levels },
|
|
109
|
416
|
110 ngx_null_command
|
|
111 };
|
|
112
|
|
113
|
|
114 static ngx_http_module_t ngx_http_limit_req_module_ctx = {
|
|
115 NULL, /* preconfiguration */
|
|
116 ngx_http_limit_req_init, /* postconfiguration */
|
|
117
|
|
118 NULL, /* create main configuration */
|
|
119 NULL, /* init main configuration */
|
|
120
|
|
121 NULL, /* create server configuration */
|
|
122 NULL, /* merge server configuration */
|
|
123
|
664
|
124 ngx_http_limit_req_create_conf, /* create location configuration */
|
|
125 ngx_http_limit_req_merge_conf /* merge location configuration */
|
416
|
126 };
|
|
127
|
|
128
|
|
129 ngx_module_t ngx_http_limit_req_module = {
|
|
130 NGX_MODULE_V1,
|
|
131 &ngx_http_limit_req_module_ctx, /* module context */
|
|
132 ngx_http_limit_req_commands, /* module directives */
|
|
133 NGX_HTTP_MODULE, /* module type */
|
|
134 NULL, /* init master */
|
|
135 NULL, /* init module */
|
|
136 NULL, /* init process */
|
|
137 NULL, /* init thread */
|
|
138 NULL, /* exit thread */
|
|
139 NULL, /* exit process */
|
|
140 NULL, /* exit master */
|
|
141 NGX_MODULE_V1_PADDING
|
|
142 };
|
|
143
|
|
144
|
|
145 static ngx_int_t
|
|
146 ngx_http_limit_req_handler(ngx_http_request_t *r)
|
|
147 {
|
660
|
148 size_t len;
|
|
149 uint32_t hash;
|
|
150 ngx_int_t rc;
|
|
151 ngx_uint_t n, excess;
|
|
152 ngx_msec_t delay;
|
|
153 ngx_http_variable_value_t *vv;
|
|
154 ngx_http_limit_req_ctx_t *ctx;
|
|
155 ngx_http_limit_req_conf_t *lrcf;
|
|
156 ngx_http_limit_req_limit_t *limit, *limits;
|
416
|
157
|
|
158 if (r->main->limit_req_set) {
|
|
159 return NGX_DECLINED;
|
|
160 }
|
|
161
|
426
|
162 lrcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_req_module);
|
660
|
163 limits = lrcf->limits.elts;
|
416
|
164
|
660
|
165 excess = 0;
|
|
166
|
|
167 rc = NGX_DECLINED;
|
|
168
|
|
169 #if (NGX_SUPPRESS_WARN)
|
|
170 limit = NULL;
|
|
171 #endif
|
|
172
|
|
173 for (n = 0; n < lrcf->limits.nelts; n++) {
|
|
174
|
|
175 limit = &limits[n];
|
|
176
|
|
177 ctx = limit->shm_zone->data;
|
|
178
|
|
179 vv = ngx_http_get_indexed_variable(r, ctx->index);
|
|
180
|
|
181 if (vv == NULL || vv->not_found) {
|
|
182 continue;
|
|
183 }
|
|
184
|
|
185 len = vv->len;
|
|
186
|
|
187 if (len == 0) {
|
|
188 continue;
|
|
189 }
|
|
190
|
|
191 if (len > 65535) {
|
|
192 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
|
|
193 "the value of the \"%V\" variable "
|
|
194 "is more than 65535 bytes: \"%v\"",
|
|
195 &ctx->var, vv);
|
|
196 continue;
|
|
197 }
|
|
198
|
|
199 hash = ngx_crc32_short(vv->data, len);
|
|
200
|
|
201 ngx_shmtx_lock(&ctx->shpool->mutex);
|
|
202
|
|
203 rc = ngx_http_limit_req_lookup(limit, hash, vv->data, len, &excess,
|
|
204 (n == lrcf->limits.nelts - 1));
|
|
205
|
|
206 ngx_shmtx_unlock(&ctx->shpool->mutex);
|
|
207
|
|
208 ngx_log_debug4(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
|
209 "limit_req[%ui]: %i %ui.%03ui",
|
|
210 n, rc, excess / 1000, excess % 1000);
|
|
211
|
|
212 if (rc != NGX_AGAIN) {
|
|
213 break;
|
|
214 }
|
416
|
215 }
|
|
216
|
660
|
217 if (rc == NGX_DECLINED) {
|
416
|
218 return NGX_DECLINED;
|
|
219 }
|
|
220
|
|
221 r->main->limit_req_set = 1;
|
|
222
|
660
|
223 if (rc == NGX_BUSY || rc == NGX_ERROR) {
|
602
|
224
|
660
|
225 if (rc == NGX_BUSY) {
|
|
226 ngx_log_error(lrcf->limit_log_level, r->connection->log, 0,
|
|
227 "limiting requests, excess: %ui.%03ui by zone \"%V\"",
|
|
228 excess / 1000, excess % 1000,
|
|
229 &limit->shm_zone->shm.name);
|
602
|
230 }
|
|
231
|
660
|
232 while (n--) {
|
|
233 ctx = limits[n].shm_zone->data;
|
602
|
234
|
660
|
235 if (ctx->node == NULL) {
|
|
236 continue;
|
|
237 }
|
602
|
238
|
660
|
239 ngx_shmtx_lock(&ctx->shpool->mutex);
|
416
|
240
|
660
|
241 ctx->node->count--;
|
602
|
242
|
660
|
243 ngx_shmtx_unlock(&ctx->shpool->mutex);
|
602
|
244
|
660
|
245 ctx->node = NULL;
|
|
246 }
|
416
|
247
|
|
248 return NGX_HTTP_SERVICE_UNAVAILABLE;
|
|
249 }
|
|
250
|
660
|
251 /* rc == NGX_AGAIN || rc == NGX_OK */
|
418
|
252
|
660
|
253 if (rc == NGX_AGAIN) {
|
|
254 excess = 0;
|
|
255 }
|
|
256
|
|
257 delay = ngx_http_limit_req_account(limits, n, &excess, &limit);
|
|
258
|
|
259 if (!delay) {
|
602
|
260 return NGX_DECLINED;
|
416
|
261 }
|
|
262
|
602
|
263 ngx_log_error(lrcf->delay_log_level, r->connection->log, 0,
|
|
264 "delaying request, excess: %ui.%03ui, by zone \"%V\"",
|
660
|
265 excess / 1000, excess % 1000, &limit->shm_zone->shm.name);
|
416
|
266
|
602
|
267 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
|
|
268 return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
416
|
269 }
|
|
270
|
602
|
271 r->read_event_handler = ngx_http_test_reading;
|
|
272 r->write_event_handler = ngx_http_limit_req_delay;
|
660
|
273 ngx_add_timer(r->connection->write, delay);
|
416
|
274
|
602
|
275 return NGX_AGAIN;
|
416
|
276 }
|
|
277
|
|
278
|
|
279 static void
|
|
280 ngx_http_limit_req_delay(ngx_http_request_t *r)
|
|
281 {
|
506
|
282 ngx_event_t *wev;
|
|
283
|
416
|
284 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
506
|
285 "limit_req delay");
|
|
286
|
|
287 wev = r->connection->write;
|
|
288
|
|
289 if (!wev->timedout) {
|
|
290
|
|
291 if (ngx_handle_write_event(wev, 0) != NGX_OK) {
|
|
292 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
293 }
|
|
294
|
|
295 return;
|
|
296 }
|
|
297
|
|
298 wev->timedout = 0;
|
416
|
299
|
|
300 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) {
|
|
301 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
|
|
302 return;
|
|
303 }
|
|
304
|
|
305 r->read_event_handler = ngx_http_block_reading;
|
|
306 r->write_event_handler = ngx_http_core_run_phases;
|
|
307
|
|
308 ngx_http_core_run_phases(r);
|
|
309 }
|
|
310
|
|
311
|
|
312 static void
|
|
313 ngx_http_limit_req_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
314 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
315 {
|
|
316 ngx_rbtree_node_t **p;
|
426
|
317 ngx_http_limit_req_node_t *lrn, *lrnt;
|
416
|
318
|
|
319 for ( ;; ) {
|
|
320
|
|
321 if (node->key < temp->key) {
|
|
322
|
|
323 p = &temp->left;
|
|
324
|
|
325 } else if (node->key > temp->key) {
|
|
326
|
|
327 p = &temp->right;
|
|
328
|
|
329 } else { /* node->key == temp->key */
|
|
330
|
426
|
331 lrn = (ngx_http_limit_req_node_t *) &node->color;
|
|
332 lrnt = (ngx_http_limit_req_node_t *) &temp->color;
|
416
|
333
|
426
|
334 p = (ngx_memn2cmp(lrn->data, lrnt->data, lrn->len, lrnt->len) < 0)
|
416
|
335 ? &temp->left : &temp->right;
|
|
336 }
|
|
337
|
|
338 if (*p == sentinel) {
|
|
339 break;
|
|
340 }
|
|
341
|
|
342 temp = *p;
|
|
343 }
|
|
344
|
|
345 *p = node;
|
|
346 node->parent = temp;
|
|
347 node->left = sentinel;
|
|
348 node->right = sentinel;
|
|
349 ngx_rbt_red(node);
|
|
350 }
|
|
351
|
|
352
|
|
353 static ngx_int_t
|
660
|
354 ngx_http_limit_req_lookup(ngx_http_limit_req_limit_t *limit, ngx_uint_t hash,
|
|
355 u_char *data, size_t len, ngx_uint_t *ep, ngx_uint_t account)
|
416
|
356 {
|
660
|
357 size_t size;
|
420
|
358 ngx_int_t rc, excess;
|
416
|
359 ngx_time_t *tp;
|
|
360 ngx_msec_t now;
|
|
361 ngx_msec_int_t ms;
|
|
362 ngx_rbtree_node_t *node, *sentinel;
|
|
363 ngx_http_limit_req_ctx_t *ctx;
|
426
|
364 ngx_http_limit_req_node_t *lr;
|
416
|
365
|
660
|
366 tp = ngx_timeofday();
|
|
367 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
|
|
368
|
|
369 ctx = limit->shm_zone->data;
|
416
|
370
|
480
|
371 node = ctx->sh->rbtree.root;
|
|
372 sentinel = ctx->sh->rbtree.sentinel;
|
416
|
373
|
|
374 while (node != sentinel) {
|
|
375
|
|
376 if (hash < node->key) {
|
|
377 node = node->left;
|
|
378 continue;
|
|
379 }
|
|
380
|
|
381 if (hash > node->key) {
|
|
382 node = node->right;
|
|
383 continue;
|
|
384 }
|
|
385
|
|
386 /* hash == node->key */
|
|
387
|
664
|
388 lr = (ngx_http_limit_req_node_t *) &node->color;
|
416
|
389
|
664
|
390 rc = ngx_memn2cmp(data, lr->data, len, (size_t) lr->len);
|
534
|
391
|
664
|
392 if (rc == 0) {
|
|
393 ngx_queue_remove(&lr->queue);
|
|
394 ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
|
532
|
395
|
664
|
396 ms = (ngx_msec_int_t) (now - lr->last);
|
416
|
397
|
664
|
398 excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
|
660
|
399
|
664
|
400 if (excess < 0) {
|
|
401 excess = 0;
|
416
|
402 }
|
|
403
|
664
|
404 *ep = excess;
|
|
405
|
|
406 if ((ngx_uint_t) excess > limit->burst) {
|
|
407 return NGX_BUSY;
|
|
408 }
|
416
|
409
|
664
|
410 if (account) {
|
|
411 lr->excess = excess;
|
|
412 lr->last = now;
|
|
413 return NGX_OK;
|
|
414 }
|
416
|
415
|
664
|
416 lr->count++;
|
|
417
|
|
418 ctx->node = lr;
|
|
419
|
|
420 return NGX_AGAIN;
|
|
421 }
|
|
422
|
|
423 node = (rc < 0) ? node->left : node->right;
|
416
|
424 }
|
|
425
|
602
|
426 *ep = 0;
|
416
|
427
|
660
|
428 size = offsetof(ngx_rbtree_node_t, color)
|
|
429 + offsetof(ngx_http_limit_req_node_t, data)
|
|
430 + len;
|
|
431
|
|
432 ngx_http_limit_req_expire(ctx, 1);
|
|
433
|
|
434 node = ngx_slab_alloc_locked(ctx->shpool, size);
|
|
435
|
|
436 if (node == NULL) {
|
|
437 ngx_http_limit_req_expire(ctx, 0);
|
|
438
|
|
439 node = ngx_slab_alloc_locked(ctx->shpool, size);
|
|
440 if (node == NULL) {
|
|
441 return NGX_ERROR;
|
|
442 }
|
|
443 }
|
|
444
|
|
445 node->key = hash;
|
|
446
|
|
447 lr = (ngx_http_limit_req_node_t *) &node->color;
|
|
448
|
|
449 lr->len = (u_char) len;
|
|
450 lr->excess = 0;
|
|
451
|
|
452 ngx_memcpy(lr->data, data, len);
|
|
453
|
686
|
454 ngx_rbtree_insert(&ctx->sh->rbtree, node);
|
|
455
|
|
456 ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
|
|
457
|
660
|
458 if (account) {
|
|
459 lr->last = now;
|
|
460 lr->count = 0;
|
|
461 return NGX_OK;
|
|
462 }
|
|
463
|
|
464 lr->last = 0;
|
|
465 lr->count = 1;
|
|
466
|
|
467 ctx->node = lr;
|
|
468
|
|
469 return NGX_AGAIN;
|
|
470 }
|
|
471
|
|
472
|
|
473 static ngx_msec_t
|
|
474 ngx_http_limit_req_account(ngx_http_limit_req_limit_t *limits, ngx_uint_t n,
|
|
475 ngx_uint_t *ep, ngx_http_limit_req_limit_t **limit)
|
|
476 {
|
|
477 ngx_int_t excess;
|
|
478 ngx_time_t *tp;
|
|
479 ngx_msec_t now, delay, max_delay;
|
|
480 ngx_msec_int_t ms;
|
|
481 ngx_http_limit_req_ctx_t *ctx;
|
|
482 ngx_http_limit_req_node_t *lr;
|
|
483
|
|
484 excess = *ep;
|
|
485
|
|
486 if (excess == 0 || (*limit)->nodelay) {
|
|
487 max_delay = 0;
|
|
488
|
|
489 } else {
|
|
490 ctx = (*limit)->shm_zone->data;
|
|
491 max_delay = excess * 1000 / ctx->rate;
|
|
492 }
|
|
493
|
|
494 while (n--) {
|
|
495 ctx = limits[n].shm_zone->data;
|
|
496 lr = ctx->node;
|
|
497
|
|
498 if (lr == NULL) {
|
|
499 continue;
|
|
500 }
|
|
501
|
|
502 ngx_shmtx_lock(&ctx->shpool->mutex);
|
|
503
|
|
504 tp = ngx_timeofday();
|
|
505
|
|
506 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
|
|
507 ms = (ngx_msec_int_t) (now - lr->last);
|
|
508
|
|
509 excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
|
|
510
|
|
511 if (excess < 0) {
|
|
512 excess = 0;
|
|
513 }
|
|
514
|
|
515 lr->last = now;
|
|
516 lr->excess = excess;
|
|
517 lr->count--;
|
|
518
|
|
519 ngx_shmtx_unlock(&ctx->shpool->mutex);
|
|
520
|
|
521 ctx->node = NULL;
|
|
522
|
|
523 if (limits[n].nodelay) {
|
|
524 continue;
|
|
525 }
|
|
526
|
|
527 delay = excess * 1000 / ctx->rate;
|
|
528
|
|
529 if (delay > max_delay) {
|
|
530 max_delay = delay;
|
|
531 *ep = excess;
|
|
532 *limit = &limits[n];
|
|
533 }
|
|
534 }
|
|
535
|
|
536 return max_delay;
|
416
|
537 }
|
|
538
|
|
539
|
|
540 static void
|
|
541 ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx, ngx_uint_t n)
|
|
542 {
|
420
|
543 ngx_int_t excess;
|
416
|
544 ngx_time_t *tp;
|
|
545 ngx_msec_t now;
|
|
546 ngx_queue_t *q;
|
|
547 ngx_msec_int_t ms;
|
|
548 ngx_rbtree_node_t *node;
|
426
|
549 ngx_http_limit_req_node_t *lr;
|
416
|
550
|
|
551 tp = ngx_timeofday();
|
|
552
|
|
553 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
|
|
554
|
|
555 /*
|
|
556 * n == 1 deletes one or two zero rate entries
|
|
557 * n == 0 deletes oldest entry by force
|
|
558 * and one or two zero rate entries
|
|
559 */
|
|
560
|
|
561 while (n < 3) {
|
|
562
|
480
|
563 if (ngx_queue_empty(&ctx->sh->queue)) {
|
416
|
564 return;
|
|
565 }
|
|
566
|
480
|
567 q = ngx_queue_last(&ctx->sh->queue);
|
416
|
568
|
426
|
569 lr = ngx_queue_data(q, ngx_http_limit_req_node_t, queue);
|
416
|
570
|
660
|
571 if (lr->count) {
|
|
572
|
|
573 /*
|
|
574 * There is not much sense in looking further,
|
|
575 * because we bump nodes on the lookup stage.
|
|
576 */
|
|
577
|
|
578 return;
|
|
579 }
|
|
580
|
416
|
581 if (n++ != 0) {
|
|
582
|
426
|
583 ms = (ngx_msec_int_t) (now - lr->last);
|
416
|
584 ms = ngx_abs(ms);
|
|
585
|
|
586 if (ms < 60000) {
|
|
587 return;
|
|
588 }
|
|
589
|
426
|
590 excess = lr->excess - ctx->rate * ms / 1000;
|
416
|
591
|
420
|
592 if (excess > 0) {
|
416
|
593 return;
|
|
594 }
|
|
595 }
|
|
596
|
|
597 ngx_queue_remove(q);
|
|
598
|
|
599 node = (ngx_rbtree_node_t *)
|
426
|
600 ((u_char *) lr - offsetof(ngx_rbtree_node_t, color));
|
416
|
601
|
480
|
602 ngx_rbtree_delete(&ctx->sh->rbtree, node);
|
416
|
603
|
|
604 ngx_slab_free_locked(ctx->shpool, node);
|
|
605 }
|
|
606 }
|
|
607
|
|
608
|
|
609 static ngx_int_t
|
|
610 ngx_http_limit_req_init_zone(ngx_shm_zone_t *shm_zone, void *data)
|
|
611 {
|
|
612 ngx_http_limit_req_ctx_t *octx = data;
|
|
613
|
468
|
614 size_t len;
|
416
|
615 ngx_http_limit_req_ctx_t *ctx;
|
|
616
|
|
617 ctx = shm_zone->data;
|
|
618
|
|
619 if (octx) {
|
|
620 if (ngx_strcmp(ctx->var.data, octx->var.data) != 0) {
|
|
621 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
|
|
622 "limit_req \"%V\" uses the \"%V\" variable "
|
|
623 "while previously it used the \"%V\" variable",
|
480
|
624 &shm_zone->shm.name, &ctx->var, &octx->var);
|
416
|
625 return NGX_ERROR;
|
|
626 }
|
|
627
|
480
|
628 ctx->sh = octx->sh;
|
416
|
629 ctx->shpool = octx->shpool;
|
|
630
|
|
631 return NGX_OK;
|
|
632 }
|
|
633
|
|
634 ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
635
|
480
|
636 if (shm_zone->shm.exists) {
|
|
637 ctx->sh = ctx->shpool->data;
|
|
638
|
|
639 return NGX_OK;
|
416
|
640 }
|
|
641
|
480
|
642 ctx->sh = ngx_slab_alloc(ctx->shpool, sizeof(ngx_http_limit_req_shctx_t));
|
|
643 if (ctx->sh == NULL) {
|
416
|
644 return NGX_ERROR;
|
|
645 }
|
|
646
|
480
|
647 ctx->shpool->data = ctx->sh;
|
|
648
|
|
649 ngx_rbtree_init(&ctx->sh->rbtree, &ctx->sh->sentinel,
|
416
|
650 ngx_http_limit_req_rbtree_insert_value);
|
|
651
|
480
|
652 ngx_queue_init(&ctx->sh->queue);
|
416
|
653
|
480
|
654 len = sizeof(" in limit_req zone \"\"") + shm_zone->shm.name.len;
|
468
|
655
|
|
656 ctx->shpool->log_ctx = ngx_slab_alloc(ctx->shpool, len);
|
|
657 if (ctx->shpool->log_ctx == NULL) {
|
|
658 return NGX_ERROR;
|
|
659 }
|
|
660
|
|
661 ngx_sprintf(ctx->shpool->log_ctx, " in limit_req zone \"%V\"%Z",
|
480
|
662 &shm_zone->shm.name);
|
468
|
663
|
416
|
664 return NGX_OK;
|
|
665 }
|
|
666
|
|
667
|
|
668 static void *
|
|
669 ngx_http_limit_req_create_conf(ngx_conf_t *cf)
|
|
670 {
|
|
671 ngx_http_limit_req_conf_t *conf;
|
|
672
|
|
673 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_conf_t));
|
|
674 if (conf == NULL) {
|
496
|
675 return NULL;
|
416
|
676 }
|
|
677
|
|
678 /*
|
|
679 * set by ngx_pcalloc():
|
|
680 *
|
660
|
681 * conf->limits.elts = NULL;
|
416
|
682 */
|
|
683
|
532
|
684 conf->limit_log_level = NGX_CONF_UNSET_UINT;
|
|
685
|
416
|
686 return conf;
|
|
687 }
|
|
688
|
|
689
|
|
690 static char *
|
|
691 ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
692 {
|
|
693 ngx_http_limit_req_conf_t *prev = parent;
|
|
694 ngx_http_limit_req_conf_t *conf = child;
|
|
695
|
660
|
696 if (conf->limits.elts == NULL) {
|
|
697 conf->limits = prev->limits;
|
416
|
698 }
|
|
699
|
532
|
700 ngx_conf_merge_uint_value(conf->limit_log_level, prev->limit_log_level,
|
|
701 NGX_LOG_ERR);
|
|
702
|
|
703 conf->delay_log_level = (conf->limit_log_level == NGX_LOG_INFO) ?
|
|
704 NGX_LOG_INFO : conf->limit_log_level + 1;
|
|
705
|
416
|
706 return NGX_CONF_OK;
|
|
707 }
|
|
708
|
|
709
|
|
710 static char *
|
|
711 ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
712 {
|
|
713 u_char *p;
|
660
|
714 size_t len;
|
|
715 ssize_t size;
|
416
|
716 ngx_str_t *value, name, s;
|
|
717 ngx_int_t rate, scale;
|
|
718 ngx_uint_t i;
|
|
719 ngx_shm_zone_t *shm_zone;
|
|
720 ngx_http_limit_req_ctx_t *ctx;
|
|
721
|
|
722 value = cf->args->elts;
|
|
723
|
|
724 ctx = NULL;
|
|
725 size = 0;
|
|
726 rate = 1;
|
|
727 scale = 1;
|
|
728 name.len = 0;
|
|
729
|
|
730 for (i = 1; i < cf->args->nelts; i++) {
|
|
731
|
|
732 if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {
|
|
733
|
|
734 name.data = value[i].data + 5;
|
|
735
|
|
736 p = (u_char *) ngx_strchr(name.data, ':');
|
|
737
|
660
|
738 if (p == NULL) {
|
|
739 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
740 "invalid zone size \"%V\"", &value[i]);
|
|
741 return NGX_CONF_ERROR;
|
416
|
742 }
|
|
743
|
660
|
744 name.len = p - name.data;
|
|
745
|
|
746 s.data = p + 1;
|
|
747 s.len = value[i].data + value[i].len - s.data;
|
|
748
|
|
749 size = ngx_parse_size(&s);
|
|
750
|
|
751 if (size == NGX_ERROR) {
|
|
752 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
753 "invalid zone size \"%V\"", &value[i]);
|
|
754 return NGX_CONF_ERROR;
|
|
755 }
|
|
756
|
|
757 if (size < (ssize_t) (8 * ngx_pagesize)) {
|
|
758 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
759 "zone \"%V\" is too small", &value[i]);
|
|
760 return NGX_CONF_ERROR;
|
|
761 }
|
|
762
|
|
763 continue;
|
416
|
764 }
|
|
765
|
|
766 if (ngx_strncmp(value[i].data, "rate=", 5) == 0) {
|
|
767
|
|
768 len = value[i].len;
|
|
769 p = value[i].data + len - 3;
|
|
770
|
|
771 if (ngx_strncmp(p, "r/s", 3) == 0) {
|
|
772 scale = 1;
|
|
773 len -= 3;
|
|
774
|
|
775 } else if (ngx_strncmp(p, "r/m", 3) == 0) {
|
|
776 scale = 60;
|
|
777 len -= 3;
|
|
778 }
|
|
779
|
|
780 rate = ngx_atoi(value[i].data + 5, len - 5);
|
|
781 if (rate <= NGX_ERROR) {
|
|
782 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
783 "invalid rate \"%V\"", &value[i]);
|
|
784 return NGX_CONF_ERROR;
|
|
785 }
|
|
786
|
|
787 continue;
|
|
788 }
|
|
789
|
|
790 if (value[i].data[0] == '$') {
|
|
791
|
|
792 value[i].len--;
|
|
793 value[i].data++;
|
|
794
|
|
795 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_ctx_t));
|
|
796 if (ctx == NULL) {
|
|
797 return NGX_CONF_ERROR;
|
|
798 }
|
|
799
|
|
800 ctx->index = ngx_http_get_variable_index(cf, &value[i]);
|
|
801 if (ctx->index == NGX_ERROR) {
|
|
802 return NGX_CONF_ERROR;
|
|
803 }
|
|
804
|
|
805 ctx->var = value[i];
|
|
806
|
|
807 continue;
|
|
808 }
|
|
809
|
|
810 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
811 "invalid parameter \"%V\"", &value[i]);
|
|
812 return NGX_CONF_ERROR;
|
|
813 }
|
|
814
|
660
|
815 if (name.len == 0) {
|
416
|
816 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
817 "\"%V\" must have \"zone\" parameter",
|
|
818 &cmd->name);
|
|
819 return NGX_CONF_ERROR;
|
|
820 }
|
|
821
|
|
822 if (ctx == NULL) {
|
|
823 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
660
|
824 "no variable is defined for %V \"%V\"",
|
|
825 &cmd->name, &name);
|
416
|
826 return NGX_CONF_ERROR;
|
|
827 }
|
|
828
|
420
|
829 ctx->rate = rate * 1000 / scale;
|
416
|
830
|
|
831 shm_zone = ngx_shared_memory_add(cf, &name, size,
|
|
832 &ngx_http_limit_req_module);
|
|
833 if (shm_zone == NULL) {
|
|
834 return NGX_CONF_ERROR;
|
|
835 }
|
|
836
|
|
837 if (shm_zone->data) {
|
|
838 ctx = shm_zone->data;
|
|
839
|
|
840 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
660
|
841 "%V \"%V\" is already bound to variable \"%V\"",
|
|
842 &cmd->name, &name, &ctx->var);
|
416
|
843 return NGX_CONF_ERROR;
|
|
844 }
|
|
845
|
|
846 shm_zone->init = ngx_http_limit_req_init_zone;
|
|
847 shm_zone->data = ctx;
|
|
848
|
|
849 return NGX_CONF_OK;
|
|
850 }
|
|
851
|
|
852
|
|
853 static char *
|
|
854 ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
855 {
|
426
|
856 ngx_http_limit_req_conf_t *lrcf = conf;
|
416
|
857
|
660
|
858 ngx_int_t burst;
|
|
859 ngx_str_t *value, s;
|
|
860 ngx_uint_t i, nodelay;
|
|
861 ngx_shm_zone_t *shm_zone;
|
|
862 ngx_http_limit_req_limit_t *limit, *limits;
|
416
|
863
|
|
864 value = cf->args->elts;
|
|
865
|
660
|
866 shm_zone = NULL;
|
416
|
867 burst = 0;
|
660
|
868 nodelay = 0;
|
416
|
869
|
|
870 for (i = 1; i < cf->args->nelts; i++) {
|
|
871
|
|
872 if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {
|
|
873
|
|
874 s.len = value[i].len - 5;
|
|
875 s.data = value[i].data + 5;
|
|
876
|
660
|
877 shm_zone = ngx_shared_memory_add(cf, &s, 0,
|
|
878 &ngx_http_limit_req_module);
|
|
879 if (shm_zone == NULL) {
|
416
|
880 return NGX_CONF_ERROR;
|
|
881 }
|
|
882
|
|
883 continue;
|
|
884 }
|
|
885
|
|
886 if (ngx_strncmp(value[i].data, "burst=", 6) == 0) {
|
|
887
|
418
|
888 burst = ngx_atoi(value[i].data + 6, value[i].len - 6);
|
416
|
889 if (burst <= 0) {
|
|
890 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
891 "invalid burst rate \"%V\"", &value[i]);
|
|
892 return NGX_CONF_ERROR;
|
|
893 }
|
|
894
|
|
895 continue;
|
|
896 }
|
|
897
|
418
|
898 if (ngx_strncmp(value[i].data, "nodelay", 7) == 0) {
|
660
|
899 nodelay = 1;
|
416
|
900 continue;
|
|
901 }
|
|
902
|
|
903 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
904 "invalid parameter \"%V\"", &value[i]);
|
|
905 return NGX_CONF_ERROR;
|
|
906 }
|
|
907
|
660
|
908 if (shm_zone == NULL) {
|
416
|
909 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
910 "\"%V\" must have \"zone\" parameter",
|
|
911 &cmd->name);
|
|
912 return NGX_CONF_ERROR;
|
|
913 }
|
|
914
|
660
|
915 if (shm_zone->data == NULL) {
|
416
|
916 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
917 "unknown limit_req_zone \"%V\"",
|
660
|
918 &shm_zone->shm.name);
|
416
|
919 return NGX_CONF_ERROR;
|
|
920 }
|
|
921
|
660
|
922 limits = lrcf->limits.elts;
|
|
923
|
|
924 if (limits == NULL) {
|
|
925 if (ngx_array_init(&lrcf->limits, cf->pool, 1,
|
|
926 sizeof(ngx_http_limit_req_limit_t))
|
|
927 != NGX_OK)
|
|
928 {
|
|
929 return NGX_CONF_ERROR;
|
|
930 }
|
|
931 }
|
|
932
|
|
933 for (i = 0; i < lrcf->limits.nelts; i++) {
|
|
934 if (shm_zone == limits[i].shm_zone) {
|
|
935 return "is duplicate";
|
|
936 }
|
|
937 }
|
|
938
|
|
939 limit = ngx_array_push(&lrcf->limits);
|
684
|
940 if (limit == NULL) {
|
|
941 return NGX_CONF_ERROR;
|
|
942 }
|
660
|
943
|
|
944 limit->shm_zone = shm_zone;
|
|
945 limit->burst = burst * 1000;
|
|
946 limit->nodelay = nodelay;
|
416
|
947
|
|
948 return NGX_CONF_OK;
|
|
949 }
|
|
950
|
|
951
|
|
952 static ngx_int_t
|
|
953 ngx_http_limit_req_init(ngx_conf_t *cf)
|
|
954 {
|
|
955 ngx_http_handler_pt *h;
|
|
956 ngx_http_core_main_conf_t *cmcf;
|
|
957
|
|
958 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
959
|
|
960 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
|
|
961 if (h == NULL) {
|
|
962 return NGX_ERROR;
|
|
963 }
|
|
964
|
|
965 *h = ngx_http_limit_req_handler;
|
|
966
|
|
967 return NGX_OK;
|
|
968 }
|