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