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