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