Mercurial > hg > nginx-quic
annotate src/http/modules/ngx_http_limit_req_module.c @ 2535:ec5825afc7eb
use variable for often used field
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 24 Feb 2009 08:32:02 +0000 |
parents | 29d89920a749 |
children | 2bce3f6416c6 |
rev | line source |
---|---|
980 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 */ | |
5 | |
6 | |
7 #include <ngx_config.h> | |
8 #include <ngx_core.h> | |
9 #include <ngx_http.h> | |
10 | |
11 | |
12 typedef struct { | |
981 | 13 u_char color; |
2294 | 14 u_char dummy; |
15 u_short len; | |
16 ngx_queue_t queue; | |
17 ngx_msec_t last; | |
2313 | 18 ngx_uint_t excess; /* integer value, 1 corresponds to 0.001 r/s */ |
980 | 19 u_char data[1]; |
2294 | 20 } ngx_http_limit_req_node_t; |
980 | 21 |
22 | |
23 typedef struct { | |
987 | 24 ngx_rbtree_t *rbtree; |
2294 | 25 ngx_queue_t *queue; |
26 ngx_slab_pool_t *shpool; | |
2313 | 27 ngx_uint_t rate; /* integer value, 1 corresponds to 0.001 r/s */ |
987 | 28 ngx_int_t index; |
29 ngx_str_t var; | |
2294 | 30 } ngx_http_limit_req_ctx_t; |
987 | 31 |
32 | |
33 typedef struct { | |
980 | 34 ngx_shm_zone_t *shm_zone; |
2313 | 35 ngx_uint_t burst; /* integer value, 1 corresponds to 0.001 r/s */ |
36 ngx_uint_t nodelay;/* unsigned nodelay:1 */ | |
2294 | 37 } ngx_http_limit_req_conf_t; |
980 | 38 |
39 | |
2294 | 40 static void ngx_http_limit_req_delay(ngx_http_request_t *r); |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
41 static ngx_int_t ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, |
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
42 ngx_uint_t hash, u_char *data, size_t len, ngx_http_limit_req_node_t **lrp); |
2294 | 43 static void ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx, |
44 ngx_uint_t n); | |
980 | 45 |
2294 | 46 static void *ngx_http_limit_req_create_conf(ngx_conf_t *cf); |
47 static char *ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent, | |
980 | 48 void *child); |
2294 | 49 static char *ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, |
980 | 50 void *conf); |
2294 | 51 static char *ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, |
980 | 52 void *conf); |
2294 | 53 static ngx_int_t ngx_http_limit_req_init(ngx_conf_t *cf); |
980 | 54 |
55 | |
2294 | 56 static ngx_command_t ngx_http_limit_req_commands[] = { |
980 | 57 |
2294 | 58 { ngx_string("limit_req_zone"), |
987 | 59 NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE3, |
2294 | 60 ngx_http_limit_req_zone, |
980 | 61 0, |
62 0, | |
63 NULL }, | |
64 | |
2294 | 65 { ngx_string("limit_req"), |
66 NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE123, | |
67 ngx_http_limit_req, | |
980 | 68 NGX_HTTP_LOC_CONF_OFFSET, |
69 0, | |
70 NULL }, | |
71 | |
72 ngx_null_command | |
73 }; | |
74 | |
75 | |
2294 | 76 static ngx_http_module_t ngx_http_limit_req_module_ctx = { |
980 | 77 NULL, /* preconfiguration */ |
2294 | 78 ngx_http_limit_req_init, /* postconfiguration */ |
980 | 79 |
80 NULL, /* create main configuration */ | |
81 NULL, /* init main configuration */ | |
82 | |
83 NULL, /* create server configuration */ | |
84 NULL, /* merge server configuration */ | |
85 | |
2294 | 86 ngx_http_limit_req_create_conf, /* create location configration */ |
87 ngx_http_limit_req_merge_conf /* merge location configration */ | |
980 | 88 }; |
89 | |
90 | |
2294 | 91 ngx_module_t ngx_http_limit_req_module = { |
980 | 92 NGX_MODULE_V1, |
2294 | 93 &ngx_http_limit_req_module_ctx, /* module context */ |
94 ngx_http_limit_req_commands, /* module directives */ | |
980 | 95 NGX_HTTP_MODULE, /* module type */ |
96 NULL, /* init master */ | |
97 NULL, /* init module */ | |
98 NULL, /* init process */ | |
99 NULL, /* init thread */ | |
100 NULL, /* exit thread */ | |
101 NULL, /* exit process */ | |
102 NULL, /* exit master */ | |
103 NGX_MODULE_V1_PADDING | |
104 }; | |
105 | |
106 | |
107 static ngx_int_t | |
2294 | 108 ngx_http_limit_req_handler(ngx_http_request_t *r) |
980 | 109 { |
2294 | 110 size_t len, n; |
111 uint32_t hash; | |
112 ngx_int_t rc; | |
2313 | 113 ngx_uint_t excess; |
2294 | 114 ngx_time_t *tp; |
115 ngx_rbtree_node_t *node; | |
116 ngx_http_variable_value_t *vv; | |
117 ngx_http_limit_req_ctx_t *ctx; | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
118 ngx_http_limit_req_node_t *lr; |
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
119 ngx_http_limit_req_conf_t *lrcf; |
980 | 120 |
2294 | 121 if (r->main->limit_req_set) { |
984
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
122 return NGX_DECLINED; |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
123 } |
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
124 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
125 lrcf = ngx_http_get_module_loc_conf(r, ngx_http_limit_req_module); |
980 | 126 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
127 if (lrcf->shm_zone == NULL) { |
980 | 128 return NGX_DECLINED; |
129 } | |
130 | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
131 ctx = lrcf->shm_zone->data; |
987 | 132 |
133 vv = ngx_http_get_indexed_variable(r, ctx->index); | |
980 | 134 |
135 if (vv == NULL || vv->not_found) { | |
136 return NGX_DECLINED; | |
137 } | |
138 | |
1011
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
139 len = vv->len; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
140 |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
141 if (len == 0) { |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
142 return NGX_DECLINED; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
143 } |
984
dd128232e6ba
count connection once per request
Igor Sysoev <igor@sysoev.ru>
parents:
981
diff
changeset
|
144 |
2294 | 145 if (len > 65535) { |
1011
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
146 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
147 "the value of the \"%V\" variable " |
2294 | 148 "is more than 65535 bytes: \"%v\"", |
1011
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
149 &ctx->var, vv); |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
150 return NGX_DECLINED; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
151 } |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
152 |
2294 | 153 r->main->limit_req_set = 1; |
980 | 154 |
155 hash = ngx_crc32_short(vv->data, len); | |
156 | |
2294 | 157 ngx_shmtx_lock(&ctx->shpool->mutex); |
158 | |
159 ngx_http_limit_req_expire(ctx, 1); | |
160 | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
161 rc = ngx_http_limit_req_lookup(lrcf, hash, vv->data, len, &lr); |
2294 | 162 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
163 if (lr) { |
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
164 ngx_queue_remove(&lr->queue); |
2294 | 165 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
166 ngx_queue_insert_head(ctx->queue, &lr->queue); |
2294 | 167 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
168 excess = lr->excess; |
2294 | 169 |
170 } else { | |
2313 | 171 excess = 0; |
980 | 172 } |
173 | |
2313 | 174 ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, |
175 "limit_req: %i %ui.%03ui", rc, excess / 1000, excess % 1000); | |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
176 |
2294 | 177 if (rc == NGX_BUSY) { |
178 ngx_shmtx_unlock(&ctx->shpool->mutex); | |
1012
11ffb8e4753f
stop rbtree search early if equal hash was found
Igor Sysoev <igor@sysoev.ru>
parents:
1011
diff
changeset
|
179 |
2294 | 180 ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, |
2376
29d89920a749
*) add zone name while logging 503 error reason
Igor Sysoev <igor@sysoev.ru>
parents:
2375
diff
changeset
|
181 "limiting requests, excess: %ui.%03ui by zone \"%V\"", |
29d89920a749
*) add zone name while logging 503 error reason
Igor Sysoev <igor@sysoev.ru>
parents:
2375
diff
changeset
|
182 excess / 1000, excess % 1000, &lrcf->shm_zone->name); |
980 | 183 |
184 return NGX_HTTP_SERVICE_UNAVAILABLE; | |
185 } | |
186 | |
2294 | 187 if (rc == NGX_AGAIN) { |
188 ngx_shmtx_unlock(&ctx->shpool->mutex); | |
189 | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
190 if (lrcf->nodelay) { |
2300
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
191 return NGX_DECLINED; |
2294 | 192 } |
193 | |
2300
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
194 ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, |
2376
29d89920a749
*) add zone name while logging 503 error reason
Igor Sysoev <igor@sysoev.ru>
parents:
2375
diff
changeset
|
195 "delaying request, excess: %ui.%03ui, by zone \"%V\"", |
29d89920a749
*) add zone name while logging 503 error reason
Igor Sysoev <igor@sysoev.ru>
parents:
2375
diff
changeset
|
196 excess / 1000, excess % 1000, &lrcf->shm_zone->name); |
2300
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
197 |
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
198 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) { |
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
199 return NGX_HTTP_INTERNAL_SERVER_ERROR; |
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
200 } |
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
201 |
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
202 r->read_event_handler = ngx_http_test_reading; |
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
203 r->write_event_handler = ngx_http_limit_req_delay; |
2313 | 204 ngx_add_timer(r->connection->write, (ngx_msec_t) excess); |
2300
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
205 |
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
206 return NGX_AGAIN; |
2294 | 207 } |
208 | |
209 if (rc == NGX_OK) { | |
210 goto done; | |
211 } | |
212 | |
213 /* rc == NGX_DECLINED */ | |
214 | |
215 n = offsetof(ngx_rbtree_node_t, color) | |
216 + offsetof(ngx_http_limit_req_node_t, data) | |
217 + len; | |
218 | |
219 node = ngx_slab_alloc_locked(ctx->shpool, n); | |
220 if (node == NULL) { | |
221 | |
222 ngx_http_limit_req_expire(ctx, 0); | |
223 | |
224 node = ngx_slab_alloc_locked(ctx->shpool, n); | |
225 if (node == NULL) { | |
226 ngx_shmtx_unlock(&ctx->shpool->mutex); | |
2376
29d89920a749
*) add zone name while logging 503 error reason
Igor Sysoev <igor@sysoev.ru>
parents:
2375
diff
changeset
|
227 |
29d89920a749
*) add zone name while logging 503 error reason
Igor Sysoev <igor@sysoev.ru>
parents:
2375
diff
changeset
|
228 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0, |
29d89920a749
*) add zone name while logging 503 error reason
Igor Sysoev <igor@sysoev.ru>
parents:
2375
diff
changeset
|
229 "could not allocate memory in zone \"%V\"", |
29d89920a749
*) add zone name while logging 503 error reason
Igor Sysoev <igor@sysoev.ru>
parents:
2375
diff
changeset
|
230 &lrcf->shm_zone->name); |
29d89920a749
*) add zone name while logging 503 error reason
Igor Sysoev <igor@sysoev.ru>
parents:
2375
diff
changeset
|
231 |
2294 | 232 return NGX_HTTP_SERVICE_UNAVAILABLE; |
233 } | |
234 } | |
235 | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
236 lr = (ngx_http_limit_req_node_t *) &node->color; |
980 | 237 |
238 node->key = hash; | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
239 lr->len = (u_char) len; |
2294 | 240 |
241 tp = ngx_timeofday(); | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
242 lr->last = (ngx_msec_t) (tp->sec * 1000 + tp->msec); |
2294 | 243 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
244 lr->excess = 0; |
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
245 ngx_memcpy(lr->data, vv->data, len); |
980 | 246 |
987 | 247 ngx_rbtree_insert(ctx->rbtree, node); |
980 | 248 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
249 ngx_queue_insert_head(ctx->queue, &lr->queue); |
2294 | 250 |
980 | 251 done: |
252 | |
2294 | 253 ngx_shmtx_unlock(&ctx->shpool->mutex); |
980 | 254 |
255 return NGX_DECLINED; | |
256 } | |
257 | |
258 | |
259 static void | |
2294 | 260 ngx_http_limit_req_delay(ngx_http_request_t *r) |
261 { | |
262 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, | |
263 "limit_req delay"); | |
264 | |
265 if (ngx_handle_read_event(r->connection->read, 0) != NGX_OK) { | |
266 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); | |
267 return; | |
268 } | |
269 | |
270 r->read_event_handler = ngx_http_block_reading; | |
271 r->write_event_handler = ngx_http_core_run_phases; | |
272 | |
273 ngx_http_core_run_phases(r); | |
274 } | |
275 | |
276 | |
277 static void | |
278 ngx_http_limit_req_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
279 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
|
280 { |
2294 | 281 ngx_rbtree_node_t **p; |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
282 ngx_http_limit_req_node_t *lrn, *lrnt; |
1026
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 for ( ;; ) { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
285 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
286 if (node->key < temp->key) { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
287 |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
288 p = &temp->left; |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
289 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
290 } else if (node->key > temp->key) { |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
291 |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
292 p = &temp->right; |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
293 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
294 } else { /* node->key == temp->key */ |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
295 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
296 lrn = (ngx_http_limit_req_node_t *) &node->color; |
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
297 lrnt = (ngx_http_limit_req_node_t *) &temp->color; |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
298 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
299 p = (ngx_memn2cmp(lrn->data, lrnt->data, lrn->len, lrnt->len) < 0) |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
300 ? &temp->left : &temp->right; |
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
301 } |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
302 |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
303 if (*p == sentinel) { |
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
304 break; |
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
305 } |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
306 |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
307 temp = *p; |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
308 } |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
309 |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
310 *p = node; |
1026
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
311 node->parent = temp; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
312 node->left = sentinel; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
313 node->right = sentinel; |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
314 ngx_rbt_red(node); |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
315 } |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
316 |
38be15c1379a
fix duplicate rbtree keys case
Igor Sysoev <igor@sysoev.ru>
parents:
1012
diff
changeset
|
317 |
2294 | 318 static ngx_int_t |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
319 ngx_http_limit_req_lookup(ngx_http_limit_req_conf_t *lrcf, ngx_uint_t hash, |
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
320 u_char *data, size_t len, ngx_http_limit_req_node_t **lrp) |
980 | 321 { |
2313 | 322 ngx_int_t rc, excess; |
2294 | 323 ngx_time_t *tp; |
324 ngx_msec_t now; | |
325 ngx_msec_int_t ms; | |
326 ngx_rbtree_node_t *node, *sentinel; | |
327 ngx_http_limit_req_ctx_t *ctx; | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
328 ngx_http_limit_req_node_t *lr; |
2294 | 329 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
330 ctx = lrcf->shm_zone->data; |
2294 | 331 |
332 node = ctx->rbtree->root; | |
333 sentinel = ctx->rbtree->sentinel; | |
980 | 334 |
2294 | 335 while (node != sentinel) { |
336 | |
337 if (hash < node->key) { | |
338 node = node->left; | |
339 continue; | |
340 } | |
341 | |
342 if (hash > node->key) { | |
343 node = node->right; | |
344 continue; | |
345 } | |
346 | |
347 /* hash == node->key */ | |
348 | |
349 do { | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
350 lr = (ngx_http_limit_req_node_t *) &node->color; |
980 | 351 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
352 rc = ngx_memn2cmp(data, lr->data, len, (size_t) lr->len); |
2294 | 353 |
354 if (rc == 0) { | |
355 | |
356 tp = ngx_timeofday(); | |
980 | 357 |
2294 | 358 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec); |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
359 ms = (ngx_msec_int_t) (now - lr->last); |
2294 | 360 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
361 excess = lr->excess - ctx->rate * ngx_abs(ms) / 1000 + 1000; |
2294 | 362 |
2313 | 363 if (excess < 0) { |
364 excess = 0; | |
2294 | 365 } |
366 | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
367 lr->excess = excess; |
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
368 lr->last = now; |
980 | 369 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
370 *lrp = lr; |
2294 | 371 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
372 if ((ngx_uint_t) excess > lrcf->burst) { |
2294 | 373 return NGX_BUSY; |
374 } | |
980 | 375 |
2313 | 376 if (excess) { |
2294 | 377 return NGX_AGAIN; |
378 } | |
980 | 379 |
2294 | 380 return NGX_OK; |
381 } | |
382 | |
383 node = (rc < 0) ? node->left : node->right; | |
384 | |
385 } while (node != sentinel && hash == node->key); | |
386 | |
387 break; | |
980 | 388 } |
389 | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
390 *lrp = NULL; |
2294 | 391 |
392 return NGX_DECLINED; | |
393 } | |
394 | |
395 | |
396 static void | |
397 ngx_http_limit_req_expire(ngx_http_limit_req_ctx_t *ctx, ngx_uint_t n) | |
398 { | |
2313 | 399 ngx_int_t excess; |
2294 | 400 ngx_time_t *tp; |
401 ngx_msec_t now; | |
402 ngx_queue_t *q; | |
403 ngx_msec_int_t ms; | |
404 ngx_rbtree_node_t *node; | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
405 ngx_http_limit_req_node_t *lr; |
2294 | 406 |
407 tp = ngx_timeofday(); | |
408 | |
409 now = (ngx_msec_t) (tp->sec * 1000 + tp->msec); | |
410 | |
411 /* | |
412 * n == 1 deletes one or two zero rate entries | |
413 * n == 0 deletes oldest entry by force | |
414 * and one or two zero rate entries | |
415 */ | |
416 | |
417 while (n < 3) { | |
418 | |
419 if (ngx_queue_empty(ctx->queue)) { | |
420 return; | |
421 } | |
422 | |
423 q = ngx_queue_last(ctx->queue); | |
424 | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
425 lr = ngx_queue_data(q, ngx_http_limit_req_node_t, queue); |
2294 | 426 |
427 if (n++ != 0) { | |
428 | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
429 ms = (ngx_msec_int_t) (now - lr->last); |
2294 | 430 ms = ngx_abs(ms); |
431 | |
432 if (ms < 60000) { | |
433 return; | |
434 } | |
435 | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
436 excess = lr->excess - ctx->rate * ms / 1000; |
2294 | 437 |
2313 | 438 if (excess > 0) { |
2294 | 439 return; |
440 } | |
441 } | |
442 | |
443 ngx_queue_remove(q); | |
444 | |
445 node = (ngx_rbtree_node_t *) | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
446 ((u_char *) lr - offsetof(ngx_rbtree_node_t, color)); |
2294 | 447 |
448 ngx_rbtree_delete(ctx->rbtree, node); | |
449 | |
450 ngx_slab_free_locked(ctx->shpool, node); | |
451 } | |
980 | 452 } |
453 | |
454 | |
455 static ngx_int_t | |
2294 | 456 ngx_http_limit_req_init_zone(ngx_shm_zone_t *shm_zone, void *data) |
980 | 457 { |
2294 | 458 ngx_http_limit_req_ctx_t *octx = data; |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
459 |
2294 | 460 ngx_rbtree_node_t *sentinel; |
461 ngx_http_limit_req_ctx_t *ctx; | |
980 | 462 |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
463 ctx = shm_zone->data; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
464 |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
465 if (octx) { |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
466 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
|
467 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0, |
2294 | 468 "limit_req \"%V\" uses the \"%V\" variable " |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
469 "while previously it used the \"%V\" variable", |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
470 &shm_zone->name, &ctx->var, &octx->var); |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
471 return NGX_ERROR; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
472 } |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
473 |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
474 ctx->rbtree = octx->rbtree; |
2294 | 475 ctx->queue = octx->queue; |
476 ctx->shpool = octx->shpool; | |
993
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
477 |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
478 return NGX_OK; |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
479 } |
1b9a4d92173f
pass the inherited shm_zone data
Igor Sysoev <igor@sysoev.ru>
parents:
987
diff
changeset
|
480 |
2294 | 481 ctx->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr; |
980 | 482 |
2294 | 483 ctx->rbtree = ngx_slab_alloc(ctx->shpool, sizeof(ngx_rbtree_t)); |
987 | 484 if (ctx->rbtree == NULL) { |
980 | 485 return NGX_ERROR; |
486 } | |
487 | |
2294 | 488 sentinel = ngx_slab_alloc(ctx->shpool, sizeof(ngx_rbtree_node_t)); |
980 | 489 if (sentinel == NULL) { |
490 return NGX_ERROR; | |
491 } | |
492 | |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1406
diff
changeset
|
493 ngx_rbtree_init(ctx->rbtree, sentinel, |
2294 | 494 ngx_http_limit_req_rbtree_insert_value); |
495 | |
496 ctx->queue = ngx_slab_alloc(ctx->shpool, sizeof(ngx_queue_t)); | |
497 if (ctx->queue == NULL) { | |
498 return NGX_ERROR; | |
499 } | |
500 | |
501 ngx_queue_init(ctx->queue); | |
980 | 502 |
503 return NGX_OK; | |
504 } | |
505 | |
506 | |
507 static void * | |
2294 | 508 ngx_http_limit_req_create_conf(ngx_conf_t *cf) |
980 | 509 { |
2294 | 510 ngx_http_limit_req_conf_t *conf; |
980 | 511 |
2294 | 512 conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_conf_t)); |
980 | 513 if (conf == NULL) { |
514 return NGX_CONF_ERROR; | |
515 } | |
516 | |
517 /* | |
518 * set by ngx_pcalloc(): | |
519 * | |
520 * conf->shm_zone = NULL; | |
2375 | 521 * conf->burst = 0; |
2300
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
522 * conf->nodelay = 0; |
980 | 523 */ |
524 | |
525 return conf; | |
526 } | |
527 | |
528 | |
529 static char * | |
2294 | 530 ngx_http_limit_req_merge_conf(ngx_conf_t *cf, void *parent, void *child) |
980 | 531 { |
2294 | 532 ngx_http_limit_req_conf_t *prev = parent; |
533 ngx_http_limit_req_conf_t *conf = child; | |
980 | 534 |
535 if (conf->shm_zone == NULL) { | |
536 *conf = *prev; | |
537 } | |
538 | |
539 return NGX_CONF_OK; | |
540 } | |
541 | |
542 | |
543 static char * | |
2294 | 544 ngx_http_limit_req_zone(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
980 | 545 { |
2294 | 546 u_char *p; |
547 size_t size, len; | |
548 ngx_str_t *value, name, s; | |
549 ngx_int_t rate, scale; | |
550 ngx_uint_t i; | |
551 ngx_shm_zone_t *shm_zone; | |
552 ngx_http_limit_req_ctx_t *ctx; | |
980 | 553 |
554 value = cf->args->elts; | |
555 | |
2294 | 556 ctx = NULL; |
557 size = 0; | |
558 rate = 1; | |
559 scale = 1; | |
560 name.len = 0; | |
561 | |
562 for (i = 1; i < cf->args->nelts; i++) { | |
563 | |
564 if (ngx_strncmp(value[i].data, "zone=", 5) == 0) { | |
565 | |
566 name.data = value[i].data + 5; | |
567 | |
568 p = (u_char *) ngx_strchr(name.data, ':'); | |
569 | |
570 if (p) { | |
571 name.len = p - name.data; | |
572 | |
573 p++; | |
574 | |
575 s.len = value[i].data + value[i].len - p; | |
576 s.data = p; | |
577 | |
578 size = ngx_parse_size(&s); | |
579 if (size > 8191) { | |
580 continue; | |
581 } | |
582 } | |
583 | |
584 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
585 "invalid zone size \"%V\"", &value[i]); | |
586 return NGX_CONF_ERROR; | |
587 } | |
588 | |
589 if (ngx_strncmp(value[i].data, "rate=", 5) == 0) { | |
590 | |
591 len = value[i].len; | |
592 p = value[i].data + len - 3; | |
987 | 593 |
2294 | 594 if (ngx_strncmp(p, "r/s", 3) == 0) { |
595 scale = 1; | |
596 len -= 3; | |
597 | |
598 } else if (ngx_strncmp(p, "r/m", 3) == 0) { | |
599 scale = 60; | |
600 len -= 3; | |
601 } | |
602 | |
603 rate = ngx_atoi(value[i].data + 5, len - 5); | |
604 if (rate <= NGX_ERROR) { | |
605 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
606 "invalid rate \"%V\"", &value[i]); | |
607 return NGX_CONF_ERROR; | |
608 } | |
609 | |
610 continue; | |
611 } | |
612 | |
613 if (value[i].data[0] == '$') { | |
987 | 614 |
2294 | 615 value[i].len--; |
616 value[i].data++; | |
617 | |
618 ctx = ngx_pcalloc(cf->pool, sizeof(ngx_http_limit_req_ctx_t)); | |
619 if (ctx == NULL) { | |
620 return NGX_CONF_ERROR; | |
621 } | |
622 | |
623 ctx->index = ngx_http_get_variable_index(cf, &value[i]); | |
624 if (ctx->index == NGX_ERROR) { | |
625 return NGX_CONF_ERROR; | |
626 } | |
627 | |
628 ctx->var = value[i]; | |
629 | |
630 continue; | |
631 } | |
632 | |
633 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
634 "invalid parameter \"%V\"", &value[i]); | |
987 | 635 return NGX_CONF_ERROR; |
636 } | |
637 | |
2294 | 638 if (name.len == 0 || size == 0) { |
639 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
640 "\"%V\" must have \"zone\" parameter", | |
641 &cmd->name); | |
987 | 642 return NGX_CONF_ERROR; |
643 } | |
644 | |
2294 | 645 if (ctx == NULL) { |
980 | 646 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2294 | 647 "no variable is defined for limit_req_zone \"%V\"", |
648 &cmd->name); | |
980 | 649 return NGX_CONF_ERROR; |
650 } | |
651 | |
2313 | 652 ctx->rate = rate * 1000 / scale; |
980 | 653 |
2294 | 654 shm_zone = ngx_shared_memory_add(cf, &name, size, |
655 &ngx_http_limit_req_module); | |
980 | 656 if (shm_zone == NULL) { |
657 return NGX_CONF_ERROR; | |
658 } | |
659 | |
987 | 660 if (shm_zone->data) { |
661 ctx = shm_zone->data; | |
662 | |
663 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
2294 | 664 "limit_req_zone \"%V\" is already bound to variable \"%V\"", |
665 &value[1], &ctx->var); | |
987 | 666 return NGX_CONF_ERROR; |
667 } | |
668 | |
2294 | 669 shm_zone->init = ngx_http_limit_req_init_zone; |
987 | 670 shm_zone->data = ctx; |
980 | 671 |
672 return NGX_CONF_OK; | |
673 } | |
674 | |
675 | |
676 static char * | |
2294 | 677 ngx_http_limit_req(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
980 | 678 { |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
679 ngx_http_limit_req_conf_t *lrcf = conf; |
980 | 680 |
2300
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
681 ngx_int_t burst; |
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
682 ngx_str_t *value, s; |
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
683 ngx_uint_t i; |
2294 | 684 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
685 if (lrcf->shm_zone) { |
2294 | 686 return "is duplicate"; |
687 } | |
980 | 688 |
689 value = cf->args->elts; | |
690 | |
2294 | 691 burst = 0; |
692 | |
693 for (i = 1; i < cf->args->nelts; i++) { | |
694 | |
695 if (ngx_strncmp(value[i].data, "zone=", 5) == 0) { | |
696 | |
697 s.len = value[i].len - 5; | |
698 s.data = value[i].data + 5; | |
699 | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
700 lrcf->shm_zone = ngx_shared_memory_add(cf, &s, 0, |
2294 | 701 &ngx_http_limit_req_module); |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
702 if (lrcf->shm_zone == NULL) { |
2294 | 703 return NGX_CONF_ERROR; |
704 } | |
705 | |
706 continue; | |
707 } | |
708 | |
709 if (ngx_strncmp(value[i].data, "burst=", 6) == 0) { | |
710 | |
2300
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
711 burst = ngx_atoi(value[i].data + 6, value[i].len - 6); |
2294 | 712 if (burst <= 0) { |
713 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
714 "invalid burst rate \"%V\"", &value[i]); | |
715 return NGX_CONF_ERROR; | |
716 } | |
717 | |
718 continue; | |
719 } | |
720 | |
2300
159136c9808d
*) correct leaky bucket implementation
Igor Sysoev <igor@sysoev.ru>
parents:
2294
diff
changeset
|
721 if (ngx_strncmp(value[i].data, "nodelay", 7) == 0) { |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
722 lrcf->nodelay = 1; |
2294 | 723 continue; |
724 } | |
725 | |
726 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, | |
727 "invalid parameter \"%V\"", &value[i]); | |
980 | 728 return NGX_CONF_ERROR; |
729 } | |
730 | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
731 if (lrcf->shm_zone == NULL) { |
980 | 732 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2294 | 733 "\"%V\" must have \"zone\" parameter", |
734 &cmd->name); | |
980 | 735 return NGX_CONF_ERROR; |
736 } | |
737 | |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
738 if (lrcf->shm_zone->data == NULL) { |
1011
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
739 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, |
2294 | 740 "unknown limit_req_zone \"%V\"", |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
741 &lrcf->shm_zone->name); |
1011
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
742 return NGX_CONF_ERROR; |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
743 } |
19118c44303f
test length of variable and number of connections
Igor Sysoev <igor@sysoev.ru>
parents:
1002
diff
changeset
|
744 |
2374
7b11f9a1bfe1
rename "lz" to "lr" in variable names
Igor Sysoev <igor@sysoev.ru>
parents:
2313
diff
changeset
|
745 lrcf->burst = burst * 1000; |
980 | 746 |
747 return NGX_CONF_OK; | |
748 } | |
749 | |
750 | |
751 static ngx_int_t | |
2294 | 752 ngx_http_limit_req_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 | |
2294 | 764 *h = ngx_http_limit_req_handler; |
980 | 765 |
766 return NGX_OK; | |
767 } |