Mercurial > hg > nginx-quic
annotate src/http/ngx_http_upstream_round_robin.c @ 1579:aa1835ad46ba
style fix
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Wed, 17 Oct 2007 13:21:20 +0000 |
parents | 771c4071f5f6 |
children | 13f8dec720b5 |
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 | |
250 ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data) | |
251 { | |
252 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
253 | |
1378 | 254 time_t now; |
255 uintptr_t m; | |
256 ngx_int_t rc; | |
257 ngx_uint_t i, n; | |
258 ngx_connection_t *c; | |
259 ngx_http_upstream_rr_peer_t *peer; | |
260 ngx_http_upstream_rr_peers_t *peers; | |
884 | 261 |
262 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
263 "get rr peer, try: %ui", pc->tries); | |
264 | |
265 now = ngx_time(); | |
266 | |
267 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
268 | |
269 if (rrp->peers->last_cached) { | |
270 | |
271 /* cached connection */ | |
272 | |
273 c = rrp->peers->cached[rrp->peers->last_cached]; | |
274 rrp->peers->last_cached--; | |
275 | |
276 /* ngx_unlock_mutex(ppr->peers->mutex); */ | |
277 | |
278 #if (NGX_THREADS) | |
279 c->read->lock = c->read->own_lock; | |
280 c->write->lock = c->write->own_lock; | |
281 #endif | |
282 | |
283 pc->connection = c; | |
284 pc->cached = 1; | |
285 | |
286 return NGX_OK; | |
287 } | |
288 | |
289 pc->cached = 0; | |
290 pc->connection = NULL; | |
291 | |
1378 | 292 if (rrp->peers->single) { |
884 | 293 peer = &rrp->peers->peer[0]; |
294 | |
295 } else { | |
296 | |
297 /* there are several peers */ | |
298 | |
299 if (pc->tries == rrp->peers->number) { | |
300 | |
301 /* it's a first try - get a current peer */ | |
302 | |
1419
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
303 i = pc->tries; |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
304 |
884 | 305 for ( ;; ) { |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
306 rrp->current = ngx_http_upstream_get_peer(rrp->peers); |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
307 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
308 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
309 "get rr peer, current: %ui %i", |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
310 rrp->current, |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
311 rrp->peers->peer[rrp->current].current_weight); |
884 | 312 |
313 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
|
314 m = (uintptr_t) 1 << rrp->current % (8 * sizeof(uintptr_t)); |
884 | 315 |
316 if (!(rrp->tried[n] & m)) { | |
317 peer = &rrp->peers->peer[rrp->current]; | |
318 | |
319 if (!peer->down) { | |
320 | |
321 if (peer->max_fails == 0 | |
322 || peer->fails < peer->max_fails) | |
323 { | |
324 break; | |
325 } | |
326 | |
327 if (now - peer->accessed > peer->fail_timeout) { | |
328 peer->fails = 0; | |
329 break; | |
330 } | |
331 | |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
332 peer->current_weight = 0; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
333 |
884 | 334 } else { |
335 rrp->tried[n] |= m; | |
336 } | |
337 | |
338 pc->tries--; | |
339 } | |
340 | |
1419
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
341 if (pc->tries == 0) { |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
342 goto failed; |
884 | 343 } |
344 | |
1419
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
345 if (--i == 0) { |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
346 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
|
347 "round robin upstream stuck on %ui tries", |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
348 pc->tries); |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
349 goto failed; |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
350 } |
884 | 351 } |
352 | |
353 peer->current_weight--; | |
354 | |
355 } else { | |
1419
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
356 |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
357 i = pc->tries; |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
358 |
884 | 359 for ( ;; ) { |
360 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
|
361 m = (uintptr_t) 1 << rrp->current % (8 * sizeof(uintptr_t)); |
884 | 362 |
363 if (!(rrp->tried[n] & m)) { | |
364 | |
365 peer = &rrp->peers->peer[rrp->current]; | |
366 | |
367 if (!peer->down) { | |
368 | |
369 if (peer->max_fails == 0 | |
370 || peer->fails < peer->max_fails) | |
371 { | |
372 break; | |
373 } | |
374 | |
375 if (now - peer->accessed > peer->fail_timeout) { | |
376 peer->fails = 0; | |
377 break; | |
378 } | |
379 | |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
380 peer->current_weight = 0; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
381 |
884 | 382 } else { |
383 rrp->tried[n] |= m; | |
384 } | |
385 | |
386 pc->tries--; | |
387 } | |
388 | |
389 rrp->current++; | |
390 | |
391 if (rrp->current >= rrp->peers->number) { | |
392 rrp->current = 0; | |
393 } | |
394 | |
1419
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
395 if (pc->tries == 0) { |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
396 goto failed; |
884 | 397 } |
398 | |
1419
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
399 if (--i == 0) { |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
400 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
|
401 "round robin upstream stuck on %ui tries", |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
402 pc->tries); |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
403 goto failed; |
79f033a89dd4
add guard code to prevent endless loop
Igor Sysoev <igor@sysoev.ru>
parents:
1389
diff
changeset
|
404 } |
884 | 405 } |
406 | |
407 peer->current_weight--; | |
408 } | |
409 | |
410 rrp->tried[n] |= m; | |
411 } | |
412 | |
413 pc->sockaddr = peer->sockaddr; | |
414 pc->socklen = peer->socklen; | |
415 pc->name = &peer->name; | |
416 | |
417 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
418 | |
1378 | 419 if (pc->tries == 1 && rrp->peers->next) { |
420 pc->tries += rrp->peers->next->number; | |
421 | |
422 n = rrp->peers->next->number / (8 * sizeof(uintptr_t)) + 1; | |
1389 | 423 for (i = 0; i < n; i++) { |
1378 | 424 rrp->tried[i] = 0; |
425 } | |
426 } | |
427 | |
884 | 428 return NGX_OK; |
429 | |
430 failed: | |
431 | |
1378 | 432 peers = rrp->peers; |
433 | |
434 if (peers->next) { | |
435 | |
436 /* ngx_unlock_mutex(peers->mutex); */ | |
437 | |
438 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, "backup servers"); | |
439 | |
440 rrp->peers = peers->next; | |
441 pc->tries = rrp->peers->number; | |
442 | |
443 n = rrp->peers->number / (8 * sizeof(uintptr_t)) + 1; | |
1389 | 444 for (i = 0; i < n; i++) { |
1378 | 445 rrp->tried[i] = 0; |
446 } | |
447 | |
448 rc = ngx_http_upstream_get_round_robin_peer(pc, rrp); | |
449 | |
450 if (rc != NGX_BUSY) { | |
451 return rc; | |
452 } | |
453 | |
454 /* ngx_lock_mutex(peers->mutex); */ | |
455 } | |
456 | |
884 | 457 /* all peers failed, mark them as live for quick recovery */ |
458 | |
1378 | 459 for (i = 0; i < peers->number; i++) { |
460 peers->peer[i].fails = 0; | |
884 | 461 } |
462 | |
1378 | 463 /* ngx_unlock_mutex(peers->mutex); */ |
884 | 464 |
1378 | 465 pc->name = peers->name; |
884 | 466 |
467 return NGX_BUSY; | |
468 } | |
469 | |
470 | |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
471 static ngx_uint_t |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
472 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
|
473 { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
474 ngx_uint_t i, n; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
475 ngx_http_upstream_rr_peer_t *peer; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
476 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
477 peer = &peers->peer[0]; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
478 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
479 for ( ;; ) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
480 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
481 for (i = 0; i < peers->number; i++) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
482 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
483 if (peer[i].current_weight <= 0) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
484 continue; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
485 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
486 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
487 n = i; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
488 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
489 while (i < peers->number - 1) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
490 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
491 i++; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
492 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
493 if (peer[i].current_weight <= 0) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
494 continue; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
495 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
496 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
497 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
|
498 > peer[n].weight * 1000 / peer[i].weight) |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
499 { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
500 return n; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
501 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
502 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
503 n = i; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
504 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
505 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
506 if (peer[i].current_weight > 0) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
507 n = i; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
508 } |
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 return n; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
511 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
512 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
513 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
|
514 peer[i].current_weight += peer[i].weight; |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
515 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
516 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
517 } |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
518 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
519 |
884 | 520 void |
521 ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data, | |
522 ngx_uint_t state) | |
523 { | |
524 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
525 | |
526 time_t now; | |
527 ngx_http_upstream_rr_peer_t *peer; | |
528 | |
529 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
530 "free rr peer %ui %ui", pc->tries, state); | |
531 | |
532 if (state == 0 && pc->tries == 0) { | |
533 return; | |
534 } | |
535 | |
536 /* TODO: NGX_PEER_KEEPALIVE */ | |
537 | |
1378 | 538 if (rrp->peers->single) { |
884 | 539 pc->tries = 0; |
540 return; | |
541 } | |
542 | |
543 if (state & NGX_PEER_FAILED) { | |
544 now = ngx_time(); | |
545 | |
546 peer = &rrp->peers->peer[rrp->current]; | |
547 | |
548 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
549 | |
550 peer->fails++; | |
551 peer->accessed = now; | |
552 | |
1344
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
553 peer->current_weight -= peer->weight / peer->max_fails; |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
554 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
555 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
556 "free rr peer failed: %ui %i", |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
557 rrp->current, peer->current_weight); |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
558 |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
559 if (peer->current_weight < 0) { |
8f5b5641966c
fair upstream weight balancer
Igor Sysoev <igor@sysoev.ru>
parents:
1284
diff
changeset
|
560 peer->current_weight = 0; |
884 | 561 } |
562 | |
563 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
564 } | |
565 | |
566 rrp->current++; | |
567 | |
568 if (rrp->current >= rrp->peers->number) { | |
569 rrp->current = 0; | |
570 } | |
571 | |
572 if (pc->tries) { | |
573 pc->tries--; | |
574 } | |
575 | |
576 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
577 } | |
578 | |
579 | |
580 #if (NGX_HTTP_SSL) | |
581 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
582 ngx_int_t |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
583 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
|
584 void *data) |
884 | 585 { |
586 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
587 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
588 ngx_int_t rc; |
884 | 589 ngx_ssl_session_t *ssl_session; |
590 ngx_http_upstream_rr_peer_t *peer; | |
591 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
592 peer = &rrp->peers->peer[rrp->current]; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
593 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
594 /* TODO: threads only mutex */ |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
595 /* ngx_lock_mutex(rrp->peers->mutex); */ |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
596 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
597 ssl_session = peer->ssl_session; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
598 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
599 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
|
600 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
601 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
1443 | 602 "set session: %p:%d", |
603 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
|
604 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
605 /* ngx_unlock_mutex(rrp->peers->mutex); */ |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
606 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
607 return rc; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
608 } |
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 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
611 void |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
612 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
|
613 void *data) |
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 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
|
616 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
617 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
|
618 ngx_http_upstream_rr_peer_t *peer; |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
619 |
884 | 620 ssl_session = ngx_ssl_get_session(pc->connection); |
621 | |
622 if (ssl_session == NULL) { | |
623 return; | |
624 } | |
625 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
626 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
1443 | 627 "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
|
628 |
884 | 629 peer = &rrp->peers->peer[rrp->current]; |
630 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
631 /* TODO: threads only mutex */ |
884 | 632 /* ngx_lock_mutex(rrp->peers->mutex); */ |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
633 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
634 old_ssl_session = peer->ssl_session; |
884 | 635 peer->ssl_session = ssl_session; |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
636 |
884 | 637 /* ngx_unlock_mutex(rrp->peers->mutex); */ |
638 | |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
639 if (old_ssl_session) { |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
640 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
641 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
1443 | 642 "old session: %p:%d", |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
643 old_ssl_session, old_ssl_session->references); |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
644 |
884 | 645 /* TODO: may block */ |
1284
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
646 |
be2b895d31e0
fix segfault when session was freed twice
Igor Sysoev <igor@sysoev.ru>
parents:
1206
diff
changeset
|
647 ngx_ssl_free_session(old_ssl_session); |
884 | 648 } |
649 } | |
650 | |
651 #endif |