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