Mercurial > hg > nginx
annotate src/http/ngx_http_upstream_round_robin.c @ 4795:ad072e83f2a9 stable-1.2
Merge of r4757: debugging messages in limit_conn.
Fixed debugging messages to account that limit_zone was renamed to limit_conn.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Mon, 06 Aug 2012 16:19:35 +0000 |
parents | 0141b4aec0e4 |
children | 3795aeb360fb |
rev | line source |
---|---|
884 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4412 | 4 * Copyright (C) Nginx, Inc. |
884 | 5 */ |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_http.h> | |
11 | |
12 | |
1956
cb8c0c8e0c27
use ngx_int_t in ngx_sort() callback
Igor Sysoev <igor@sysoev.ru>
parents:
1887
diff
changeset
|
13 static ngx_int_t ngx_http_upstream_cmp_servers(const void *one, |
cb8c0c8e0c27
use ngx_int_t in ngx_sort() callback
Igor Sysoev <igor@sysoev.ru>
parents:
1887
diff
changeset
|
14 const void *two); |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
15 static ngx_http_upstream_rr_peer_t *ngx_http_upstream_get_peer( |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
16 ngx_http_upstream_rr_peer_data_t *rrp); |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
17 |
3964
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
18 #if (NGX_HTTP_SSL) |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
19 |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
20 static ngx_int_t ngx_http_upstream_empty_set_session(ngx_peer_connection_t *pc, |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
21 void *data); |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
22 static void ngx_http_upstream_empty_save_session(ngx_peer_connection_t *pc, |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
23 void *data); |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
24 |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
25 #endif |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
26 |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
27 |
884 | 28 ngx_int_t |
29 ngx_http_upstream_init_round_robin(ngx_conf_t *cf, | |
30 ngx_http_upstream_srv_conf_t *us) | |
31 { | |
32 ngx_url_t u; | |
4724
0141b4aec0e4
Merge of r4655, r4656, r4657, r4695, r4696: upstream changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4667
diff
changeset
|
33 ngx_uint_t i, j, n, w; |
884 | 34 ngx_http_upstream_server_t *server; |
1378 | 35 ngx_http_upstream_rr_peers_t *peers, *backup; |
884 | 36 |
37 us->peer.init = ngx_http_upstream_init_round_robin_peer; | |
38 | |
39 if (us->servers) { | |
40 server = us->servers->elts; | |
41 | |
1378 | 42 n = 0; |
4724
0141b4aec0e4
Merge of r4655, r4656, r4657, r4695, r4696: upstream changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4667
diff
changeset
|
43 w = 0; |
1378 | 44 |
884 | 45 for (i = 0; i < us->servers->nelts; i++) { |
1378 | 46 if (server[i].backup) { |
47 continue; | |
48 } | |
49 | |
884 | 50 n += server[i].naddrs; |
4724
0141b4aec0e4
Merge of r4655, r4656, r4657, r4695, r4696: upstream changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4667
diff
changeset
|
51 w += server[i].naddrs * server[i].weight; |
884 | 52 } |
53 | |
4569
1db899642518
Upstream: reject upstreams without normal servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
54 if (n == 0) { |
1db899642518
Upstream: reject upstreams without normal servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
55 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, |
1db899642518
Upstream: reject upstreams without normal servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
56 "no servers in upstream \"%V\" in %s:%ui", |
1db899642518
Upstream: reject upstreams without normal servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
57 &us->host, us->file_name, us->line); |
1db899642518
Upstream: reject upstreams without normal servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
58 return NGX_ERROR; |
1db899642518
Upstream: reject upstreams without normal servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
59 } |
1db899642518
Upstream: reject upstreams without normal servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4412
diff
changeset
|
60 |
884 | 61 peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t) |
62 + sizeof(ngx_http_upstream_rr_peer_t) * (n - 1)); | |
63 if (peers == NULL) { | |
64 return NGX_ERROR; | |
65 } | |
66 | |
1378 | 67 peers->single = (n == 1); |
884 | 68 peers->number = n; |
4724
0141b4aec0e4
Merge of r4655, r4656, r4657, r4695, r4696: upstream changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4667
diff
changeset
|
69 peers->weighted = (w != n); |
0141b4aec0e4
Merge of r4655, r4656, r4657, r4695, r4696: upstream changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4667
diff
changeset
|
70 peers->total_weight = w; |
884 | 71 peers->name = &us->host; |
72 | |
73 n = 0; | |
74 | |
75 for (i = 0; i < us->servers->nelts; i++) { | |
76 for (j = 0; j < server[i].naddrs; j++) { | |
1378 | 77 if (server[i].backup) { |
78 continue; | |
79 } | |
80 | |
884 | 81 peers->peer[n].sockaddr = server[i].addrs[j].sockaddr; |
82 peers->peer[n].socklen = server[i].addrs[j].socklen; | |
83 peers->peer[n].name = server[i].addrs[j].name; | |
84 peers->peer[n].max_fails = server[i].max_fails; | |
85 peers->peer[n].fail_timeout = server[i].fail_timeout; | |
86 peers->peer[n].down = server[i].down; | |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
87 peers->peer[n].weight = server[i].weight; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
88 peers->peer[n].effective_weight = server[i].weight; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
89 peers->peer[n].current_weight = 0; |
884 | 90 n++; |
91 } | |
92 } | |
93 | |
94 us->peer.data = peers; | |
95 | |
1379 | 96 ngx_sort(&peers->peer[0], (size_t) n, |
97 sizeof(ngx_http_upstream_rr_peer_t), | |
98 ngx_http_upstream_cmp_servers); | |
99 | |
1378 | 100 /* backup servers */ |
101 | |
102 n = 0; | |
4724
0141b4aec0e4
Merge of r4655, r4656, r4657, r4695, r4696: upstream changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4667
diff
changeset
|
103 w = 0; |
1378 | 104 |
105 for (i = 0; i < us->servers->nelts; i++) { | |
106 if (!server[i].backup) { | |
107 continue; | |
108 } | |
109 | |
110 n += server[i].naddrs; | |
4724
0141b4aec0e4
Merge of r4655, r4656, r4657, r4695, r4696: upstream changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4667
diff
changeset
|
111 w += server[i].naddrs * server[i].weight; |
1378 | 112 } |
113 | |
114 if (n == 0) { | |
115 return NGX_OK; | |
116 } | |
117 | |
118 backup = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t) | |
119 + sizeof(ngx_http_upstream_rr_peer_t) * (n - 1)); | |
120 if (backup == NULL) { | |
121 return NGX_ERROR; | |
122 } | |
123 | |
124 peers->single = 0; | |
125 backup->single = 0; | |
126 backup->number = n; | |
4724
0141b4aec0e4
Merge of r4655, r4656, r4657, r4695, r4696: upstream changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4667
diff
changeset
|
127 backup->weighted = (w != n); |
0141b4aec0e4
Merge of r4655, r4656, r4657, r4695, r4696: upstream changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4667
diff
changeset
|
128 backup->total_weight = w; |
1378 | 129 backup->name = &us->host; |
130 | |
131 n = 0; | |
132 | |
133 for (i = 0; i < us->servers->nelts; i++) { | |
134 for (j = 0; j < server[i].naddrs; j++) { | |
135 if (!server[i].backup) { | |
136 continue; | |
137 } | |
138 | |
139 backup->peer[n].sockaddr = server[i].addrs[j].sockaddr; | |
140 backup->peer[n].socklen = server[i].addrs[j].socklen; | |
141 backup->peer[n].name = server[i].addrs[j].name; | |
142 backup->peer[n].weight = server[i].weight; | |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
143 backup->peer[n].effective_weight = server[i].weight; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
144 backup->peer[n].current_weight = 0; |
1378 | 145 backup->peer[n].max_fails = server[i].max_fails; |
146 backup->peer[n].fail_timeout = server[i].fail_timeout; | |
147 backup->peer[n].down = server[i].down; | |
148 n++; | |
149 } | |
150 } | |
151 | |
152 peers->next = backup; | |
153 | |
1379 | 154 ngx_sort(&backup->peer[0], (size_t) n, |
155 sizeof(ngx_http_upstream_rr_peer_t), | |
156 ngx_http_upstream_cmp_servers); | |
157 | |
884 | 158 return NGX_OK; |
159 } | |
160 | |
161 | |
162 /* an upstream implicitly defined by proxy_pass, etc. */ | |
163 | |
906 | 164 if (us->port == 0 && us->default_port == 0) { |
165 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
166 "no port in upstream \"%V\" in %s:%ui", | |
1489
56f1ea5baabb
u_char* is enough to keep file name
Igor Sysoev <igor@sysoev.ru>
parents:
1465
diff
changeset
|
167 &us->host, us->file_name, us->line); |
906 | 168 return NGX_ERROR; |
169 } | |
170 | |
884 | 171 ngx_memzero(&u, sizeof(ngx_url_t)); |
172 | |
173 u.host = us->host; | |
916 | 174 u.port = (in_port_t) (us->port ? us->port : us->default_port); |
884 | 175 |
1559
fe11e2a3946d
use pool instead of ngx_conf_t
Igor Sysoev <igor@sysoev.ru>
parents:
1556
diff
changeset
|
176 if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) { |
884 | 177 if (u.err) { |
178 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
896
f247db60fc85
fix fastcgi and memcached upstreams
Igor Sysoev <igor@sysoev.ru>
parents:
884
diff
changeset
|
179 "%s in upstream \"%V\" in %s:%ui", |
1489
56f1ea5baabb
u_char* is enough to keep file name
Igor Sysoev <igor@sysoev.ru>
parents:
1465
diff
changeset
|
180 u.err, &us->host, us->file_name, us->line); |
884 | 181 } |
182 | |
183 return NGX_ERROR; | |
184 } | |
185 | |
186 n = u.naddrs; | |
187 | |
188 peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t) | |
189 + sizeof(ngx_http_upstream_rr_peer_t) * (n - 1)); | |
190 if (peers == NULL) { | |
191 return NGX_ERROR; | |
192 } | |
193 | |
1378 | 194 peers->single = (n == 1); |
884 | 195 peers->number = n; |
4724
0141b4aec0e4
Merge of r4655, r4656, r4657, r4695, r4696: upstream changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4667
diff
changeset
|
196 peers->weighted = 0; |
0141b4aec0e4
Merge of r4655, r4656, r4657, r4695, r4696: upstream changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4667
diff
changeset
|
197 peers->total_weight = n; |
884 | 198 peers->name = &us->host; |
199 | |
200 for (i = 0; i < u.naddrs; i++) { | |
1564 | 201 peers->peer[i].sockaddr = u.addrs[i].sockaddr; |
202 peers->peer[i].socklen = u.addrs[i].socklen; | |
203 peers->peer[i].name = u.addrs[i].name; | |
204 peers->peer[i].weight = 1; | |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
205 peers->peer[i].effective_weight = 1; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
206 peers->peer[i].current_weight = 0; |
1564 | 207 peers->peer[i].max_fails = 1; |
208 peers->peer[i].fail_timeout = 10; | |
884 | 209 } |
210 | |
211 us->peer.data = peers; | |
212 | |
1378 | 213 /* implicitly defined upstream has no backup servers */ |
214 | |
884 | 215 return NGX_OK; |
216 } | |
217 | |
218 | |
1956
cb8c0c8e0c27
use ngx_int_t in ngx_sort() callback
Igor Sysoev <igor@sysoev.ru>
parents:
1887
diff
changeset
|
219 static ngx_int_t |
1379 | 220 ngx_http_upstream_cmp_servers(const void *one, const void *two) |
221 { | |
222 ngx_http_upstream_rr_peer_t *first, *second; | |
223 | |
224 first = (ngx_http_upstream_rr_peer_t *) one; | |
225 second = (ngx_http_upstream_rr_peer_t *) two; | |
226 | |
227 return (first->weight < second->weight); | |
228 } | |
229 | |
230 | |
884 | 231 ngx_int_t |
232 ngx_http_upstream_init_round_robin_peer(ngx_http_request_t *r, | |
233 ngx_http_upstream_srv_conf_t *us) | |
234 { | |
235 ngx_uint_t n; | |
236 ngx_http_upstream_rr_peer_data_t *rrp; | |
237 | |
238 rrp = r->upstream->peer.data; | |
239 | |
240 if (rrp == NULL) { | |
241 rrp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_rr_peer_data_t)); | |
242 if (rrp == NULL) { | |
243 return NGX_ERROR; | |
244 } | |
245 | |
246 r->upstream->peer.data = rrp; | |
247 } | |
248 | |
249 rrp->peers = us->peer.data; | |
250 rrp->current = 0; | |
251 | |
4011
9d4cbb09ae8b
Upstream: properly allocate memory for tried flags.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4010
diff
changeset
|
252 n = rrp->peers->number; |
9d4cbb09ae8b
Upstream: properly allocate memory for tried flags.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4010
diff
changeset
|
253 |
9d4cbb09ae8b
Upstream: properly allocate memory for tried flags.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4010
diff
changeset
|
254 if (rrp->peers->next && rrp->peers->next->number > n) { |
9d4cbb09ae8b
Upstream: properly allocate memory for tried flags.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4010
diff
changeset
|
255 n = rrp->peers->next->number; |
9d4cbb09ae8b
Upstream: properly allocate memory for tried flags.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4010
diff
changeset
|
256 } |
9d4cbb09ae8b
Upstream: properly allocate memory for tried flags.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4010
diff
changeset
|
257 |
9d4cbb09ae8b
Upstream: properly allocate memory for tried flags.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4010
diff
changeset
|
258 if (n <= 8 * sizeof(uintptr_t)) { |
884 | 259 rrp->tried = &rrp->data; |
260 rrp->data = 0; | |
261 | |
262 } else { | |
4011
9d4cbb09ae8b
Upstream: properly allocate memory for tried flags.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4010
diff
changeset
|
263 n = (n + (8 * sizeof(uintptr_t) - 1)) / (8 * sizeof(uintptr_t)); |
884 | 264 |
265 rrp->tried = ngx_pcalloc(r->pool, n * sizeof(uintptr_t)); | |
266 if (rrp->tried == NULL) { | |
267 return NGX_ERROR; | |
268 } | |
269 } | |
270 | |
271 r->upstream->peer.get = ngx_http_upstream_get_round_robin_peer; | |
272 r->upstream->peer.free = ngx_http_upstream_free_round_robin_peer; | |
273 r->upstream->peer.tries = rrp->peers->number; | |
274 #if (NGX_HTTP_SSL) | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
275 r->upstream->peer.set_session = |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
276 ngx_http_upstream_set_round_robin_peer_session; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
277 r->upstream->peer.save_session = |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
278 ngx_http_upstream_save_round_robin_peer_session; |
884 | 279 #endif |
280 | |
281 return NGX_OK; | |
282 } | |
283 | |
284 | |
285 ngx_int_t | |
1658 | 286 ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r, |
287 ngx_http_upstream_resolved_t *ur) | |
288 { | |
289 u_char *p; | |
290 size_t len; | |
291 ngx_uint_t i, n; | |
292 struct sockaddr_in *sin; | |
293 ngx_http_upstream_rr_peers_t *peers; | |
294 ngx_http_upstream_rr_peer_data_t *rrp; | |
295 | |
296 rrp = r->upstream->peer.data; | |
297 | |
298 if (rrp == NULL) { | |
299 rrp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_rr_peer_data_t)); | |
300 if (rrp == NULL) { | |
301 return NGX_ERROR; | |
302 } | |
303 | |
304 r->upstream->peer.data = rrp; | |
305 } | |
306 | |
307 peers = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peers_t) | |
308 + sizeof(ngx_http_upstream_rr_peer_t) * (ur->naddrs - 1)); | |
309 if (peers == NULL) { | |
310 return NGX_ERROR; | |
311 } | |
312 | |
313 peers->single = (ur->naddrs == 1); | |
314 peers->number = ur->naddrs; | |
315 peers->name = &ur->host; | |
316 | |
2422
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
317 if (ur->sockaddr) { |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
318 peers->peer[0].sockaddr = ur->sockaddr; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
319 peers->peer[0].socklen = ur->socklen; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
320 peers->peer[0].name = ur->host; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
321 peers->peer[0].weight = 1; |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
322 peers->peer[0].effective_weight = 1; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
323 peers->peer[0].current_weight = 0; |
2422
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
324 peers->peer[0].max_fails = 1; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
325 peers->peer[0].fail_timeout = 10; |
1658 | 326 |
2422
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
327 } else { |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
328 |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
329 for (i = 0; i < ur->naddrs; i++) { |
1658 | 330 |
2422
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
331 len = NGX_INET_ADDRSTRLEN + sizeof(":65536") - 1; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
332 |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
333 p = ngx_pnalloc(r->pool, len); |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
334 if (p == NULL) { |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
335 return NGX_ERROR; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
336 } |
1658 | 337 |
2422
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
338 len = ngx_inet_ntop(AF_INET, &ur->addrs[i], p, NGX_INET_ADDRSTRLEN); |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
339 len = ngx_sprintf(&p[len], ":%d", ur->port) - p; |
1658 | 340 |
2422
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
341 sin = ngx_pcalloc(r->pool, sizeof(struct sockaddr_in)); |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
342 if (sin == NULL) { |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
343 return NGX_ERROR; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
344 } |
1658 | 345 |
2422
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
346 sin->sin_family = AF_INET; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
347 sin->sin_port = htons(ur->port); |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
348 sin->sin_addr.s_addr = ur->addrs[i]; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
349 |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
350 peers->peer[i].sockaddr = (struct sockaddr *) sin; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
351 peers->peer[i].socklen = sizeof(struct sockaddr_in); |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
352 peers->peer[i].name.len = len; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
353 peers->peer[i].name.data = p; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
354 peers->peer[i].weight = 1; |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
355 peers->peer[i].effective_weight = 1; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
356 peers->peer[i].current_weight = 0; |
2422
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
357 peers->peer[i].max_fails = 1; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
358 peers->peer[i].fail_timeout = 10; |
7af1e5fe102c
variable support for unix sockets in fastcgi_pass and proxy_pass
Igor Sysoev <igor@sysoev.ru>
parents:
2204
diff
changeset
|
359 } |
1658 | 360 } |
361 | |
362 rrp->peers = peers; | |
363 rrp->current = 0; | |
364 | |
365 if (rrp->peers->number <= 8 * sizeof(uintptr_t)) { | |
366 rrp->tried = &rrp->data; | |
367 rrp->data = 0; | |
368 | |
369 } else { | |
370 n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1)) | |
371 / (8 * sizeof(uintptr_t)); | |
372 | |
373 rrp->tried = ngx_pcalloc(r->pool, n * sizeof(uintptr_t)); | |
374 if (rrp->tried == NULL) { | |
375 return NGX_ERROR; | |
376 } | |
377 } | |
378 | |
379 r->upstream->peer.get = ngx_http_upstream_get_round_robin_peer; | |
380 r->upstream->peer.free = ngx_http_upstream_free_round_robin_peer; | |
381 r->upstream->peer.tries = rrp->peers->number; | |
382 #if (NGX_HTTP_SSL) | |
3964
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
383 r->upstream->peer.set_session = ngx_http_upstream_empty_set_session; |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
384 r->upstream->peer.save_session = ngx_http_upstream_empty_save_session; |
1658 | 385 #endif |
386 | |
387 return NGX_OK; | |
388 } | |
389 | |
390 | |
391 ngx_int_t | |
884 | 392 ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data) |
393 { | |
394 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
395 | |
1378 | 396 ngx_int_t rc; |
397 ngx_uint_t i, n; | |
398 ngx_connection_t *c; | |
399 ngx_http_upstream_rr_peer_t *peer; | |
400 ngx_http_upstream_rr_peers_t *peers; | |
884 | 401 |
402 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
403 "get rr peer, try: %ui", pc->tries); | |
404 | |
405 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
406 | |
407 if (rrp->peers->last_cached) { | |
408 | |
409 /* cached connection */ | |
410 | |
411 c = rrp->peers->cached[rrp->peers->last_cached]; | |
412 rrp->peers->last_cached--; | |
413 | |
414 /* ngx_unlock_mutex(ppr->peers->mutex); */ | |
415 | |
416 #if (NGX_THREADS) | |
417 c->read->lock = c->read->own_lock; | |
418 c->write->lock = c->write->own_lock; | |
419 #endif | |
420 | |
421 pc->connection = c; | |
422 pc->cached = 1; | |
423 | |
424 return NGX_OK; | |
425 } | |
426 | |
427 pc->cached = 0; | |
428 pc->connection = NULL; | |
429 | |
1378 | 430 if (rrp->peers->single) { |
884 | 431 peer = &rrp->peers->peer[0]; |
432 | |
433 } else { | |
434 | |
435 /* there are several peers */ | |
436 | |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
437 peer = ngx_http_upstream_get_peer(rrp); |
884 | 438 |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
439 if (peer == NULL) { |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
440 goto failed; |
884 | 441 } |
442 | |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
443 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
444 "get rr peer, current: %ui %i", |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
445 rrp->current, peer->current_weight); |
884 | 446 } |
447 | |
448 pc->sockaddr = peer->sockaddr; | |
449 pc->socklen = peer->socklen; | |
450 pc->name = &peer->name; | |
451 | |
452 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
453 | |
1378 | 454 if (pc->tries == 1 && rrp->peers->next) { |
455 pc->tries += rrp->peers->next->number; | |
456 } | |
457 | |
884 | 458 return NGX_OK; |
459 | |
460 failed: | |
461 | |
1378 | 462 peers = rrp->peers; |
463 | |
464 if (peers->next) { | |
465 | |
466 /* ngx_unlock_mutex(peers->mutex); */ | |
467 | |
468 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, "backup servers"); | |
469 | |
470 rrp->peers = peers->next; | |
471 pc->tries = rrp->peers->number; | |
472 | |
473 n = rrp->peers->number / (8 * sizeof(uintptr_t)) + 1; | |
1389 | 474 for (i = 0; i < n; i++) { |
1378 | 475 rrp->tried[i] = 0; |
476 } | |
477 | |
478 rc = ngx_http_upstream_get_round_robin_peer(pc, rrp); | |
479 | |
480 if (rc != NGX_BUSY) { | |
481 return rc; | |
482 } | |
483 | |
484 /* ngx_lock_mutex(peers->mutex); */ | |
485 } | |
486 | |
884 | 487 /* all peers failed, mark them as live for quick recovery */ |
488 | |
1378 | 489 for (i = 0; i < peers->number; i++) { |
490 peers->peer[i].fails = 0; | |
884 | 491 } |
492 | |
1378 | 493 /* ngx_unlock_mutex(peers->mutex); */ |
884 | 494 |
1378 | 495 pc->name = peers->name; |
884 | 496 |
497 return NGX_BUSY; | |
498 } | |
499 | |
500 | |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
501 static ngx_http_upstream_rr_peer_t * |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
502 ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp) |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
503 { |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
504 time_t now; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
505 uintptr_t m; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
506 ngx_int_t total; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
507 ngx_uint_t i, n; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
508 ngx_http_upstream_rr_peer_t *peer, *best; |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
509 |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
510 now = ngx_time(); |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
511 |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
512 best = NULL; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
513 total = 0; |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
514 |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
515 for (i = 0; i < rrp->peers->number; i++) { |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
516 |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
517 n = i / (8 * sizeof(uintptr_t)); |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
518 m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t)); |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
519 |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
520 if (rrp->tried[n] & m) { |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
521 continue; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
522 } |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
523 |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
524 peer = &rrp->peers->peer[i]; |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
525 |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
526 if (peer->down) { |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
527 continue; |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
528 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
529 |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
530 if (peer->max_fails |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
531 && peer->fails >= peer->max_fails |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
532 && now - peer->checked <= peer->fail_timeout) |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
533 { |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
534 continue; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
535 } |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
536 |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
537 peer->current_weight += peer->effective_weight; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
538 total += peer->effective_weight; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
539 |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
540 if (peer->effective_weight < peer->weight) { |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
541 peer->effective_weight++; |
4010
74a93d3fdd85
Fixing cpu hog with all upstream servers marked "down".
Maxim Dounin <mdounin@mdounin.ru>
parents:
3964
diff
changeset
|
542 } |
74a93d3fdd85
Fixing cpu hog with all upstream servers marked "down".
Maxim Dounin <mdounin@mdounin.ru>
parents:
3964
diff
changeset
|
543 |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
544 if (best == NULL || peer->current_weight > best->current_weight) { |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
545 best = peer; |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
546 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
547 } |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
548 |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
549 if (best == NULL) { |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
550 return NULL; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
551 } |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
552 |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
553 i = best - &rrp->peers->peer[0]; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
554 |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
555 rrp->current = i; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
556 |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
557 n = i / (8 * sizeof(uintptr_t)); |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
558 m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t)); |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
559 |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
560 rrp->tried[n] |= m; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
561 |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
562 best->current_weight -= total; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
563 best->checked = now; |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
564 |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
565 return best; |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
566 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
567 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
568 |
884 | 569 void |
570 ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data, | |
571 ngx_uint_t state) | |
572 { | |
573 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
574 | |
575 time_t now; | |
576 ngx_http_upstream_rr_peer_t *peer; | |
577 | |
578 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
579 "free rr peer %ui %ui", pc->tries, state); | |
580 | |
581 if (state == 0 && pc->tries == 0) { | |
582 return; | |
583 } | |
584 | |
585 /* TODO: NGX_PEER_KEEPALIVE */ | |
586 | |
1378 | 587 if (rrp->peers->single) { |
884 | 588 pc->tries = 0; |
589 return; | |
590 } | |
591 | |
4207
4fc91bae6f83
Better recheck of dead upstream servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4011
diff
changeset
|
592 peer = &rrp->peers->peer[rrp->current]; |
4fc91bae6f83
Better recheck of dead upstream servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4011
diff
changeset
|
593 |
884 | 594 if (state & NGX_PEER_FAILED) { |
595 now = ngx_time(); | |
596 | |
597 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
598 | |
599 peer->fails++; | |
600 peer->accessed = now; | |
4207
4fc91bae6f83
Better recheck of dead upstream servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4011
diff
changeset
|
601 peer->checked = now; |
884 | 602 |
2204
70a2bcc7e307
fix divide by zero if max_fails=0
Igor Sysoev <igor@sysoev.ru>
parents:
2197
diff
changeset
|
603 if (peer->max_fails) { |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
604 peer->effective_weight -= peer->weight / peer->max_fails; |
2204
70a2bcc7e307
fix divide by zero if max_fails=0
Igor Sysoev <igor@sysoev.ru>
parents:
2197
diff
changeset
|
605 } |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
606 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
607 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
608 "free rr peer failed: %ui %i", |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
609 rrp->current, peer->effective_weight); |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
610 |
4667
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
611 if (peer->effective_weight < 0) { |
d05ab8793a69
Merge of r4622, r4623: balancing changes.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4569
diff
changeset
|
612 peer->effective_weight = 0; |
884 | 613 } |
614 | |
615 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
4207
4fc91bae6f83
Better recheck of dead upstream servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4011
diff
changeset
|
616 |
4fc91bae6f83
Better recheck of dead upstream servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4011
diff
changeset
|
617 } else { |
4fc91bae6f83
Better recheck of dead upstream servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4011
diff
changeset
|
618 |
4fc91bae6f83
Better recheck of dead upstream servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4011
diff
changeset
|
619 /* mark peer live if check passed */ |
4fc91bae6f83
Better recheck of dead upstream servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4011
diff
changeset
|
620 |
4fc91bae6f83
Better recheck of dead upstream servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4011
diff
changeset
|
621 if (peer->accessed < peer->checked) { |
4fc91bae6f83
Better recheck of dead upstream servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4011
diff
changeset
|
622 peer->fails = 0; |
4fc91bae6f83
Better recheck of dead upstream servers.
Maxim Dounin <mdounin@mdounin.ru>
parents:
4011
diff
changeset
|
623 } |
884 | 624 } |
625 | |
626 if (pc->tries) { | |
627 pc->tries--; | |
628 } | |
629 | |
630 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
631 } | |
632 | |
633 | |
634 #if (NGX_HTTP_SSL) | |
635 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
636 ngx_int_t |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
637 ngx_http_upstream_set_round_robin_peer_session(ngx_peer_connection_t *pc, |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
638 void *data) |
884 | 639 { |
640 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
641 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
642 ngx_int_t rc; |
884 | 643 ngx_ssl_session_t *ssl_session; |
644 ngx_http_upstream_rr_peer_t *peer; | |
645 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
646 peer = &rrp->peers->peer[rrp->current]; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
647 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
648 /* TODO: threads only mutex */ |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
649 /* ngx_lock_mutex(rrp->peers->mutex); */ |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
650 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
651 ssl_session = peer->ssl_session; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
652 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
653 rc = ngx_ssl_set_session(pc->connection, ssl_session); |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
654 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
655 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
1443 | 656 "set session: %p:%d", |
657 ssl_session, ssl_session ? ssl_session->references : 0); | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
658 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
659 /* ngx_unlock_mutex(rrp->peers->mutex); */ |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
660 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
661 return rc; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
662 } |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
663 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
664 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
665 void |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
666 ngx_http_upstream_save_round_robin_peer_session(ngx_peer_connection_t *pc, |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
667 void *data) |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
668 { |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
669 ngx_http_upstream_rr_peer_data_t *rrp = data; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
670 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
671 ngx_ssl_session_t *old_ssl_session, *ssl_session; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
672 ngx_http_upstream_rr_peer_t *peer; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
673 |
884 | 674 ssl_session = ngx_ssl_get_session(pc->connection); |
675 | |
676 if (ssl_session == NULL) { | |
677 return; | |
678 } | |
679 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
680 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
1443 | 681 "save session: %p:%d", ssl_session, ssl_session->references); |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
682 |
884 | 683 peer = &rrp->peers->peer[rrp->current]; |
684 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
685 /* TODO: threads only mutex */ |
884 | 686 /* ngx_lock_mutex(rrp->peers->mutex); */ |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
687 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
688 old_ssl_session = peer->ssl_session; |
884 | 689 peer->ssl_session = ssl_session; |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
690 |
884 | 691 /* ngx_unlock_mutex(rrp->peers->mutex); */ |
692 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
693 if (old_ssl_session) { |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
694 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
695 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
1443 | 696 "old session: %p:%d", |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
697 old_ssl_session, old_ssl_session->references); |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
698 |
884 | 699 /* TODO: may block */ |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
700 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
701 ngx_ssl_free_session(old_ssl_session); |
884 | 702 } |
703 } | |
704 | |
3964
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
705 |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
706 static ngx_int_t |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
707 ngx_http_upstream_empty_set_session(ngx_peer_connection_t *pc, void *data) |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
708 { |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
709 return NGX_OK; |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
710 } |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
711 |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
712 |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
713 static void |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
714 ngx_http_upstream_empty_save_session(ngx_peer_connection_t *pc, void *data) |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
715 { |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
716 return; |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
717 } |
ff127ba3b091
do not try to reuse and save a SSL session for a peer created on the fly
Igor Sysoev <igor@sysoev.ru>
parents:
2422
diff
changeset
|
718 |
884 | 719 #endif |