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