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