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