Mercurial > hg > nginx
annotate src/http/modules/ngx_http_limit_zone_module.c @ 1184:040907ba513c
release-0.5.19 tag
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 24 Apr 2007 06:21:00 +0000 |
parents | defdce84b172 |
children | 03341711f9a2 |
rev | line source |
---|---|
980 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
12 typedef struct { | |
981 | 13 u_char color; |
14 u_char len; | |
980 | 15 u_short conn; |
16 u_char data[1]; | |
17 } ngx_http_limit_zone_node_t; | |
18 | |
19 | |
20 typedef struct { | |
21 ngx_shm_zone_t *shm_zone; | |
22 ngx_rbtree_node_t *node; | |
23 } ngx_http_limit_zone_cleanup_t; | |
24 | |
25 | |
26 typedef struct { | |
987 | 27 ngx_rbtree_t *rbtree; |
28 ngx_int_t index; | |
29 ngx_str_t var; | |
30 } ngx_http_limit_zone_ctx_t; | |
31 | |
32 | |
33 typedef struct { | |
980 | 34 ngx_shm_zone_t *shm_zone; |
35 ngx_uint_t conn; | |
36 } ngx_http_limit_zone_conf_t; | |
37 | |
38 | |
39 static void ngx_http_limit_zone_cleanup(void *data); | |
40 | |
41 static void *ngx_http_limit_zone_create_conf(ngx_conf_t *cf); | |
42 static char *ngx_http_limit_zone_merge_conf(ngx_conf_t *cf, void *parent, | |
43 void *child); | |
44 static char *ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, | |
45 void *conf); | |
46 static char *ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, | |
47 void *conf); | |
48 static ngx_int_t ngx_http_limit_zone_init(ngx_conf_t *cf); | |
49 | |
50 | |
51 static ngx_command_t ngx_http_limit_zone_commands[] = { | |
52 | |
53 { ngx_string("limit_zone"), | |
987 | 54 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3, |
980 | 55 ngx_http_limit_zone, |
56 0, | |
57 0, | |
58 NULL }, | |
59 | |
60 { ngx_string("limit_conn"), | |
987 | 61 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, |
980 | 62 ngx_http_limit_conn, |
63 NGX_HTTP_LOC_CONF_OFFSET, | |
64 0, | |
65 NULL }, | |
66 | |
67 ngx_null_command | |
68 }; | |
69 | |
70 | |
71 static ngx_http_module_t ngx_http_limit_zone_module_ctx = { | |
72 NULL, /* preconfiguration */ | |
73 ngx_http_limit_zone_init, /* postconfiguration */ | |
74 | |
75 NULL, /* create main configuration */ | |
76 NULL, /* init main configuration */ | |
77 | |
78 NULL, /* create server configuration */ | |
79 NULL, /* merge server configuration */ | |
80 | |
81 ngx_http_limit_zone_create_conf, /* create location configration */ | |
82 ngx_http_limit_zone_merge_conf /* merge location configration */ | |
83 }; | |
84 | |
85 | |
86 ngx_module_t ngx_http_limit_zone_module = { | |
87 NGX_MODULE_V1, | |
88 &ngx_http_limit_zone_module_ctx, /* module context */ | |
89 ngx_http_limit_zone_commands, /* module directives */ | |
90 NGX_HTTP_MODULE, /* module type */ | |
91 NULL, /* init master */ | |
92 NULL, /* init module */ | |
93 NULL, /* init process */ | |
94 NULL, /* init thread */ | |
95 NULL, /* exit thread */ | |
96 NULL, /* exit process */ | |
97 NULL, /* exit master */ | |
98 NGX_MODULE_V1_PADDING | |
99 }; | |
100 | |
101 | |
102 static ngx_int_t | |
103 ngx_http_limit_zone_handler(ngx_http_request_t *r) | |
104 { | |
105 size_t len, n; | |
106 uint32_t hash; | |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
107 ngx_int_t rc; |
980 | 108 ngx_slab_pool_t *shpool; |
109 ngx_rbtree_node_t *node, *sentinel; | |
110 ngx_pool_cleanup_t *cln; | |
111 ngx_http_variable_value_t *vv; | |
987 | 112 ngx_http_limit_zone_ctx_t *ctx; |
980 | 113 ngx_http_limit_zone_node_t *lz; |
114 ngx_http_limit_zone_conf_t *lzcf; | |
115 ngx_http_limit_zone_cleanup_t *lzcln; | |
116 | |
997
5900ec20604a
do not count connection for subrequest
Igor Sysoev <igor@sysoev.ru>
parents:
993
diff
changeset
|
117 if (r->main->limit_zone_set) { |
984
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
118 return NGX_DECLINED; |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
119 } |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
120 |
980 | 121 lzcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_zone_module); |
122 | |
123 if (lzcf->shm_zone == NULL) { | |
124 return NGX_DECLINED; | |
125 } | |
126 | |
987 | 127 ctx = lzcf->shm_zone->data; |
128 | |
129 vv = ngx_http_get_indexed_variable(r, ctx->index); | |
980 | 130 |
131 if (vv == NULL || vv->not_found) { | |
132 return NGX_DECLINED; | |
133 } | |
134 | |
1011
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
135 len = vv->len; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
136 |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
137 if (len == 0) { |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
138 return NGX_DECLINED; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
139 } |
984
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
140 |
1011
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
141 if (len > 255) { |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
142 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
|
143 "the value of the \"%V\" variable " |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
144 "is more than 255 bytes: \"%V\"", |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
145 &ctx->var, vv); |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
146 return NGX_DECLINED; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
147 } |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
148 |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
149 r->main->limit_zone_set = 1; |
980 | 150 |
151 hash = ngx_crc32_short(vv->data, len); | |
152 | |
153 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_limit_zone_cleanup_t)); | |
154 if (cln == NULL) { | |
155 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
156 } | |
157 | |
158 shpool = (ngx_slab_pool_t *) lzcf->shm_zone->shm.addr; | |
159 | |
160 ngx_shmtx_lock(&shpool->mutex); | |
161 | |
987 | 162 node = ctx->rbtree->root; |
163 sentinel = ctx->rbtree->sentinel; | |
980 | 164 |
165 while (node != sentinel) { | |
166 | |
167 if (hash < node->key) { | |
168 node = node->left; | |
169 continue; | |
170 } | |
171 | |
172 if (hash > node->key) { | |
173 node = node->right; | |
174 continue; | |
175 } | |
176 | |
1012
11ffb8e4753f
stop rbtree search early if equal hash was found
Igor Sysoev <igor@sysoev.ru>
parents:
1011
diff
changeset
|
177 /* hash == node->key */ |
11ffb8e4753f
stop rbtree search early if equal hash was found
Igor Sysoev <igor@sysoev.ru>
parents:
1011
diff
changeset
|
178 |
11ffb8e4753f
stop rbtree search early if equal hash was found
Igor Sysoev <igor@sysoev.ru>
parents:
1011
diff
changeset
|
179 do { |
981 | 180 lz = (ngx_http_limit_zone_node_t *) &node->color; |
980 | 181 |
1036 | 182 rc = ngx_memn2cmp(vv->data, lz->data, len, (size_t) lz->len); |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
183 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
184 if (rc == 0) { |
981 | 185 if ((ngx_uint_t) lz->conn < lzcf->conn) { |
980 | 186 lz->conn++; |
187 goto done; | |
188 } | |
189 | |
190 ngx_shmtx_unlock(&shpool->mutex); | |
191 | |
192 return NGX_HTTP_SERVICE_UNAVAILABLE; | |
193 } | |
1012
11ffb8e4753f
stop rbtree search early if equal hash was found
Igor Sysoev <igor@sysoev.ru>
parents:
1011
diff
changeset
|
194 |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
195 node = (rc < 0) ? node->left : node->right; |
1012
11ffb8e4753f
stop rbtree search early if equal hash was found
Igor Sysoev <igor@sysoev.ru>
parents:
1011
diff
changeset
|
196 |
11ffb8e4753f
stop rbtree search early if equal hash was found
Igor Sysoev <igor@sysoev.ru>
parents:
1011
diff
changeset
|
197 } while (node != sentinel && hash == node->key); |
11ffb8e4753f
stop rbtree search early if equal hash was found
Igor Sysoev <igor@sysoev.ru>
parents:
1011
diff
changeset
|
198 |
11ffb8e4753f
stop rbtree search early if equal hash was found
Igor Sysoev <igor@sysoev.ru>
parents:
1011
diff
changeset
|
199 break; |
980 | 200 } |
201 | |
981 | 202 n = offsetof(ngx_rbtree_node_t, color) |
980 | 203 + offsetof(ngx_http_limit_zone_node_t, data) |
204 + len; | |
205 | |
206 node = ngx_slab_alloc_locked(shpool, n); | |
207 if (node == NULL) { | |
208 ngx_shmtx_unlock(&shpool->mutex); | |
209 return NGX_HTTP_SERVICE_UNAVAILABLE; | |
210 } | |
211 | |
981 | 212 lz = (ngx_http_limit_zone_node_t *) &node->color; |
980 | 213 |
214 node->key = hash; | |
981 | 215 lz->len = (u_char) len; |
980 | 216 lz->conn = 1; |
217 ngx_memcpy(lz->data, vv->data, len); | |
218 | |
987 | 219 ngx_rbtree_insert(ctx->rbtree, node); |
980 | 220 |
221 done: | |
222 | |
223 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
224 "limit zone: %08XD %d", node->key, lz->conn); | |
225 | |
226 ngx_shmtx_unlock(&shpool->mutex); | |
227 | |
228 cln->handler = ngx_http_limit_zone_cleanup; | |
229 lzcln = cln->data; | |
230 | |
231 lzcln->shm_zone = lzcf->shm_zone; | |
232 lzcln->node = node; | |
233 | |
234 return NGX_DECLINED; | |
235 } | |
236 | |
237 | |
238 static void | |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
239 ngx_http_limit_zone_rbtree_insert_value(ngx_rbtree_node_t *temp, |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
240 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
|
241 { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
242 ngx_http_limit_zone_node_t *lzn, *lznt; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
243 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
244 for ( ;; ) { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
245 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
246 if (node->key < temp->key) { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
247 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
248 if (temp->left == sentinel) { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
249 temp->left = node; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
250 break; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
251 } |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
252 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
253 temp = temp->left; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
254 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
255 } else if (node->key > temp->key) { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
256 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
257 if (temp->right == sentinel) { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
258 temp->right = node; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
259 break; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
260 } |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
261 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
262 temp = temp->right; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
263 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
264 } else { /* node->key == temp->key */ |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
265 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
266 lzn = (ngx_http_limit_zone_node_t *) &node->color; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
267 lznt = (ngx_http_limit_zone_node_t *) &temp->color; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
268 |
1029
ce08bc4cb97b
ngx_strn2cmp() > ngx_memn2cmp()
Igor Sysoev <igor@sysoev.ru>
parents:
1026
diff
changeset
|
269 if (ngx_memn2cmp(lzn->data, lznt->data, lzn->len, lznt->len) < 0) { |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
270 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
271 if (temp->left == sentinel) { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
272 temp->left = node; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
273 break; |
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 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
276 temp = temp->left; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
277 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
278 } else { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
279 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
280 if (temp->right == sentinel) { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
281 temp->right = node; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
282 break; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
283 } |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
284 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
285 temp = temp->right; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
286 } |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
287 } |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
288 } |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
289 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
290 node->parent = temp; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
291 node->left = sentinel; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
292 node->right = sentinel; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
293 ngx_rbt_red(node); |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
294 } |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
295 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
296 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
297 static void |
980 | 298 ngx_http_limit_zone_cleanup(void *data) |
299 { | |
300 ngx_http_limit_zone_cleanup_t *lzcln = data; | |
301 | |
302 ngx_slab_pool_t *shpool; | |
303 ngx_rbtree_node_t *node; | |
987 | 304 ngx_http_limit_zone_ctx_t *ctx; |
980 | 305 ngx_http_limit_zone_node_t *lz; |
306 | |
987 | 307 ctx = lzcln->shm_zone->data; |
980 | 308 shpool = (ngx_slab_pool_t *) lzcln->shm_zone->shm.addr; |
309 node = lzcln->node; | |
981 | 310 lz = (ngx_http_limit_zone_node_t *) &node->color; |
980 | 311 |
312 ngx_shmtx_lock(&shpool->mutex); | |
313 | |
314 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, lzcln->shm_zone->shm.log, 0, | |
315 "limit zone cleanup: %08XD %d", node->key, lz->conn); | |
316 | |
317 lz->conn--; | |
318 | |
319 if (lz->conn == 0) { | |
987 | 320 ngx_rbtree_delete(ctx->rbtree, node); |
980 | 321 ngx_slab_free_locked(shpool, node); |
322 } | |
323 | |
324 ngx_shmtx_unlock(&shpool->mutex); | |
325 } | |
326 | |
327 | |
328 static ngx_int_t | |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
329 ngx_http_limit_zone_init_zone(ngx_shm_zone_t *shm_zone, void *data) |
980 | 330 { |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
331 ngx_http_limit_zone_ctx_t *octx = data; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
332 |
987 | 333 ngx_slab_pool_t *shpool; |
334 ngx_rbtree_node_t *sentinel; | |
335 ngx_http_limit_zone_ctx_t *ctx; | |
980 | 336 |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
337 ctx = shm_zone->data; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
338 |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
339 if (octx) { |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
340 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
|
341 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, |
1001 | 342 "limit_zone \"%V\" uses the \"%V\" variable " |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
343 "while previously it used the \"%V\" variable", |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
344 &shm_zone->name, &ctx->var, &octx->var); |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
345 return NGX_ERROR; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
346 } |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
347 |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
348 ctx->rbtree = octx->rbtree; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
349 |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
350 return NGX_OK; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
351 } |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
352 |
980 | 353 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; |
354 | |
987 | 355 ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t)); |
356 if (ctx->rbtree == NULL) { | |
980 | 357 return NGX_ERROR; |
358 } | |
359 | |
360 sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t)); | |
361 if (sentinel == NULL) { | |
362 return NGX_ERROR; | |
363 } | |
364 | |
365 ngx_rbtree_sentinel_init(sentinel); | |
366 | |
987 | 367 ctx->rbtree->root = sentinel; |
368 ctx->rbtree->sentinel = sentinel; | |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
369 ctx->rbtree->insert = ngx_http_limit_zone_rbtree_insert_value; |
980 | 370 |
371 return NGX_OK; | |
372 } | |
373 | |
374 | |
375 static void * | |
376 ngx_http_limit_zone_create_conf(ngx_conf_t *cf) | |
377 { | |
378 ngx_http_limit_zone_conf_t *conf; | |
379 | |
380 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_conf_t)); | |
381 if (conf == NULL) { | |
382 return NGX_CONF_ERROR; | |
383 } | |
384 | |
385 /* | |
386 * set by ngx_pcalloc(): | |
387 * | |
388 * conf->shm_zone = NULL; | |
389 * conf->conn = 0; | |
390 */ | |
391 | |
392 return conf; | |
393 } | |
394 | |
395 | |
396 static char * | |
397 ngx_http_limit_zone_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
398 { | |
399 ngx_http_limit_zone_conf_t *prev = parent; | |
400 ngx_http_limit_zone_conf_t *conf = child; | |
401 | |
402 if (conf->shm_zone == NULL) { | |
403 *conf = *prev; | |
404 } | |
405 | |
406 return NGX_CONF_OK; | |
407 } | |
408 | |
409 | |
410 static char * | |
411 ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
412 { | |
987 | 413 ssize_t n; |
414 ngx_str_t *value; | |
415 ngx_shm_zone_t *shm_zone; | |
416 ngx_http_limit_zone_ctx_t *ctx; | |
980 | 417 |
418 value = cf->args->elts; | |
419 | |
987 | 420 if (value[2].data[0] != '$') { |
421 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
422 "invalid variable name \"%V\"", &value[2]); | |
423 return NGX_CONF_ERROR; | |
424 } | |
425 | |
426 value[2].len--; | |
427 value[2].data++; | |
428 | |
429 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_ctx_t)); | |
430 if (ctx == NULL) { | |
431 return NGX_CONF_ERROR; | |
432 } | |
433 | |
434 ctx->index = ngx_http_get_variable_index(cf, &value[2]); | |
435 if (ctx->index == NGX_ERROR) { | |
436 return NGX_CONF_ERROR; | |
437 } | |
438 | |
439 ctx->var = value[2]; | |
440 | |
441 n = ngx_parse_size(&value[3]); | |
980 | 442 |
443 if (n == NGX_ERROR) { | |
444 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
987 | 445 "invalid size of limit_zone \"%V\"", &value[3]); |
980 | 446 return NGX_CONF_ERROR; |
447 } | |
448 | |
449 if (n < (ngx_int_t) (8 * ngx_pagesize)) { | |
450 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
451 "limit_zone \"%V\" is too small", &value[1]); | |
452 return NGX_CONF_ERROR; | |
453 } | |
454 | |
455 | |
456 shm_zone = ngx_shared_memory_add(cf, &value[1], n, | |
457 &ngx_http_limit_zone_module); | |
458 if (shm_zone == NULL) { | |
459 return NGX_CONF_ERROR; | |
460 } | |
461 | |
987 | 462 if (shm_zone->data) { |
463 ctx = shm_zone->data; | |
464 | |
465 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
466 "limit_zone \"%V\" is already bound to variable \"%V\"", | |
467 &value[1], &ctx->var); | |
468 return NGX_CONF_ERROR; | |
469 } | |
470 | |
980 | 471 shm_zone->init = ngx_http_limit_zone_init_zone; |
987 | 472 shm_zone->data = ctx; |
980 | 473 |
474 return NGX_CONF_OK; | |
475 } | |
476 | |
477 | |
478 static char * | |
479 ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
480 { | |
481 ngx_http_limit_zone_conf_t *lzcf = conf; | |
482 | |
483 ngx_int_t n; | |
484 ngx_str_t *value; | |
485 | |
486 value = cf->args->elts; | |
487 | |
987 | 488 lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0, |
489 &ngx_http_limit_zone_module); | |
490 if (lzcf->shm_zone == NULL) { | |
980 | 491 return NGX_CONF_ERROR; |
492 } | |
493 | |
987 | 494 n = ngx_atoi(value[2].data, value[2].len); |
495 if (n <= 0) { | |
980 | 496 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
987 | 497 "invalid number of connections \"%V\"", &value[2]); |
980 | 498 return NGX_CONF_ERROR; |
499 } | |
500 | |
1011
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
501 if (n > 65535) { |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
502 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
503 "connection limit must be less 65536"); |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
504 return NGX_CONF_ERROR; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
505 } |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
506 |
980 | 507 lzcf->conn = n; |
508 | |
509 return NGX_CONF_OK; | |
510 } | |
511 | |
512 | |
513 static ngx_int_t | |
514 ngx_http_limit_zone_init(ngx_conf_t *cf) | |
515 { | |
516 ngx_http_handler_pt *h; | |
517 ngx_http_core_main_conf_t *cmcf; | |
518 | |
519 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
520 | |
521 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers); | |
522 if (h == NULL) { | |
523 return NGX_ERROR; | |
524 } | |
525 | |
526 *h = ngx_http_limit_zone_handler; | |
527 | |
528 return NGX_OK; | |
529 } |