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
|
|
124 ngx_http_limit_req_create_conf, /* create location configration */
|
|
125 ngx_http_limit_req_merge_conf /* merge location configration */
|
|
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
|
|
388 do {
|
426
|
389 lr = (ngx_http_limit_req_node_t *) &node->color;
|
416
|
390
|
426
|
391 rc = ngx_memn2cmp(data, lr->data, len, (size_t) lr->len);
|
416
|
392
|
|
393 if (rc == 0) {
|
602
|
394 ngx_queue_remove(&lr->queue);
|
|
395 ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
|
416
|
396
|
426
|
397 ms = (ngx_msec_int_t) (now - lr->last);
|
416
|
398
|
426
|
399 excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
|
416
|
400
|
534
|
401 if (excess < 0) {
|
|
402 excess = 0;
|
|
403 }
|
|
404
|
602
|
405 *ep = excess;
|
|
406
|
660
|
407 if ((ngx_uint_t) excess > limit->burst) {
|
532
|
408 return NGX_BUSY;
|
|
409 }
|
|
410
|
660
|
411 if (account) {
|
|
412 lr->excess = excess;
|
|
413 lr->last = now;
|
|
414 return NGX_OK;
|
416
|
415 }
|
|
416
|
660
|
417 lr->count++;
|
|
418
|
|
419 ctx->node = lr;
|
|
420
|
|
421 return NGX_AGAIN;
|
416
|
422 }
|
|
423
|
|
424 node = (rc < 0) ? node->left : node->right;
|
|
425
|
|
426 } while (node != sentinel && hash == node->key);
|
|
427
|
|
428 break;
|
|
429 }
|
|
430
|
602
|
431 *ep = 0;
|
416
|
432
|
660
|
433 size = offsetof(ngx_rbtree_node_t, color)
|
|
434 + offsetof(ngx_http_limit_req_node_t, data)
|
|
435 + len;
|
|
436
|
|
437 ngx_http_limit_req_expire(ctx, 1);
|
|
438
|
|
439 node = ngx_slab_alloc_locked(ctx->shpool, size);
|
|
440
|
|
441 if (node == NULL) {
|
|
442 ngx_http_limit_req_expire(ctx, 0);
|
|
443
|
|
444 node = ngx_slab_alloc_locked(ctx->shpool, size);
|
|
445 if (node == NULL) {
|
|
446 return NGX_ERROR;
|
|
447 }
|
|
448 }
|
|
449
|
|
450 node->key = hash;
|
|
451
|
|
452 ngx_rbtree_insert(&ctx->sh->rbtree, node);
|
|
453
|
|
454 lr = (ngx_http_limit_req_node_t *) &node->color;
|
|
455
|
|
456 ngx_queue_insert_head(&ctx->sh->queue, &lr->queue);
|
|
457
|
|
458 lr->len = (u_char) len;
|
|
459 lr->excess = 0;
|
|
460
|
|
461 ngx_memcpy(lr->data, data, len);
|
|
462
|
|
463 if (account) {
|
|
464 lr->last = now;
|
|
465 lr->count = 0;
|
|
466 return NGX_OK;
|
|
467 }
|
|
468
|
|
469 lr->last = 0;
|
|
470 lr->count = 1;
|
|
471
|
|
472 ctx->node = lr;
|
|
473
|
|
474 return NGX_AGAIN;
|
|
475 }
|
|
476
|
|
477
|
|
478 static ngx_msec_t
|
|
479 ngx_http_limit_req_account(ngx_http_limit_req_limit_t *limits, ngx_uint_t n,
|
|
480 ngx_uint_t *ep, ngx_http_limit_req_limit_t **limit)
|
|
481 {
|
|
482 ngx_int_t excess;
|
|
483 ngx_time_t *tp;
|
|
484 ngx_msec_t now, delay, max_delay;
|
|
485 ngx_msec_int_t ms;
|
|
486 ngx_http_limit_req_ctx_t *ctx;
|
|
487 ngx_http_limit_req_node_t *lr;
|
|
488
|
|
489 excess = *ep;
|
|
490
|
|
491 if (excess == 0 || (*limit)->nodelay) {
|
|
492 max_delay = 0;
|
|
493
|
|
494 } else {
|
|
495 ctx = (*limit)->shm_zone->data;
|
|
496 max_delay = excess * 1000 / ctx->rate;
|
|
497 }
|
|
498
|
|
499 while (n--) {
|
|
500 ctx = limits[n].shm_zone->data;
|
|
501 lr = ctx->node;
|
|
502
|
|
503 if (lr == NULL) {
|
|
504 continue;
|
|
505 }
|
|
506
|
|
507 ngx_shmtx_lock(&ctx->shpool->mutex);
|
|
508
|
|
509 tp = ngx_timeofday();
|
|
510
|
|
511 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
|
|
512 ms = (ngx_msec_int_t) (now - lr->last);
|
|
513
|
|
514 excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000;
|
|
515
|
|
516 if (excess < 0) {
|
|
517 excess = 0;
|
|
518 }
|
|
519
|
|
520 lr->last = now;
|
|
521 lr->excess = excess;
|
|
522 lr->count--;
|
|
523
|
|
524 ngx_shmtx_unlock(&ctx->shpool->mutex);
|
|
525
|
|
526 ctx->node = NULL;
|
|
527
|
|
528 if (limits[n].nodelay) {
|
|
529 continue;
|
|
530 }
|
|
531
|
|
532 delay = excess * 1000 / ctx->rate;
|
|
533
|
|
534 if (delay > max_delay) {
|
|
535 max_delay = delay;
|
|
536 *ep = excess;
|
|
537 *limit = &limits[n];
|
|
538 }
|
|
539 }
|
|
540
|
|
541 return max_delay;
|
416
|
542 }
|
|
543
|
|
544
|
|
545 static void
|
|
546 ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx, ngx_uint_t n)
|
|
547 {
|
420
|
548 ngx_int_t excess;
|
416
|
549 ngx_time_t *tp;
|
|
550 ngx_msec_t now;
|
|
551 ngx_queue_t *q;
|
|
552 ngx_msec_int_t ms;
|
|
553 ngx_rbtree_node_t *node;
|
426
|
554 ngx_http_limit_req_node_t *lr;
|
416
|
555
|
|
556 tp = ngx_timeofday();
|
|
557
|
|
558 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec);
|
|
559
|
|
560 /*
|
|
561 * n == 1 deletes one or two zero rate entries
|
|
562 * n == 0 deletes oldest entry by force
|
|
563 * and one or two zero rate entries
|
|
564 */
|
|
565
|
|
566 while (n < 3) {
|
|
567
|
480
|
568 if (ngx_queue_empty(&ctx->sh->queue)) {
|
416
|
569 return;
|
|
570 }
|
|
571
|
480
|
572 q = ngx_queue_last(&ctx->sh->queue);
|
416
|
573
|
426
|
574 lr = ngx_queue_data(q, ngx_http_limit_req_node_t, queue);
|
416
|
575
|
660
|
576 if (lr->count) {
|
|
577
|
|
578 /*
|
|
579 * There is not much sense in looking further,
|
|
580 * because we bump nodes on the lookup stage.
|
|
581 */
|
|
582
|
|
583 return;
|
|
584 }
|
|
585
|
416
|
586 if (n++ != 0) {
|
|
587
|
426
|
588 ms = (ngx_msec_int_t) (now - lr->last);
|
416
|
589 ms = ngx_abs(ms);
|
|
590
|
|
591 if (ms < 60000) {
|
|
592 return;
|
|
593 }
|
|
594
|
426
|
595 excess = lr->excess - ctx->rate * ms / 1000;
|
416
|
596
|
420
|
597 if (excess > 0) {
|
416
|
598 return;
|
|
599 }
|
|
600 }
|
|
601
|
|
602 ngx_queue_remove(q);
|
|
603
|
|
604 node = (ngx_rbtree_node_t *)
|
426
|
605 ((u_char *) lr - offsetof(ngx_rbtree_node_t, color));
|
416
|
606
|
480
|
607 ngx_rbtree_delete(&ctx->sh->rbtree, node);
|
416
|
608
|
|
609 ngx_slab_free_locked(ctx->shpool, node);
|
|
610 }
|
|
611 }
|
|
612
|
|
613
|
|
614 static ngx_int_t
|
|
615 ngx_http_limit_req_init_zone(ngx_shm_zone_t *shm_zone, void *data)
|
|
616 {
|
|
617 ngx_http_limit_req_ctx_t *octx = data;
|
|
618
|
468
|
619 size_t len;
|
416
|
620 ngx_http_limit_req_ctx_t *ctx;
|
|
621
|
|
622 ctx = shm_zone->data;
|
|
623
|
|
624 if (octx) {
|
|
625 if (ngx_strcmp(ctx->var.data, octx->var.data) != 0) {
|
|
626 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
|
|
627 "limit_req \"%V\" uses the \"%V\" variable "
|
|
628 "while previously it used the \"%V\" variable",
|
480
|
629 &shm_zone->shm.name, &ctx->var, &octx->var);
|
416
|
630 return NGX_ERROR;
|
|
631 }
|
|
632
|
480
|
633 ctx->sh = octx->sh;
|
416
|
634 ctx->shpool = octx->shpool;
|
|
635
|
|
636 return NGX_OK;
|
|
637 }
|
|
638
|
|
639 ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
|
|
640
|
480
|
641 if (shm_zone->shm.exists) {
|
|
642 ctx->sh = ctx->shpool->data;
|
|
643
|
|
644 return NGX_OK;
|
416
|
645 }
|
|
646
|
480
|
647 ctx->sh = ngx_slab_alloc(ctx->shpool, sizeof(ngx_http_limit_req_shctx_t));
|
|
648 if (ctx->sh == NULL) {
|
416
|
649 return NGX_ERROR;
|
|
650 }
|
|
651
|
480
|
652 ctx->shpool->data = ctx->sh;
|
|
653
|
|
654 ngx_rbtree_init(&ctx->sh->rbtree, &ctx->sh->sentinel,
|
416
|
655 ngx_http_limit_req_rbtree_insert_value);
|
|
656
|
480
|
657 ngx_queue_init(&ctx->sh->queue);
|
416
|
658
|
480
|
659 len = sizeof(" in limit_req zone \"\"") + shm_zone->shm.name.len;
|
468
|
660
|
|
661 ctx->shpool->log_ctx = ngx_slab_alloc(ctx->shpool, len);
|
|
662 if (ctx->shpool->log_ctx == NULL) {
|
|
663 return NGX_ERROR;
|
|
664 }
|
|
665
|
|
666 ngx_sprintf(ctx->shpool->log_ctx, " in limit_req zone \"%V\"%Z",
|
480
|
667 &shm_zone->shm.name);
|
468
|
668
|
416
|
669 return NGX_OK;
|
|
670 }
|
|
671
|
|
672
|
|
673 static void *
|
|
674 ngx_http_limit_req_create_conf(ngx_conf_t *cf)
|
|
675 {
|
|
676 ngx_http_limit_req_conf_t *conf;
|
|
677
|
|
678 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_conf_t));
|
|
679 if (conf == NULL) {
|
496
|
680 return NULL;
|
416
|
681 }
|
|
682
|
|
683 /*
|
|
684 * set by ngx_pcalloc():
|
|
685 *
|
660
|
686 * conf->limits.elts = NULL;
|
416
|
687 */
|
|
688
|
532
|
689 conf->limit_log_level = NGX_CONF_UNSET_UINT;
|
|
690
|
416
|
691 return conf;
|
|
692 }
|
|
693
|
|
694
|
|
695 static char *
|
|
696 ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent, void *child)
|
|
697 {
|
|
698 ngx_http_limit_req_conf_t *prev = parent;
|
|
699 ngx_http_limit_req_conf_t *conf = child;
|
|
700
|
660
|
701 if (conf->limits.elts == NULL) {
|
|
702 conf->limits = prev->limits;
|
416
|
703 }
|
|
704
|
532
|
705 ngx_conf_merge_uint_value(conf->limit_log_level, prev->limit_log_level,
|
|
706 NGX_LOG_ERR);
|
|
707
|
|
708 conf->delay_log_level = (conf->limit_log_level == NGX_LOG_INFO) ?
|
|
709 NGX_LOG_INFO : conf->limit_log_level + 1;
|
|
710
|
416
|
711 return NGX_CONF_OK;
|
|
712 }
|
|
713
|
|
714
|
|
715 static char *
|
|
716 ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
717 {
|
|
718 u_char *p;
|
660
|
719 size_t len;
|
|
720 ssize_t size;
|
416
|
721 ngx_str_t *value, name, s;
|
|
722 ngx_int_t rate, scale;
|
|
723 ngx_uint_t i;
|
|
724 ngx_shm_zone_t *shm_zone;
|
|
725 ngx_http_limit_req_ctx_t *ctx;
|
|
726
|
|
727 value = cf->args->elts;
|
|
728
|
|
729 ctx = NULL;
|
|
730 size = 0;
|
|
731 rate = 1;
|
|
732 scale = 1;
|
|
733 name.len = 0;
|
|
734
|
|
735 for (i = 1; i < cf->args->nelts; i++) {
|
|
736
|
|
737 if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {
|
|
738
|
|
739 name.data = value[i].data + 5;
|
|
740
|
|
741 p = (u_char *) ngx_strchr(name.data, ':');
|
|
742
|
660
|
743 if (p == NULL) {
|
|
744 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
745 "invalid zone size \"%V\"", &value[i]);
|
|
746 return NGX_CONF_ERROR;
|
416
|
747 }
|
|
748
|
660
|
749 name.len = p - name.data;
|
|
750
|
|
751 s.data = p + 1;
|
|
752 s.len = value[i].data + value[i].len - s.data;
|
|
753
|
|
754 size = ngx_parse_size(&s);
|
|
755
|
|
756 if (size == NGX_ERROR) {
|
|
757 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
758 "invalid zone size \"%V\"", &value[i]);
|
|
759 return NGX_CONF_ERROR;
|
|
760 }
|
|
761
|
|
762 if (size < (ssize_t) (8 * ngx_pagesize)) {
|
|
763 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
764 "zone \"%V\" is too small", &value[i]);
|
|
765 return NGX_CONF_ERROR;
|
|
766 }
|
|
767
|
|
768 continue;
|
416
|
769 }
|
|
770
|
|
771 if (ngx_strncmp(value[i].data, "rate=", 5) == 0) {
|
|
772
|
|
773 len = value[i].len;
|
|
774 p = value[i].data + len - 3;
|
|
775
|
|
776 if (ngx_strncmp(p, "r/s", 3) == 0) {
|
|
777 scale = 1;
|
|
778 len -= 3;
|
|
779
|
|
780 } else if (ngx_strncmp(p, "r/m", 3) == 0) {
|
|
781 scale = 60;
|
|
782 len -= 3;
|
|
783 }
|
|
784
|
|
785 rate = ngx_atoi(value[i].data + 5, len - 5);
|
|
786 if (rate <= NGX_ERROR) {
|
|
787 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
788 "invalid rate \"%V\"", &value[i]);
|
|
789 return NGX_CONF_ERROR;
|
|
790 }
|
|
791
|
|
792 continue;
|
|
793 }
|
|
794
|
|
795 if (value[i].data[0] == '$') {
|
|
796
|
|
797 value[i].len--;
|
|
798 value[i].data++;
|
|
799
|
|
800 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_ctx_t));
|
|
801 if (ctx == NULL) {
|
|
802 return NGX_CONF_ERROR;
|
|
803 }
|
|
804
|
|
805 ctx->index = ngx_http_get_variable_index(cf, &value[i]);
|
|
806 if (ctx->index == NGX_ERROR) {
|
|
807 return NGX_CONF_ERROR;
|
|
808 }
|
|
809
|
|
810 ctx->var = value[i];
|
|
811
|
|
812 continue;
|
|
813 }
|
|
814
|
|
815 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
816 "invalid parameter \"%V\"", &value[i]);
|
|
817 return NGX_CONF_ERROR;
|
|
818 }
|
|
819
|
660
|
820 if (name.len == 0) {
|
416
|
821 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
822 "\"%V\" must have \"zone\" parameter",
|
|
823 &cmd->name);
|
|
824 return NGX_CONF_ERROR;
|
|
825 }
|
|
826
|
|
827 if (ctx == NULL) {
|
|
828 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
660
|
829 "no variable is defined for %V \"%V\"",
|
|
830 &cmd->name, &name);
|
416
|
831 return NGX_CONF_ERROR;
|
|
832 }
|
|
833
|
420
|
834 ctx->rate = rate * 1000 / scale;
|
416
|
835
|
|
836 shm_zone = ngx_shared_memory_add(cf, &name, size,
|
|
837 &ngx_http_limit_req_module);
|
|
838 if (shm_zone == NULL) {
|
|
839 return NGX_CONF_ERROR;
|
|
840 }
|
|
841
|
|
842 if (shm_zone->data) {
|
|
843 ctx = shm_zone->data;
|
|
844
|
|
845 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
660
|
846 "%V \"%V\" is already bound to variable \"%V\"",
|
|
847 &cmd->name, &name, &ctx->var);
|
416
|
848 return NGX_CONF_ERROR;
|
|
849 }
|
|
850
|
|
851 shm_zone->init = ngx_http_limit_req_init_zone;
|
|
852 shm_zone->data = ctx;
|
|
853
|
|
854 return NGX_CONF_OK;
|
|
855 }
|
|
856
|
|
857
|
|
858 static char *
|
|
859 ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
|
860 {
|
426
|
861 ngx_http_limit_req_conf_t *lrcf = conf;
|
416
|
862
|
660
|
863 ngx_int_t burst;
|
|
864 ngx_str_t *value, s;
|
|
865 ngx_uint_t i, nodelay;
|
|
866 ngx_shm_zone_t *shm_zone;
|
|
867 ngx_http_limit_req_limit_t *limit, *limits;
|
416
|
868
|
|
869 value = cf->args->elts;
|
|
870
|
660
|
871 shm_zone = NULL;
|
416
|
872 burst = 0;
|
660
|
873 nodelay = 0;
|
416
|
874
|
|
875 for (i = 1; i < cf->args->nelts; i++) {
|
|
876
|
|
877 if (ngx_strncmp(value[i].data, "zone=", 5) == 0) {
|
|
878
|
|
879 s.len = value[i].len - 5;
|
|
880 s.data = value[i].data + 5;
|
|
881
|
660
|
882 shm_zone = ngx_shared_memory_add(cf, &s, 0,
|
|
883 &ngx_http_limit_req_module);
|
|
884 if (shm_zone == NULL) {
|
416
|
885 return NGX_CONF_ERROR;
|
|
886 }
|
|
887
|
|
888 continue;
|
|
889 }
|
|
890
|
|
891 if (ngx_strncmp(value[i].data, "burst=", 6) == 0) {
|
|
892
|
418
|
893 burst = ngx_atoi(value[i].data + 6, value[i].len - 6);
|
416
|
894 if (burst <= 0) {
|
|
895 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
896 "invalid burst rate \"%V\"", &value[i]);
|
|
897 return NGX_CONF_ERROR;
|
|
898 }
|
|
899
|
|
900 continue;
|
|
901 }
|
|
902
|
418
|
903 if (ngx_strncmp(value[i].data, "nodelay", 7) == 0) {
|
660
|
904 nodelay = 1;
|
416
|
905 continue;
|
|
906 }
|
|
907
|
|
908 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
909 "invalid parameter \"%V\"", &value[i]);
|
|
910 return NGX_CONF_ERROR;
|
|
911 }
|
|
912
|
660
|
913 if (shm_zone == NULL) {
|
416
|
914 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
915 "\"%V\" must have \"zone\" parameter",
|
|
916 &cmd->name);
|
|
917 return NGX_CONF_ERROR;
|
|
918 }
|
|
919
|
660
|
920 if (shm_zone->data == NULL) {
|
416
|
921 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
|
|
922 "unknown limit_req_zone \"%V\"",
|
660
|
923 &shm_zone->shm.name);
|
416
|
924 return NGX_CONF_ERROR;
|
|
925 }
|
|
926
|
660
|
927 limits = lrcf->limits.elts;
|
|
928
|
|
929 if (limits == NULL) {
|
|
930 if (ngx_array_init(&lrcf->limits, cf->pool, 1,
|
|
931 sizeof(ngx_http_limit_req_limit_t))
|
|
932 != NGX_OK)
|
|
933 {
|
|
934 return NGX_CONF_ERROR;
|
|
935 }
|
|
936 }
|
|
937
|
|
938 for (i = 0; i < lrcf->limits.nelts; i++) {
|
|
939 if (shm_zone == limits[i].shm_zone) {
|
|
940 return "is duplicate";
|
|
941 }
|
|
942 }
|
|
943
|
|
944 limit = ngx_array_push(&lrcf->limits);
|
|
945
|
|
946 limit->shm_zone = shm_zone;
|
|
947 limit->burst = burst * 1000;
|
|
948 limit->nodelay = nodelay;
|
416
|
949
|
|
950 return NGX_CONF_OK;
|
|
951 }
|
|
952
|
|
953
|
|
954 static ngx_int_t
|
|
955 ngx_http_limit_req_init(ngx_conf_t *cf)
|
|
956 {
|
|
957 ngx_http_handler_pt *h;
|
|
958 ngx_http_core_main_conf_t *cmcf;
|
|
959
|
|
960 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
|
|
961
|
|
962 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
|
|
963 if (h == NULL) {
|
|
964 return NGX_ERROR;
|
|
965 }
|
|
966
|
|
967 *h = ngx_http_limit_req_handler;
|
|
968
|
|
969 return NGX_OK;
|
|
970 }
|