annotate src/http/modules/ngx_http_limit_req_module.c @ 4418:aac79fc948cc

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