Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_limit_zone_module.c @ 984:dd128232e6ba
count connection once per request
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Sun, 07 Jan 2007 17:47:17 +0000 |
parents | 6fe76f377a62 |
children | 14e68f471d02 |
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 { | |
27 ngx_shm_zone_t *shm_zone; | |
28 ngx_int_t index; | |
29 ngx_uint_t conn; | |
30 } ngx_http_limit_zone_conf_t; | |
31 | |
32 | |
33 static void ngx_http_limit_zone_cleanup(void *data); | |
34 | |
35 static void *ngx_http_limit_zone_create_conf(ngx_conf_t *cf); | |
36 static char *ngx_http_limit_zone_merge_conf(ngx_conf_t *cf, void *parent, | |
37 void *child); | |
38 static char *ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, | |
39 void *conf); | |
40 static char *ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, | |
41 void *conf); | |
42 static ngx_int_t ngx_http_limit_zone_init(ngx_conf_t *cf); | |
43 | |
44 | |
45 static ngx_command_t ngx_http_limit_zone_commands[] = { | |
46 | |
47 { ngx_string("limit_zone"), | |
48 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2, | |
49 ngx_http_limit_zone, | |
50 0, | |
51 0, | |
52 NULL }, | |
53 | |
54 { ngx_string("limit_conn"), | |
55 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE3, | |
56 ngx_http_limit_conn, | |
57 NGX_HTTP_LOC_CONF_OFFSET, | |
58 0, | |
59 NULL }, | |
60 | |
61 ngx_null_command | |
62 }; | |
63 | |
64 | |
65 static ngx_http_module_t ngx_http_limit_zone_module_ctx = { | |
66 NULL, /* preconfiguration */ | |
67 ngx_http_limit_zone_init, /* postconfiguration */ | |
68 | |
69 NULL, /* create main configuration */ | |
70 NULL, /* init main configuration */ | |
71 | |
72 NULL, /* create server configuration */ | |
73 NULL, /* merge server configuration */ | |
74 | |
75 ngx_http_limit_zone_create_conf, /* create location configration */ | |
76 ngx_http_limit_zone_merge_conf /* merge location configration */ | |
77 }; | |
78 | |
79 | |
80 ngx_module_t ngx_http_limit_zone_module = { | |
81 NGX_MODULE_V1, | |
82 &ngx_http_limit_zone_module_ctx, /* module context */ | |
83 ngx_http_limit_zone_commands, /* module directives */ | |
84 NGX_HTTP_MODULE, /* module type */ | |
85 NULL, /* init master */ | |
86 NULL, /* init module */ | |
87 NULL, /* init process */ | |
88 NULL, /* init thread */ | |
89 NULL, /* exit thread */ | |
90 NULL, /* exit process */ | |
91 NULL, /* exit master */ | |
92 NGX_MODULE_V1_PADDING | |
93 }; | |
94 | |
95 | |
96 static ngx_int_t | |
97 ngx_http_limit_zone_handler(ngx_http_request_t *r) | |
98 { | |
99 size_t len, n; | |
100 uint32_t hash; | |
101 ngx_rbtree_t *rbtree; | |
102 ngx_slab_pool_t *shpool; | |
103 ngx_rbtree_node_t *node, *sentinel; | |
104 ngx_pool_cleanup_t *cln; | |
105 ngx_http_variable_value_t *vv; | |
106 ngx_http_limit_zone_node_t *lz; | |
107 ngx_http_limit_zone_conf_t *lzcf; | |
108 ngx_http_limit_zone_cleanup_t *lzcln; | |
109 | |
984
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
110 if (r->limit_zone_set) { |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
111 return NGX_DECLINED; |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
112 } |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
113 |
980 | 114 lzcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_zone_module); |
115 | |
116 if (lzcf->shm_zone == NULL) { | |
117 return NGX_DECLINED; | |
118 } | |
119 | |
120 vv = ngx_http_get_indexed_variable(r, lzcf->index); | |
121 | |
122 if (vv == NULL || vv->not_found) { | |
123 return NGX_DECLINED; | |
124 } | |
125 | |
984
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
126 r->limit_zone_set = 1; |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
127 |
980 | 128 len = vv->len; |
129 | |
130 hash = ngx_crc32_short(vv->data, len); | |
131 | |
132 cln = ngx_pool_cleanup_add(r->pool, sizeof(ngx_http_limit_zone_cleanup_t)); | |
133 if (cln == NULL) { | |
134 return NGX_HTTP_INTERNAL_SERVER_ERROR; | |
135 } | |
136 | |
137 rbtree = lzcf->shm_zone->data; | |
138 shpool = (ngx_slab_pool_t *) lzcf->shm_zone->shm.addr; | |
139 | |
140 ngx_shmtx_lock(&shpool->mutex); | |
141 | |
142 node = rbtree->root; | |
143 sentinel = rbtree->sentinel; | |
144 | |
145 while (node != sentinel) { | |
146 | |
147 if (hash < node->key) { | |
148 node = node->left; | |
149 continue; | |
150 } | |
151 | |
152 if (hash > node->key) { | |
153 node = node->right; | |
154 continue; | |
155 } | |
156 | |
157 if (hash == node->key ){ | |
981 | 158 lz = (ngx_http_limit_zone_node_t *) &node->color; |
980 | 159 |
160 if (len == (size_t) lz->len | |
161 && ngx_strncmp(lz->data, vv->data, len) == 0) | |
162 { | |
981 | 163 if ((ngx_uint_t) lz->conn < lzcf->conn) { |
980 | 164 lz->conn++; |
165 goto done; | |
166 } | |
167 | |
168 ngx_shmtx_unlock(&shpool->mutex); | |
169 | |
170 return NGX_HTTP_SERVICE_UNAVAILABLE; | |
171 } | |
172 } | |
173 } | |
174 | |
981 | 175 n = offsetof(ngx_rbtree_node_t, color) |
980 | 176 + offsetof(ngx_http_limit_zone_node_t, data) |
177 + len; | |
178 | |
179 node = ngx_slab_alloc_locked(shpool, n); | |
180 if (node == NULL) { | |
181 ngx_shmtx_unlock(&shpool->mutex); | |
182 return NGX_HTTP_SERVICE_UNAVAILABLE; | |
183 } | |
184 | |
981 | 185 lz = (ngx_http_limit_zone_node_t *) &node->color; |
980 | 186 |
187 node->key = hash; | |
981 | 188 lz->len = (u_char) len; |
980 | 189 lz->conn = 1; |
190 ngx_memcpy(lz->data, vv->data, len); | |
191 | |
192 ngx_rbtree_insert(rbtree, node); | |
193 | |
194 done: | |
195 | |
196 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
197 "limit zone: %08XD %d", node->key, lz->conn); | |
198 | |
199 ngx_shmtx_unlock(&shpool->mutex); | |
200 | |
201 cln->handler = ngx_http_limit_zone_cleanup; | |
202 lzcln = cln->data; | |
203 | |
204 lzcln->shm_zone = lzcf->shm_zone; | |
205 lzcln->node = node; | |
206 | |
207 return NGX_DECLINED; | |
208 } | |
209 | |
210 | |
211 static void | |
212 ngx_http_limit_zone_cleanup(void *data) | |
213 { | |
214 ngx_http_limit_zone_cleanup_t *lzcln = data; | |
215 | |
216 ngx_rbtree_t *rbtree; | |
217 ngx_slab_pool_t *shpool; | |
218 ngx_rbtree_node_t *node; | |
219 ngx_http_limit_zone_node_t *lz; | |
220 | |
221 rbtree = lzcln->shm_zone->data; | |
222 shpool = (ngx_slab_pool_t *) lzcln->shm_zone->shm.addr; | |
223 node = lzcln->node; | |
981 | 224 lz = (ngx_http_limit_zone_node_t *) &node->color; |
980 | 225 |
226 ngx_shmtx_lock(&shpool->mutex); | |
227 | |
228 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, lzcln->shm_zone->shm.log, 0, | |
229 "limit zone cleanup: %08XD %d", node->key, lz->conn); | |
230 | |
231 lz->conn--; | |
232 | |
233 if (lz->conn == 0) { | |
234 ngx_rbtree_delete(rbtree, node); | |
235 ngx_slab_free_locked(shpool, node); | |
236 } | |
237 | |
238 ngx_shmtx_unlock(&shpool->mutex); | |
239 } | |
240 | |
241 | |
242 static ngx_int_t | |
243 ngx_http_limit_zone_init_zone(ngx_shm_zone_t *shm_zone) | |
244 { | |
245 ngx_rbtree_t *rbtree; | |
246 ngx_slab_pool_t *shpool; | |
247 ngx_rbtree_node_t *sentinel; | |
248 | |
249 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; | |
250 | |
251 rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t)); | |
252 if (rbtree == NULL) { | |
253 return NGX_ERROR; | |
254 } | |
255 | |
256 sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t)); | |
257 if (sentinel == NULL) { | |
258 return NGX_ERROR; | |
259 } | |
260 | |
261 ngx_rbtree_sentinel_init(sentinel); | |
262 | |
263 rbtree->root = sentinel; | |
264 rbtree->sentinel = sentinel; | |
265 rbtree->insert = ngx_rbtree_insert_value; | |
266 | |
267 shm_zone->data = rbtree; | |
268 | |
269 return NGX_OK; | |
270 } | |
271 | |
272 | |
273 static void * | |
274 ngx_http_limit_zone_create_conf(ngx_conf_t *cf) | |
275 { | |
276 ngx_http_limit_zone_conf_t *conf; | |
277 | |
278 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_zone_conf_t)); | |
279 if (conf == NULL) { | |
280 return NGX_CONF_ERROR; | |
281 } | |
282 | |
283 /* | |
284 * set by ngx_pcalloc(): | |
285 * | |
286 * conf->shm_zone = NULL; | |
287 * conf->index = 0; | |
288 * conf->conn = 0; | |
289 */ | |
290 | |
291 return conf; | |
292 } | |
293 | |
294 | |
295 static char * | |
296 ngx_http_limit_zone_merge_conf(ngx_conf_t *cf, void *parent, void *child) | |
297 { | |
298 ngx_http_limit_zone_conf_t *prev = parent; | |
299 ngx_http_limit_zone_conf_t *conf = child; | |
300 | |
301 if (conf->shm_zone == NULL) { | |
302 *conf = *prev; | |
303 } | |
304 | |
305 return NGX_CONF_OK; | |
306 } | |
307 | |
308 | |
309 static char * | |
310 ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
311 { | |
312 ssize_t n; | |
313 ngx_str_t *value; | |
314 ngx_shm_zone_t *shm_zone; | |
315 | |
316 value = cf->args->elts; | |
317 | |
318 n = ngx_parse_size(&value[2]); | |
319 | |
320 if (n == NGX_ERROR) { | |
321 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
322 "invalid size of limit_zone \"%V\"", &value[2]); | |
323 return NGX_CONF_ERROR; | |
324 } | |
325 | |
326 if (n < (ngx_int_t) (8 * ngx_pagesize)) { | |
327 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
328 "limit_zone \"%V\" is too small", &value[1]); | |
329 return NGX_CONF_ERROR; | |
330 } | |
331 | |
332 | |
333 shm_zone = ngx_shared_memory_add(cf, &value[1], n, | |
334 &ngx_http_limit_zone_module); | |
335 if (shm_zone == NULL) { | |
336 return NGX_CONF_ERROR; | |
337 } | |
338 | |
339 shm_zone->init = ngx_http_limit_zone_init_zone; | |
340 | |
341 return NGX_CONF_OK; | |
342 } | |
343 | |
344 | |
345 static char * | |
346 ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
347 { | |
348 ngx_http_limit_zone_conf_t *lzcf = conf; | |
349 | |
350 ngx_int_t n; | |
351 ngx_str_t *value; | |
352 | |
353 value = cf->args->elts; | |
354 | |
355 n = ngx_atoi(value[1].data, value[1].len); | |
356 if (n <= 0) { | |
357 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
358 "invalid number of connections"); | |
359 return NGX_CONF_ERROR; | |
360 } | |
361 | |
362 if (value[2].data[0] != '$') { | |
363 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
364 "invalid variable name \"%V\"", &value[2]); | |
365 return NGX_CONF_ERROR; | |
366 } | |
367 | |
368 value[2].len--; | |
369 value[2].data++; | |
370 | |
371 lzcf->index = ngx_http_get_variable_index(cf, &value[2]); | |
372 if (lzcf->index == NGX_ERROR) { | |
373 return NGX_CONF_ERROR; | |
374 } | |
375 | |
376 lzcf->shm_zone = ngx_shared_memory_add(cf, &value[3], 0, | |
377 &ngx_http_limit_zone_module); | |
378 if (lzcf->shm_zone == NULL) { | |
379 return NGX_CONF_ERROR; | |
380 } | |
381 | |
382 lzcf->conn = n; | |
383 | |
384 return NGX_CONF_OK; | |
385 } | |
386 | |
387 | |
388 static ngx_int_t | |
389 ngx_http_limit_zone_init(ngx_conf_t *cf) | |
390 { | |
391 ngx_http_handler_pt *h; | |
392 ngx_http_core_main_conf_t *cmcf; | |
393 | |
394 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
395 | |
396 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers); | |
397 if (h == NULL) { | |
398 return NGX_ERROR; | |
399 } | |
400 | |
401 *h = ngx_http_limit_zone_handler; | |
402 | |
403 return NGX_OK; | |
404 } |