Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_limit_conn_module.c @ 5306:43900b822890
Perl: fixed syntax usage for C preprocessor directives.
As per perlxs, C preprocessor directives should be at the first
non-whitespace of a line to avoid interpreting them as comments.
#if and #endif are moved so that there are no blank lines before them
to retain them as part of the function body.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Mon, 29 Jul 2013 17:30:01 +0400 |
parents | 00e4459739ed |
children | ad9116427acf |
rev | line source |
---|---|
980 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
980 | 5 */ |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 | |
12 | |
13 typedef struct { | |
981 | 14 u_char color; |
15 u_char len; | |
980 | 16 u_short conn; |
17 u_char data[1]; | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
18 } ngx_http_limit_conn_node_t; |
980 | 19 |
20 | |
21 typedef struct { | |
22 ngx_shm_zone_t *shm_zone; | |
23 ngx_rbtree_node_t *node; | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
24 } ngx_http_limit_conn_cleanup_t; |
980 | 25 |
26 | |
27 typedef struct { | |
987 | 28 ngx_rbtree_t *rbtree; |
29 ngx_int_t index; | |
30 ngx_str_t var; | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
31 } ngx_http_limit_conn_ctx_t; |
987 | 32 |
33 | |
34 typedef struct { | |
980 | 35 ngx_shm_zone_t *shm_zone; |
36 ngx_uint_t conn; | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
37 } ngx_http_limit_conn_limit_t; |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
38 |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
39 |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
40 typedef struct { |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
41 ngx_array_t limits; |
3186 | 42 ngx_uint_t log_level; |
5117
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
43 ngx_uint_t status_code; |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
44 } ngx_http_limit_conn_conf_t; |
980 | 45 |
46 | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
47 static ngx_rbtree_node_t *ngx_http_limit_conn_lookup(ngx_rbtree_t *rbtree, |
4270
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
48 ngx_http_variable_value_t *vv, uint32_t hash); |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
49 static void ngx_http_limit_conn_cleanup(void *data); |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
50 static ngx_inline void ngx_http_limit_conn_cleanup_all(ngx_pool_t *pool); |
980 | 51 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
52 static void *ngx_http_limit_conn_create_conf(ngx_conf_t *cf); |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
53 static char *ngx_http_limit_conn_merge_conf(ngx_conf_t *cf, void *parent, |
980 | 54 void *child); |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
55 static char *ngx_http_limit_conn_zone(ngx_conf_t *cf, ngx_command_t *cmd, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
56 void *conf); |
980 | 57 static char *ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, |
58 void *conf); | |
59 static char *ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, | |
60 void *conf); | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
61 static ngx_int_t ngx_http_limit_conn_init(ngx_conf_t *cf); |
980 | 62 |
63 | |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
64 static ngx_conf_deprecated_t ngx_conf_deprecated_limit_zone = { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
65 ngx_conf_deprecated, "limit_zone", "limit_conn_zone" |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
66 }; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
67 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
68 |
3186 | 69 static ngx_conf_enum_t ngx_http_limit_conn_log_levels[] = { |
70 { ngx_string("info"), NGX_LOG_INFO }, | |
71 { ngx_string("notice"), NGX_LOG_NOTICE }, | |
72 { ngx_string("warn"), NGX_LOG_WARN }, | |
73 { ngx_string("error"), NGX_LOG_ERR }, | |
74 { ngx_null_string, 0 } | |
75 }; | |
76 | |
77 | |
5117
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
78 static ngx_conf_num_bounds_t ngx_http_limit_conn_status_bounds = { |
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
79 ngx_conf_check_num_bounds, 400, 599 |
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
80 }; |
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
81 |
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
82 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
83 static ngx_command_t ngx_http_limit_conn_commands[] = { |
980 | 84 |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
85 { ngx_string("limit_conn_zone"), |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
86 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE2, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
87 ngx_http_limit_conn_zone, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
88 0, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
89 0, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
90 NULL }, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
91 |
980 | 92 { ngx_string("limit_zone"), |
987 | 93 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3, |
980 | 94 ngx_http_limit_zone, |
95 0, | |
96 0, | |
97 NULL }, | |
98 | |
99 { ngx_string("limit_conn"), | |
987 | 100 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE2, |
980 | 101 ngx_http_limit_conn, |
102 NGX_HTTP_LOC_CONF_OFFSET, | |
103 0, | |
104 NULL }, | |
105 | |
3186 | 106 { ngx_string("limit_conn_log_level"), |
107 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, | |
108 ngx_conf_set_enum_slot, | |
109 NGX_HTTP_LOC_CONF_OFFSET, | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
110 offsetof(ngx_http_limit_conn_conf_t, log_level), |
3186 | 111 &ngx_http_limit_conn_log_levels }, |
112 | |
5117
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
113 { ngx_string("limit_conn_status"), |
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
114 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, |
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
115 ngx_conf_set_num_slot, |
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
116 NGX_HTTP_LOC_CONF_OFFSET, |
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
117 offsetof(ngx_http_limit_conn_conf_t, status_code), |
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
118 &ngx_http_limit_conn_status_bounds }, |
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
119 |
980 | 120 ngx_null_command |
121 }; | |
122 | |
123 | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
124 static ngx_http_module_t ngx_http_limit_conn_module_ctx = { |
980 | 125 NULL, /* preconfiguration */ |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
126 ngx_http_limit_conn_init, /* postconfiguration */ |
980 | 127 |
128 NULL, /* create main configuration */ | |
129 NULL, /* init main configuration */ | |
130 | |
131 NULL, /* create server configuration */ | |
132 NULL, /* merge server configuration */ | |
133 | |
4499
778ef9c3fd2d
Fixed spelling in single-line comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4497
diff
changeset
|
134 ngx_http_limit_conn_create_conf, /* create location configuration */ |
778ef9c3fd2d
Fixed spelling in single-line comments.
Ruslan Ermilov <ru@nginx.com>
parents:
4497
diff
changeset
|
135 ngx_http_limit_conn_merge_conf /* merge location configuration */ |
980 | 136 }; |
137 | |
138 | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
139 ngx_module_t ngx_http_limit_conn_module = { |
980 | 140 NGX_MODULE_V1, |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
141 &ngx_http_limit_conn_module_ctx, /* module context */ |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
142 ngx_http_limit_conn_commands, /* module directives */ |
980 | 143 NGX_HTTP_MODULE, /* module type */ |
144 NULL, /* init master */ | |
145 NULL, /* init module */ | |
146 NULL, /* init process */ | |
147 NULL, /* init thread */ | |
148 NULL, /* exit thread */ | |
149 NULL, /* exit process */ | |
150 NULL, /* exit master */ | |
151 NGX_MODULE_V1_PADDING | |
152 }; | |
153 | |
154 | |
155 static ngx_int_t | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
156 ngx_http_limit_conn_handler(ngx_http_request_t *r) |
980 | 157 { |
158 size_t len, n; | |
159 uint32_t hash; | |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
160 ngx_uint_t i; |
980 | 161 ngx_slab_pool_t *shpool; |
4270
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
162 ngx_rbtree_node_t *node; |
980 | 163 ngx_pool_cleanup_t *cln; |
164 ngx_http_variable_value_t *vv; | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
165 ngx_http_limit_conn_ctx_t *ctx; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
166 ngx_http_limit_conn_node_t *lc; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
167 ngx_http_limit_conn_conf_t *lccf; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
168 ngx_http_limit_conn_limit_t *limits; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
169 ngx_http_limit_conn_cleanup_t *lccln; |
980 | 170 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
171 if (r->main->limit_conn_set) { |
984
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
172 return NGX_DECLINED; |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
173 } |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
174 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
175 lccf = ngx_http_get_module_loc_conf(r, ngx_http_limit_conn_module); |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
176 limits = lccf->limits.elts; |
980 | 177 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
178 for (i = 0; i < lccf->limits.nelts; i++) { |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
179 ctx = limits[i].shm_zone->data; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
180 |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
181 vv = ngx_http_get_indexed_variable(r, ctx->index); |
980 | 182 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
183 if (vv == NULL || vv->not_found) { |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
184 continue; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
185 } |
980 | 186 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
187 len = vv->len; |
980 | 188 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
189 if (len == 0) { |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
190 continue; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
191 } |
980 | 192 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
193 if (len > 255) { |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
194 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
195 "the value of the \"%V\" variable " |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
196 "is more than 255 bytes: \"%v\"", |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
197 &ctx->var, vv); |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
198 continue; |
980 | 199 } |
200 | |
4458
2d4ff8bd5649
Limit conn: returned to the old behavior of using the first actual limit on
Valentin Bartenev <vbart@nginx.com>
parents:
4412
diff
changeset
|
201 r->main->limit_conn_set = 1; |
2d4ff8bd5649
Limit conn: returned to the old behavior of using the first actual limit on
Valentin Bartenev <vbart@nginx.com>
parents:
4412
diff
changeset
|
202 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
203 hash = ngx_crc32_short(vv->data, len); |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
204 |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
205 shpool = (ngx_slab_pool_t *) limits[i].shm_zone->shm.addr; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
206 |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
207 ngx_shmtx_lock(&shpool->mutex); |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
208 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
209 node = ngx_http_limit_conn_lookup(ctx->rbtree, vv, hash); |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
210 |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
211 if (node == NULL) { |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
212 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
213 n = offsetof(ngx_rbtree_node_t, color) |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
214 + offsetof(ngx_http_limit_conn_node_t, data) |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
215 + len; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
216 |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
217 node = ngx_slab_alloc_locked(shpool, n); |
980 | 218 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
219 if (node == NULL) { |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
220 ngx_shmtx_unlock(&shpool->mutex); |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
221 ngx_http_limit_conn_cleanup_all(r->pool); |
5117
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
222 return lccf->status_code; |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
223 } |
2373 | 224 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
225 lc = (ngx_http_limit_conn_node_t *) &node->color; |
1012
11ffb8e4753f
stop rbtree search early if equal hash was found
Igor Sysoev <igor@sysoev.ru>
parents:
1011
diff
changeset
|
226 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
227 node->key = hash; |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
228 lc->len = (u_char) len; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
229 lc->conn = 1; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
230 ngx_memcpy(lc->data, vv->data, len); |
1012
11ffb8e4753f
stop rbtree search early if equal hash was found
Igor Sysoev <igor@sysoev.ru>
parents:
1011
diff
changeset
|
231 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
232 ngx_rbtree_insert(ctx->rbtree, node); |
1012
11ffb8e4753f
stop rbtree search early if equal hash was found
Igor Sysoev <igor@sysoev.ru>
parents:
1011
diff
changeset
|
233 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
234 } else { |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
235 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
236 lc = (ngx_http_limit_conn_node_t *) &node->color; |
980 | 237 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
238 if ((ngx_uint_t) lc->conn >= limits[i].conn) { |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
239 |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
240 ngx_shmtx_unlock(&shpool->mutex); |
980 | 241 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
242 ngx_log_error(lccf->log_level, r->connection->log, 0, |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
243 "limiting connections by zone \"%V\"", |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
244 &limits[i].shm_zone->shm.name); |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
245 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
246 ngx_http_limit_conn_cleanup_all(r->pool); |
5117
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
247 return lccf->status_code; |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
248 } |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
249 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
250 lc->conn++; |
4270
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
251 } |
980 | 252 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
253 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
4756
daa60662b7b5
Fixed debugging messages to account that limit_zone was renamed to limit_conn.
Ruslan Ermilov <ru@nginx.com>
parents:
4499
diff
changeset
|
254 "limit conn: %08XD %d", node->key, lc->conn); |
980 | 255 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
256 ngx_shmtx_unlock(&shpool->mutex); |
980 | 257 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
258 cln = ngx_pool_cleanup_add(r->pool, |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
259 sizeof(ngx_http_limit_conn_cleanup_t)); |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
260 if (cln == NULL) { |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
261 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
262 } |
980 | 263 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
264 cln->handler = ngx_http_limit_conn_cleanup; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
265 lccln = cln->data; |
980 | 266 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
267 lccln->shm_zone = limits[i].shm_zone; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
268 lccln->node = node; |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
269 } |
980 | 270 |
271 return NGX_DECLINED; | |
272 } | |
273 | |
274 | |
275 static void | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
276 ngx_http_limit_conn_rbtree_insert_value(ngx_rbtree_node_t *temp, |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
277 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
|
278 { |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
279 ngx_rbtree_node_t **p; |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
280 ngx_http_limit_conn_node_t *lcn, *lcnt; |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
281 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
282 for ( ;; ) { |
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 if (node->key < temp->key) { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
285 |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
286 p = &temp->left; |
1026
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 } else if (node->key > temp->key) { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
289 |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
290 p = &temp->right; |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
291 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
292 } else { /* node->key == temp->key */ |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
293 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
294 lcn = (ngx_http_limit_conn_node_t *) &node->color; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
295 lcnt = (ngx_http_limit_conn_node_t *) &temp->color; |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
296 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
297 p = (ngx_memn2cmp(lcn->data, lcnt->data, lcn->len, lcnt->len) < 0) |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
298 ? &temp->left : &temp->right; |
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
299 } |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
300 |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
301 if (*p == sentinel) { |
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
302 break; |
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
303 } |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
304 |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
305 temp = *p; |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
306 } |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
307 |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
308 *p = node; |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
309 node->parent = temp; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
310 node->left = sentinel; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
311 node->right = sentinel; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
312 ngx_rbt_red(node); |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
313 } |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
314 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
315 |
4270
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
316 static ngx_rbtree_node_t * |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
317 ngx_http_limit_conn_lookup(ngx_rbtree_t *rbtree, ngx_http_variable_value_t *vv, |
4270
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
318 uint32_t hash) |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
319 { |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
320 ngx_int_t rc; |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
321 ngx_rbtree_node_t *node, *sentinel; |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
322 ngx_http_limit_conn_node_t *lcn; |
4270
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
323 |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
324 node = rbtree->root; |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
325 sentinel = rbtree->sentinel; |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
326 |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
327 while (node != sentinel) { |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
328 |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
329 if (hash < node->key) { |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
330 node = node->left; |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
331 continue; |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
332 } |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
333 |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
334 if (hash > node->key) { |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
335 node = node->right; |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
336 continue; |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
337 } |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
338 |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
339 /* hash == node->key */ |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
340 |
4497
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4458
diff
changeset
|
341 lcn = (ngx_http_limit_conn_node_t *) &node->color; |
4270
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
342 |
4497
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4458
diff
changeset
|
343 rc = ngx_memn2cmp(vv->data, lcn->data, |
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4458
diff
changeset
|
344 (size_t) vv->len, (size_t) lcn->len); |
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4458
diff
changeset
|
345 if (rc == 0) { |
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4458
diff
changeset
|
346 return node; |
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4458
diff
changeset
|
347 } |
4270
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
348 |
4497
95ab6658654a
Fix of rbtree lookup on hash collisions.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4458
diff
changeset
|
349 node = (rc < 0) ? node->left : node->right; |
4270
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
350 } |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
351 |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
352 return NULL; |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
353 } |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
354 |
3544987fef85
Limit zone: rbtree lookup moved to a separate function.
Valentin Bartenev <vbart@nginx.com>
parents:
3186
diff
changeset
|
355 |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
356 static void |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
357 ngx_http_limit_conn_cleanup(void *data) |
980 | 358 { |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
359 ngx_http_limit_conn_cleanup_t *lccln = data; |
980 | 360 |
361 ngx_slab_pool_t *shpool; | |
362 ngx_rbtree_node_t *node; | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
363 ngx_http_limit_conn_ctx_t *ctx; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
364 ngx_http_limit_conn_node_t *lc; |
980 | 365 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
366 ctx = lccln->shm_zone->data; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
367 shpool = (ngx_slab_pool_t *) lccln->shm_zone->shm.addr; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
368 node = lccln->node; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
369 lc = (ngx_http_limit_conn_node_t *) &node->color; |
980 | 370 |
371 ngx_shmtx_lock(&shpool->mutex); | |
372 | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
373 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, lccln->shm_zone->shm.log, 0, |
4756
daa60662b7b5
Fixed debugging messages to account that limit_zone was renamed to limit_conn.
Ruslan Ermilov <ru@nginx.com>
parents:
4499
diff
changeset
|
374 "limit conn cleanup: %08XD %d", node->key, lc->conn); |
980 | 375 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
376 lc->conn--; |
980 | 377 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
378 if (lc->conn == 0) { |
987 | 379 ngx_rbtree_delete(ctx->rbtree, node); |
980 | 380 ngx_slab_free_locked(shpool, node); |
381 } | |
382 | |
383 ngx_shmtx_unlock(&shpool->mutex); | |
384 } | |
385 | |
386 | |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
387 static ngx_inline void |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
388 ngx_http_limit_conn_cleanup_all(ngx_pool_t *pool) |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
389 { |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
390 ngx_pool_cleanup_t *cln; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
391 |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
392 cln = pool->cleanup; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
393 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
394 while (cln && cln->handler == ngx_http_limit_conn_cleanup) { |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
395 ngx_http_limit_conn_cleanup(cln->data); |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
396 cln = cln->next; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
397 } |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
398 |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
399 pool->cleanup = cln; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
400 } |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
401 |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
402 |
980 | 403 static ngx_int_t |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
404 ngx_http_limit_conn_init_zone(ngx_shm_zone_t *shm_zone, void *data) |
980 | 405 { |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
406 ngx_http_limit_conn_ctx_t *octx = data; |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
407 |
2611
2bce3f6416c6
improve ngx_slab_alloc() error logging
Igor Sysoev <igor@sysoev.ru>
parents:
2373
diff
changeset
|
408 size_t len; |
987 | 409 ngx_slab_pool_t *shpool; |
410 ngx_rbtree_node_t *sentinel; | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
411 ngx_http_limit_conn_ctx_t *ctx; |
980 | 412 |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
413 ctx = shm_zone->data; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
414 |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
415 if (octx) { |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
416 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
|
417 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
418 "limit_conn_zone \"%V\" uses the \"%V\" variable " |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
419 "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
|
420 &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
|
421 return NGX_ERROR; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
422 } |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
423 |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
424 ctx->rbtree = octx->rbtree; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
425 |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
426 return NGX_OK; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
427 } |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
428 |
980 | 429 shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; |
430 | |
2720
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
431 if (shm_zone->shm.exists) { |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
432 ctx->rbtree = shpool->data; |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
433 |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
434 return NGX_OK; |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
435 } |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
436 |
987 | 437 ctx->rbtree = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_t)); |
438 if (ctx->rbtree == NULL) { | |
980 | 439 return NGX_ERROR; |
440 } | |
441 | |
2720
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
442 shpool->data = ctx->rbtree; |
b3b8c66bd520
support attaching to an existent Win32 shared memory
Igor Sysoev <igor@sysoev.ru>
parents:
2716
diff
changeset
|
443 |
980 | 444 sentinel = ngx_slab_alloc(shpool, sizeof(ngx_rbtree_node_t)); |
445 if (sentinel == NULL) { | |
446 return NGX_ERROR; | |
447 } | |
448 | |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
449 ngx_rbtree_init(ctx->rbtree, sentinel, |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
450 ngx_http_limit_conn_rbtree_insert_value); |
980 | 451 |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
452 len = sizeof(" in limit_conn_zone \"\"") + shm_zone->shm.name.len; |
2611
2bce3f6416c6
improve ngx_slab_alloc() error logging
Igor Sysoev <igor@sysoev.ru>
parents:
2373
diff
changeset
|
453 |
2bce3f6416c6
improve ngx_slab_alloc() error logging
Igor Sysoev <igor@sysoev.ru>
parents:
2373
diff
changeset
|
454 shpool->log_ctx = ngx_slab_alloc(shpool, len); |
2bce3f6416c6
improve ngx_slab_alloc() error logging
Igor Sysoev <igor@sysoev.ru>
parents:
2373
diff
changeset
|
455 if (shpool->log_ctx == NULL) { |
2bce3f6416c6
improve ngx_slab_alloc() error logging
Igor Sysoev <igor@sysoev.ru>
parents:
2373
diff
changeset
|
456 return NGX_ERROR; |
2bce3f6416c6
improve ngx_slab_alloc() error logging
Igor Sysoev <igor@sysoev.ru>
parents:
2373
diff
changeset
|
457 } |
2bce3f6416c6
improve ngx_slab_alloc() error logging
Igor Sysoev <igor@sysoev.ru>
parents:
2373
diff
changeset
|
458 |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
459 ngx_sprintf(shpool->log_ctx, " in limit_conn_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
|
460 &shm_zone->shm.name); |
2611
2bce3f6416c6
improve ngx_slab_alloc() error logging
Igor Sysoev <igor@sysoev.ru>
parents:
2373
diff
changeset
|
461 |
980 | 462 return NGX_OK; |
463 } | |
464 | |
465 | |
466 static void * | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
467 ngx_http_limit_conn_create_conf(ngx_conf_t *cf) |
980 | 468 { |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
469 ngx_http_limit_conn_conf_t *conf; |
980 | 470 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
471 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_conn_conf_t)); |
980 | 472 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
|
473 return NULL; |
980 | 474 } |
475 | |
476 /* | |
477 * set by ngx_pcalloc(): | |
478 * | |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
479 * conf->limits.elts = NULL; |
980 | 480 */ |
481 | |
3186 | 482 conf->log_level = NGX_CONF_UNSET_UINT; |
5117
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
483 conf->status_code = NGX_CONF_UNSET_UINT; |
3186 | 484 |
980 | 485 return conf; |
486 } | |
487 | |
488 | |
489 static char * | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
490 ngx_http_limit_conn_merge_conf(ngx_conf_t *cf, void *parent, void *child) |
980 | 491 { |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
492 ngx_http_limit_conn_conf_t *prev = parent; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
493 ngx_http_limit_conn_conf_t *conf = child; |
980 | 494 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
495 if (conf->limits.elts == NULL) { |
4380
7697412a0921
Fixed limit_conn_log_level/limit_req_log_level inheritance.
Valentin Bartenev <vbart@nginx.com>
parents:
4280
diff
changeset
|
496 conf->limits = prev->limits; |
980 | 497 } |
498 | |
3186 | 499 ngx_conf_merge_uint_value(conf->log_level, prev->log_level, NGX_LOG_ERR); |
5117
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
500 ngx_conf_merge_uint_value(conf->status_code, prev->status_code, |
00e4459739ed
The limit_req_status and limit_conn_status directives.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4972
diff
changeset
|
501 NGX_HTTP_SERVICE_UNAVAILABLE); |
3186 | 502 |
980 | 503 return NGX_CONF_OK; |
504 } | |
505 | |
506 | |
507 static char * | |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
508 ngx_http_limit_conn_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
509 { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
510 u_char *p; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
511 ssize_t size; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
512 ngx_str_t *value, name, s; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
513 ngx_uint_t i; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
514 ngx_shm_zone_t *shm_zone; |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
515 ngx_http_limit_conn_ctx_t *ctx; |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
516 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
517 value = cf->args->elts; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
518 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
519 ctx = NULL; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
520 size = 0; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
521 name.len = 0; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
522 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
523 for (i = 1; i < cf->args->nelts; i++) { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
524 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
525 if (ngx_strncmp(value[i].data, "zone=", 5) == 0) { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
526 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
527 name.data = value[i].data + 5; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
528 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
529 p = (u_char *) ngx_strchr(name.data, ':'); |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
530 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
531 if (p == NULL) { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
532 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
533 "invalid zone size \"%V\"", &value[i]); |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
534 return NGX_CONF_ERROR; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
535 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
536 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
537 name.len = p - name.data; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
538 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
539 s.data = p + 1; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
540 s.len = value[i].data + value[i].len - s.data; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
541 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
542 size = ngx_parse_size(&s); |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
543 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
544 if (size == NGX_ERROR) { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
545 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
546 "invalid zone size \"%V\"", &value[i]); |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
547 return NGX_CONF_ERROR; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
548 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
549 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
550 if (size < (ssize_t) (8 * ngx_pagesize)) { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
551 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
552 "zone \"%V\" is too small", &value[i]); |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
553 return NGX_CONF_ERROR; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
554 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
555 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
556 continue; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
557 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
558 |
4972
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4963
diff
changeset
|
559 if (value[i].data[0] == '$') { |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
560 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
561 value[i].len--; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
562 value[i].data++; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
563 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
564 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_conn_ctx_t)); |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
565 if (ctx == NULL) { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
566 return NGX_CONF_ERROR; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
567 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
568 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
569 ctx->index = ngx_http_get_variable_index(cf, &value[i]); |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
570 if (ctx->index == NGX_ERROR) { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
571 return NGX_CONF_ERROR; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
572 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
573 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
574 ctx->var = value[i]; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
575 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
576 continue; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
577 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
578 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
579 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
580 "invalid parameter \"%V\"", &value[i]); |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
581 return NGX_CONF_ERROR; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
582 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
583 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
584 if (name.len == 0) { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
585 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
586 "\"%V\" must have \"zone\" parameter", |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
587 &cmd->name); |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
588 return NGX_CONF_ERROR; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
589 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
590 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
591 if (ctx == NULL) { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
592 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
593 "no variable is defined for %V \"%V\"", |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
594 &cmd->name, &name); |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
595 return NGX_CONF_ERROR; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
596 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
597 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
598 shm_zone = ngx_shared_memory_add(cf, &name, size, |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
599 &ngx_http_limit_conn_module); |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
600 if (shm_zone == NULL) { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
601 return NGX_CONF_ERROR; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
602 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
603 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
604 if (shm_zone->data) { |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
605 ctx = shm_zone->data; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
606 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
607 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
608 "%V \"%V\" is already bound to variable \"%V\"", |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
609 &cmd->name, &name, &ctx->var); |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
610 return NGX_CONF_ERROR; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
611 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
612 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
613 shm_zone->init = ngx_http_limit_conn_init_zone; |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
614 shm_zone->data = ctx; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
615 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
616 return NGX_CONF_OK; |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
617 } |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
618 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
619 |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
620 static char * |
980 | 621 ngx_http_limit_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
622 { | |
987 | 623 ssize_t n; |
624 ngx_str_t *value; | |
625 ngx_shm_zone_t *shm_zone; | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
626 ngx_http_limit_conn_ctx_t *ctx; |
980 | 627 |
4272
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
628 ngx_conf_deprecated(cf, &ngx_conf_deprecated_limit_zone, NULL); |
260d591cb6a3
Limit zone: added the "limit_conn_zone" directive.
Valentin Bartenev <vbart@nginx.com>
parents:
4271
diff
changeset
|
629 |
980 | 630 value = cf->args->elts; |
631 | |
4972
8b635cf36ccc
Added checks that disallow adding a variable with an empty name.
Ruslan Ermilov <ru@nginx.com>
parents:
4963
diff
changeset
|
632 if (value[2].data[0] != '$') { |
987 | 633 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
634 "invalid variable name \"%V\"", &value[2]); | |
635 return NGX_CONF_ERROR; | |
636 } | |
637 | |
638 value[2].len--; | |
639 value[2].data++; | |
640 | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
641 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_conn_ctx_t)); |
987 | 642 if (ctx == NULL) { |
643 return NGX_CONF_ERROR; | |
644 } | |
645 | |
646 ctx->index = ngx_http_get_variable_index(cf, &value[2]); | |
647 if (ctx->index == NGX_ERROR) { | |
648 return NGX_CONF_ERROR; | |
649 } | |
650 | |
651 ctx->var = value[2]; | |
652 | |
653 n = ngx_parse_size(&value[3]); | |
980 | 654 |
655 if (n == NGX_ERROR) { | |
656 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
987 | 657 "invalid size of limit_zone \"%V\"", &value[3]); |
980 | 658 return NGX_CONF_ERROR; |
659 } | |
660 | |
661 if (n < (ngx_int_t) (8 * ngx_pagesize)) { | |
662 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
663 "limit_zone \"%V\" is too small", &value[1]); | |
664 return NGX_CONF_ERROR; | |
665 } | |
666 | |
667 | |
668 shm_zone = ngx_shared_memory_add(cf, &value[1], n, | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
669 &ngx_http_limit_conn_module); |
980 | 670 if (shm_zone == NULL) { |
671 return NGX_CONF_ERROR; | |
672 } | |
673 | |
987 | 674 if (shm_zone->data) { |
675 ctx = shm_zone->data; | |
676 | |
677 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
678 "limit_zone \"%V\" is already bound to variable \"%V\"", | |
679 &value[1], &ctx->var); | |
680 return NGX_CONF_ERROR; | |
681 } | |
682 | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
683 shm_zone->init = ngx_http_limit_conn_init_zone; |
987 | 684 shm_zone->data = ctx; |
980 | 685 |
686 return NGX_CONF_OK; | |
687 } | |
688 | |
689 | |
690 static char * | |
691 ngx_http_limit_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) | |
692 { | |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
693 ngx_shm_zone_t *shm_zone; |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
694 ngx_http_limit_conn_conf_t *lccf = conf; |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
695 ngx_http_limit_conn_limit_t *limit, *limits; |
980 | 696 |
697 ngx_str_t *value; | |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
698 ngx_int_t n; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
699 ngx_uint_t i; |
2372 | 700 |
980 | 701 value = cf->args->elts; |
702 | |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
703 shm_zone = ngx_shared_memory_add(cf, &value[1], 0, |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
704 &ngx_http_limit_conn_module); |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
705 if (shm_zone == NULL) { |
980 | 706 return NGX_CONF_ERROR; |
707 } | |
708 | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
709 limits = lccf->limits.elts; |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
710 |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
711 if (limits == NULL) { |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
712 if (ngx_array_init(&lccf->limits, cf->pool, 1, |
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
713 sizeof(ngx_http_limit_conn_limit_t)) |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
714 != NGX_OK) |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
715 { |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
716 return NGX_CONF_ERROR; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
717 } |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
718 } |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
719 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
720 for (i = 0; i < lccf->limits.nelts; i++) { |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
721 if (shm_zone == limits[i].shm_zone) { |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
722 return "is duplicate"; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
723 } |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
724 } |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
725 |
987 | 726 n = ngx_atoi(value[2].data, value[2].len); |
727 if (n <= 0) { | |
980 | 728 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
987 | 729 "invalid number of connections \"%V\"", &value[2]); |
980 | 730 return NGX_CONF_ERROR; |
731 } | |
732 | |
1011
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
733 if (n > 65535) { |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
734 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
|
735 "connection limit must be less 65536"); |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
736 return NGX_CONF_ERROR; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
737 } |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
738 |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
739 limit = ngx_array_push(&lccf->limits); |
4811
21d1e3bcb356
Added three missing checks for NULL after ngx_array_push() calls.
Valentin Bartenev <vbart@nginx.com>
parents:
4756
diff
changeset
|
740 if (limit == NULL) { |
21d1e3bcb356
Added three missing checks for NULL after ngx_array_push() calls.
Valentin Bartenev <vbart@nginx.com>
parents:
4756
diff
changeset
|
741 return NGX_CONF_ERROR; |
21d1e3bcb356
Added three missing checks for NULL after ngx_array_push() calls.
Valentin Bartenev <vbart@nginx.com>
parents:
4756
diff
changeset
|
742 } |
21d1e3bcb356
Added three missing checks for NULL after ngx_array_push() calls.
Valentin Bartenev <vbart@nginx.com>
parents:
4756
diff
changeset
|
743 |
4271
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
744 limit->conn = n; |
b86cceba426a
Limit zone: support for multiple "limit_conn" limits.
Valentin Bartenev <vbart@nginx.com>
parents:
4270
diff
changeset
|
745 limit->shm_zone = shm_zone; |
980 | 746 |
747 return NGX_CONF_OK; | |
748 } | |
749 | |
750 | |
751 static ngx_int_t | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
752 ngx_http_limit_conn_init(ngx_conf_t *cf) |
980 | 753 { |
754 ngx_http_handler_pt *h; | |
755 ngx_http_core_main_conf_t *cmcf; | |
756 | |
757 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); | |
758 | |
759 h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers); | |
760 if (h == NULL) { | |
761 return NGX_ERROR; | |
762 } | |
763 | |
4280
91874133fb27
Renamed ngx_http_limit_zone_module to ngx_http_limit_conn_module.
Valentin Bartenev <vbart@nginx.com>
parents:
4272
diff
changeset
|
764 *h = ngx_http_limit_conn_handler; |
980 | 765 |
766 return NGX_OK; | |
767 } |