Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_limit_zone_module.c @ 990:bc232bbdddf9
undo r979
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 08 Jan 2007 18:20:03 +0000 |
parents | 14e68f471d02 |
children | 1b9a4d92173f |
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; | |
107 ngx_slab_pool_t *shpool; | |
108 ngx_rbtree_node_t *node, *sentinel; | |
109 ngx_pool_cleanup_t *cln; | |
110 ngx_http_variable_value_t *vv; | |
987 | 111 ngx_http_limit_zone_ctx_t *ctx; |
980 | 112 ngx_http_limit_zone_node_t *lz; |
113 ngx_http_limit_zone_conf_t *lzcf; | |
114 ngx_http_limit_zone_cleanup_t *lzcln; | |
115 | |
984
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
116 if (r->limit_zone_set) { |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
117 return NGX_DECLINED; |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
118 } |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
119 |
980 | 120 lzcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_zone_module); |
121 | |
122 if (lzcf->shm_zone == NULL) { | |
123 return NGX_DECLINED; | |
124 } | |
125 | |
987 | 126 ctx = lzcf->shm_zone->data; |
127 | |
128 vv = ngx_http_get_indexed_variable(r, ctx->index); | |
980 | 129 |
130 if (vv == NULL || vv->not_found) { | |
131 return NGX_DECLINED; | |
132 } | |
133 | |
984
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
134 r->limit_zone_set = 1; |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
135 |
980 | 136 len = vv->len; |
137 | |
138 hash = ngx_crc32_short(vv->data, len); | |
139 | |
140 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_limit_zone_cleanup_t)); | |
141 if (cln == NULL) { | |
142 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
143 } | |
144 | |
145 shpool = (ngx_slab_pool_t *) lzcf->shm_zone->shm.addr; | |
146 | |
147 ngx_shmtx_lock(&shpool->mutex); | |
148 | |
987 | 149 node = ctx->rbtree->root; |
150 sentinel = ctx->rbtree->sentinel; | |
980 | 151 |
152 while (node != sentinel) { | |
153 | |
154 if (hash < node->key) { | |
155 node = node->left; | |
156 continue; | |
157 } | |
158 | |
159 if (hash > node->key) { | |
160 node = node->right; | |
161 continue; | |
162 } | |
163 | |
164 if (hash == node->key ){ | |
981 | 165 lz = (ngx_http_limit_zone_node_t *) &node->color; |
980 | 166 |
167 if (len == (size_t) lz->len | |
168 && ngx_strncmp(lz->data, vv->data, len) == 0) | |
169 { | |
981 | 170 if ((ngx_uint_t) lz->conn < lzcf->conn) { |
980 | 171 lz->conn++; |
172 goto done; | |
173 } | |
174 | |
175 ngx_shmtx_unlock(&shpool->mutex); | |
176 | |
177 return NGX_HTTP_SERVICE_UNAVAILABLE; | |
178 } | |
179 } | |
180 } | |
181 | |
981 | 182 n = offsetof(ngx_rbtree_node_t, color) |
980 | 183 + offsetof(ngx_http_limit_zone_node_t, data) |
184 + len; | |
185 | |
186 node = ngx_slab_alloc_locked(shpool, n); | |
187 if (node == NULL) { | |
188 ngx_shmtx_unlock(&shpool->mutex); | |
189 return NGX_HTTP_SERVICE_UNAVAILABLE; | |
190 } | |
191 | |
981 | 192 lz = (ngx_http_limit_zone_node_t *) &node->color; |
980 | 193 |
194 node->key = hash; | |
981 | 195 lz->len = (u_char) len; |
980 | 196 lz->conn = 1; |
197 ngx_memcpy(lz->data, vv->data, len); | |
198 | |
987 | 199 ngx_rbtree_insert(ctx->rbtree, node); |
980 | 200 |
201 done: | |
202 | |
203 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
204 "limit zone: %08XD %d", node->key, lz->conn); | |
205 | |
206 ngx_shmtx_unlock(&shpool->mutex); | |
207 | |
208 cln->handler = ngx_http_limit_zone_cleanup; | |
209 lzcln = cln->data; | |
210 | |
211 lzcln->shm_zone = lzcf->shm_zone; | |
212 lzcln->node = node; | |
213 | |
214 return NGX_DECLINED; | |
215 } | |
216 | |
217 | |
218 static void | |
219 ngx_http_limit_zone_cleanup(void *data) | |
220 { | |
221 ngx_http_limit_zone_cleanup_t *lzcln = data; | |
222 | |
223 ngx_slab_pool_t *shpool; | |
224 ngx_rbtree_node_t *node; | |
987 | 225 ngx_http_limit_zone_ctx_t *ctx; |
980 | 226 ngx_http_limit_zone_node_t *lz; |
227 | |
987 | 228 ctx = lzcln->shm_zone->data; |
980 | 229 shpool = (ngx_slab_pool_t *) lzcln->shm_zone->shm.addr; |
230 node = lzcln->node; | |
981 | 231 lz = (ngx_http_limit_zone_node_t *) &node->color; |
980 | 232 |
233 ngx_shmtx_lock(&shpool->mutex); | |
234 | |
235 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, lzcln->shm_zone->shm.log, 0, | |
236 "limit zone cleanup: %08XD %d", node->key, lz->conn); | |
237 | |
238 lz->conn--; | |
239 | |
240 if (lz->conn == 0) { | |
987 | 241 ngx_rbtree_delete(ctx->rbtree, node); |
980 | 242 ngx_slab_free_locked(shpool, node); |
243 } | |
244 | |
245 ngx_shmtx_unlock(&shpool->mutex); | |
246 } | |
247 | |
248 | |
249 static ngx_int_t | |
250 ngx_http_limit_zone_init_zone(ngx_shm_zone_t *shm_zone) | |
251 { | |
987 | 252 ngx_slab_pool_t *shpool; |
253 ngx_rbtree_node_t *sentinel; | |
254 ngx_http_limit_zone_ctx_t *ctx; | |
980 | 255 |
256 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; | |
987 | 257 ctx = shm_zone->data; |
980 | 258 |
987 | 259 ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t)); |
260 if (ctx->rbtree == NULL) { | |
980 | 261 return NGX_ERROR; |
262 } | |
263 | |
264 sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t)); | |
265 if (sentinel == NULL) { | |
266 return NGX_ERROR; | |
267 } | |
268 | |
269 ngx_rbtree_sentinel_init(sentinel); | |
270 | |
987 | 271 ctx->rbtree->root = sentinel; |
272 ctx->rbtree->sentinel = sentinel; | |
273 ctx->rbtree->insert = ngx_rbtree_insert_value; | |
980 | 274 |
275 return NGX_OK; | |
276 } | |
277 | |
278 | |
279 static void * | |
280 ngx_http_limit_zone_create_conf(ngx_conf_t *cf) | |
281 { | |
282 ngx_http_limit_zone_conf_t *conf; | |
283 | |
284 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_conf_t)); | |
285 if (conf == NULL) { | |
286 return NGX_CONF_ERROR; | |
287 } | |
288 | |
289 /* | |
290 * set by ngx_pcalloc(): | |
291 * | |
292 * conf->shm_zone = NULL; | |
293 * conf->conn = 0; | |
294 */ | |
295 | |
296 return conf; | |
297 } | |
298 | |
299 | |
300 static char * | |
301 ngx_http_limit_zone_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
302 { | |
303 ngx_http_limit_zone_conf_t *prev = parent; | |
304 ngx_http_limit_zone_conf_t *conf = child; | |
305 | |
306 if (conf->shm_zone == NULL) { | |
307 *conf = *prev; | |
308 } | |
309 | |
310 return NGX_CONF_OK; | |
311 } | |
312 | |
313 | |
314 static char * | |
315 ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
316 { | |
987 | 317 ssize_t n; |
318 ngx_str_t *value; | |
319 ngx_shm_zone_t *shm_zone; | |
320 ngx_http_limit_zone_ctx_t *ctx; | |
980 | 321 |
322 value = cf->args->elts; | |
323 | |
987 | 324 if (value[2].data[0] != '$') { |
325 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
326 "invalid variable name \"%V\"", &value[2]); | |
327 return NGX_CONF_ERROR; | |
328 } | |
329 | |
330 value[2].len--; | |
331 value[2].data++; | |
332 | |
333 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_ctx_t)); | |
334 if (ctx == NULL) { | |
335 return NGX_CONF_ERROR; | |
336 } | |
337 | |
338 ctx->index = ngx_http_get_variable_index(cf, &value[2]); | |
339 if (ctx->index == NGX_ERROR) { | |
340 return NGX_CONF_ERROR; | |
341 } | |
342 | |
343 ctx->var = value[2]; | |
344 | |
345 n = ngx_parse_size(&value[3]); | |
980 | 346 |
347 if (n == NGX_ERROR) { | |
348 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
987 | 349 "invalid size of limit_zone \"%V\"", &value[3]); |
980 | 350 return NGX_CONF_ERROR; |
351 } | |
352 | |
353 if (n < (ngx_int_t) (8 * ngx_pagesize)) { | |
354 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
355 "limit_zone \"%V\" is too small", &value[1]); | |
356 return NGX_CONF_ERROR; | |
357 } | |
358 | |
359 | |
360 shm_zone = ngx_shared_memory_add(cf, &value[1], n, | |
361 &ngx_http_limit_zone_module); | |
362 if (shm_zone == NULL) { | |
363 return NGX_CONF_ERROR; | |
364 } | |
365 | |
987 | 366 if (shm_zone->data) { |
367 ctx = shm_zone->data; | |
368 | |
369 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
370 "limit_zone \"%V\" is already bound to variable \"%V\"", | |
371 &value[1], &ctx->var); | |
372 return NGX_CONF_ERROR; | |
373 } | |
374 | |
980 | 375 shm_zone->init = ngx_http_limit_zone_init_zone; |
987 | 376 shm_zone->data = ctx; |
980 | 377 |
378 return NGX_CONF_OK; | |
379 } | |
380 | |
381 | |
382 static char * | |
383 ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
384 { | |
385 ngx_http_limit_zone_conf_t *lzcf = conf; | |
386 | |
387 ngx_int_t n; | |
388 ngx_str_t *value; | |
389 | |
390 value = cf->args->elts; | |
391 | |
987 | 392 lzcf->shm_zone = ngx_shared_memory_add(cf, &value[1], 0, |
393 &ngx_http_limit_zone_module); | |
394 if (lzcf->shm_zone == NULL) { | |
980 | 395 return NGX_CONF_ERROR; |
396 } | |
397 | |
987 | 398 n = ngx_atoi(value[2].data, value[2].len); |
399 if (n <= 0) { | |
980 | 400 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
987 | 401 "invalid number of connections \"%V\"", &value[2]); |
980 | 402 return NGX_CONF_ERROR; |
403 } | |
404 | |
405 lzcf->conn = n; | |
406 | |
407 return NGX_CONF_OK; | |
408 } | |
409 | |
410 | |
411 static ngx_int_t | |
412 ngx_http_limit_zone_init(ngx_conf_t *cf) | |
413 { | |
414 ngx_http_handler_pt *h; | |
415 ngx_http_core_main_conf_t *cmcf; | |
416 | |
417 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
418 | |
419 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers); | |
420 if (h == NULL) { | |
421 return NGX_ERROR; | |
422 } | |
423 | |
424 *h = ngx_http_limit_zone_handler; | |
425 | |
426 return NGX_OK; | |
427 } |