Mercurial > hg > nginx-quic
annotate src/http/ngx_http_upstream_round_robin.c @ 1613:fd1fca184e67 stable-0.5
r1547 merge:
omit trailing zero in directory name
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 29 Oct 2007 15:08:01 +0000 |
parents | 9b10ba85b249 |
children | a179755ee1ea |
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 | |
12 ngx_int_t | |
13 ngx_http_upstream_init_round_robin(ngx_conf_t *cf, | |
14 ngx_http_upstream_srv_conf_t *us) | |
15 { | |
16 ngx_url_t u; | |
17 ngx_uint_t i, j, n; | |
18 ngx_http_upstream_server_t *server; | |
19 ngx_http_upstream_rr_peers_t *peers; | |
20 | |
21 us->peer.init = ngx_http_upstream_init_round_robin_peer; | |
22 | |
23 if (us->servers) { | |
24 n = 0; | |
25 server = us->servers->elts; | |
26 | |
27 for (i = 0; i < us->servers->nelts; i++) { | |
28 n += server[i].naddrs; | |
29 } | |
30 | |
31 peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t) | |
32 + sizeof(ngx_http_upstream_rr_peer_t) * (n - 1)); | |
33 if (peers == NULL) { | |
34 return NGX_ERROR; | |
35 } | |
36 | |
37 peers->number = n; | |
38 peers->name = &us->host; | |
39 | |
40 n = 0; | |
41 | |
42 for (i = 0; i < us->servers->nelts; i++) { | |
43 for (j = 0; j < server[i].naddrs; j++) { | |
44 peers->peer[n].sockaddr = server[i].addrs[j].sockaddr; | |
45 peers->peer[n].socklen = server[i].addrs[j].socklen; | |
46 peers->peer[n].name = server[i].addrs[j].name; | |
47 peers->peer[n].weight = server[i].weight; | |
48 peers->peer[n].current_weight = server[i].weight; | |
49 peers->peer[n].max_fails = server[i].max_fails; | |
50 peers->peer[n].fail_timeout = server[i].fail_timeout; | |
51 peers->peer[n].down = server[i].down; | |
52 n++; | |
53 } | |
54 } | |
55 | |
56 us->peer.data = peers; | |
57 | |
58 return NGX_OK; | |
59 } | |
60 | |
61 | |
62 /* an upstream implicitly defined by proxy_pass, etc. */ | |
63 | |
906 | 64 if (us->port == 0 && us->default_port == 0) { |
65 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
66 "no port in upstream \"%V\" in %s:%ui", | |
1533 | 67 &us->host, us->file_name, us->line); |
906 | 68 return NGX_ERROR; |
69 } | |
70 | |
884 | 71 ngx_memzero(&u, sizeof(ngx_url_t)); |
72 | |
73 u.host = us->host; | |
916 | 74 u.port = (in_port_t) (us->port ? us->port : us->default_port); |
884 | 75 |
76 if (ngx_inet_resolve_host(cf, &u) != NGX_OK) { | |
77 if (u.err) { | |
78 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
|
79 "%s in upstream \"%V\" in %s:%ui", |
1533 | 80 u.err, &us->host, us->file_name, us->line); |
884 | 81 } |
82 | |
83 return NGX_ERROR; | |
84 } | |
85 | |
86 n = u.naddrs; | |
87 | |
88 peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t) | |
89 + sizeof(ngx_http_upstream_rr_peer_t) * (n - 1)); | |
90 if (peers == NULL) { | |
91 return NGX_ERROR; | |
92 } | |
93 | |
94 peers->number = n; | |
95 peers->name = &us->host; | |
96 | |
97 n = 0; | |
98 | |
99 for (i = 0; i < u.naddrs; i++) { | |
100 peers->peer[n].sockaddr = u.addrs[i].sockaddr; | |
101 peers->peer[n].socklen = u.addrs[i].socklen; | |
102 peers->peer[n].name = u.addrs[i].name; | |
103 peers->peer[n].weight = 1; | |
104 peers->peer[n].current_weight = 1; | |
105 peers->peer[n].max_fails = 1; | |
106 peers->peer[n].fail_timeout = 10; | |
107 n++; | |
108 } | |
109 | |
110 us->peer.data = peers; | |
111 | |
112 return NGX_OK; | |
113 } | |
114 | |
115 | |
116 ngx_int_t | |
117 ngx_http_upstream_init_round_robin_peer(ngx_http_request_t *r, | |
118 ngx_http_upstream_srv_conf_t *us) | |
119 { | |
120 ngx_uint_t n; | |
121 ngx_http_upstream_rr_peer_data_t *rrp; | |
122 | |
123 rrp = r->upstream->peer.data; | |
124 | |
125 if (rrp == NULL) { | |
126 rrp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_rr_peer_data_t)); | |
127 if (rrp == NULL) { | |
128 return NGX_ERROR; | |
129 } | |
130 | |
131 r->upstream->peer.data = rrp; | |
132 } | |
133 | |
134 rrp->peers = us->peer.data; | |
135 rrp->current = 0; | |
136 | |
137 if (rrp->peers->number <= 8 * sizeof(uintptr_t)) { | |
138 rrp->tried = &rrp->data; | |
139 rrp->data = 0; | |
140 | |
141 } else { | |
142 n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1)) | |
143 / (8 * sizeof(uintptr_t)); | |
144 | |
145 rrp->tried = ngx_pcalloc(r->pool, n * sizeof(uintptr_t)); | |
146 if (rrp->tried == NULL) { | |
147 return NGX_ERROR; | |
148 } | |
149 } | |
150 | |
151 r->upstream->peer.get = ngx_http_upstream_get_round_robin_peer; | |
152 r->upstream->peer.free = ngx_http_upstream_free_round_robin_peer; | |
153 r->upstream->peer.tries = rrp->peers->number; | |
154 #if (NGX_HTTP_SSL) | |
1346 | 155 r->upstream->peer.set_session = |
156 ngx_http_upstream_set_round_robin_peer_session; | |
157 r->upstream->peer.save_session = | |
158 ngx_http_upstream_save_round_robin_peer_session; | |
884 | 159 #endif |
160 | |
161 return NGX_OK; | |
162 } | |
163 | |
164 | |
165 ngx_int_t | |
166 ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data) | |
167 { | |
168 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
169 | |
170 time_t now; | |
171 uintptr_t m; | |
172 ngx_uint_t i, n; | |
173 ngx_connection_t *c; | |
174 ngx_http_upstream_rr_peer_t *peer; | |
175 | |
176 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
177 "get rr peer, try: %ui", pc->tries); | |
178 | |
179 now = ngx_time(); | |
180 | |
181 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
182 | |
183 if (rrp->peers->last_cached) { | |
184 | |
185 /* cached connection */ | |
186 | |
187 c = rrp->peers->cached[rrp->peers->last_cached]; | |
188 rrp->peers->last_cached--; | |
189 | |
190 /* ngx_unlock_mutex(ppr->peers->mutex); */ | |
191 | |
192 #if (NGX_THREADS) | |
193 c->read->lock = c->read->own_lock; | |
194 c->write->lock = c->write->own_lock; | |
195 #endif | |
196 | |
197 pc->connection = c; | |
198 pc->cached = 1; | |
199 | |
200 return NGX_OK; | |
201 } | |
202 | |
203 pc->cached = 0; | |
204 pc->connection = NULL; | |
205 | |
206 if (rrp->peers->number == 1) { | |
207 peer = &rrp->peers->peer[0]; | |
208 | |
209 } else { | |
210 | |
211 /* there are several peers */ | |
212 | |
213 if (pc->tries == rrp->peers->number) { | |
214 | |
215 /* it's a first try - get a current peer */ | |
216 | |
217 for ( ;; ) { | |
218 rrp->current = rrp->peers->current; | |
219 | |
220 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
|
221 m = (uintptr_t) 1 << rrp->current % (8 * sizeof(uintptr_t)); |
884 | 222 |
223 if (!(rrp->tried[n] & m)) { | |
224 peer = &rrp->peers->peer[rrp->current]; | |
225 | |
226 if (!peer->down) { | |
227 | |
228 if (peer->max_fails == 0 | |
229 || peer->fails < peer->max_fails) | |
230 { | |
231 break; | |
232 } | |
233 | |
234 if (now - peer->accessed > peer->fail_timeout) { | |
235 peer->fails = 0; | |
236 break; | |
237 } | |
238 | |
239 } else { | |
240 rrp->tried[n] |= m; | |
241 } | |
242 | |
243 pc->tries--; | |
244 } | |
245 | |
246 rrp->peers->current++; | |
247 | |
248 if (rrp->peers->current >= rrp->peers->number) { | |
249 rrp->peers->current = 0; | |
250 } | |
251 | |
252 if (pc->tries) { | |
253 continue; | |
254 } | |
255 | |
256 goto failed; | |
257 } | |
258 | |
259 peer->current_weight--; | |
260 | |
261 if (peer->current_weight == 0) { | |
262 peer->current_weight = peer->weight; | |
263 | |
264 rrp->peers->current++; | |
265 | |
266 if (rrp->peers->current >= rrp->peers->number) { | |
267 rrp->peers->current = 0; | |
268 } | |
269 } | |
270 | |
271 } else { | |
272 for ( ;; ) { | |
273 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
|
274 m = (uintptr_t) 1 << rrp->current % (8 * sizeof(uintptr_t)); |
884 | 275 |
276 if (!(rrp->tried[n] & m)) { | |
277 | |
278 peer = &rrp->peers->peer[rrp->current]; | |
279 | |
280 if (!peer->down) { | |
281 | |
282 if (peer->max_fails == 0 | |
283 || peer->fails < peer->max_fails) | |
284 { | |
285 break; | |
286 } | |
287 | |
288 if (now - peer->accessed > peer->fail_timeout) { | |
289 peer->fails = 0; | |
290 break; | |
291 } | |
292 | |
293 } else { | |
294 rrp->tried[n] |= m; | |
295 } | |
296 | |
297 pc->tries--; | |
298 } | |
299 | |
300 rrp->current++; | |
301 | |
302 if (rrp->current >= rrp->peers->number) { | |
303 rrp->current = 0; | |
304 } | |
305 | |
306 if (pc->tries) { | |
307 continue; | |
308 } | |
309 | |
310 goto failed; | |
311 } | |
312 | |
313 peer->current_weight--; | |
314 | |
315 if (peer->current_weight == 0) { | |
316 peer->current_weight = peer->weight; | |
317 | |
318 if (rrp->current == rrp->peers->current) { | |
319 rrp->peers->current++; | |
320 | |
321 if (rrp->peers->current >= rrp->peers->number) { | |
322 rrp->peers->current = 0; | |
323 } | |
324 } | |
325 } | |
326 } | |
327 | |
328 rrp->tried[n] |= m; | |
329 } | |
330 | |
331 pc->sockaddr = peer->sockaddr; | |
332 pc->socklen = peer->socklen; | |
333 pc->name = &peer->name; | |
334 | |
335 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
336 | |
337 return NGX_OK; | |
338 | |
339 failed: | |
340 | |
341 /* all peers failed, mark them as live for quick recovery */ | |
342 | |
343 for (i = 0; i < rrp->peers->number; i++) { | |
344 rrp->peers->peer[i].fails = 0; | |
345 } | |
346 | |
347 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
348 | |
349 pc->name = rrp->peers->name; | |
350 | |
351 return NGX_BUSY; | |
352 } | |
353 | |
354 | |
355 void | |
356 ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data, | |
357 ngx_uint_t state) | |
358 { | |
359 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
360 | |
361 time_t now; | |
362 ngx_http_upstream_rr_peer_t *peer; | |
363 | |
364 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
365 "free rr peer %ui %ui", pc->tries, state); | |
366 | |
367 if (state == 0 && pc->tries == 0) { | |
368 return; | |
369 } | |
370 | |
371 /* TODO: NGX_PEER_KEEPALIVE */ | |
372 | |
373 if (rrp->peers->number == 1) { | |
374 pc->tries = 0; | |
375 return; | |
376 } | |
377 | |
378 if (state & NGX_PEER_FAILED) { | |
379 now = ngx_time(); | |
380 | |
381 peer = &rrp->peers->peer[rrp->current]; | |
382 | |
383 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
384 | |
385 peer->fails++; | |
386 peer->accessed = now; | |
387 | |
388 if (peer->current_weight > 1) { | |
389 peer->current_weight /= 2; | |
390 } | |
391 | |
392 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
393 } | |
394 | |
395 rrp->current++; | |
396 | |
397 if (rrp->current >= rrp->peers->number) { | |
398 rrp->current = 0; | |
399 } | |
400 | |
401 if (pc->tries) { | |
402 pc->tries--; | |
403 } | |
404 | |
405 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
406 } | |
407 | |
408 | |
409 #if (NGX_HTTP_SSL) | |
410 | |
1346 | 411 ngx_int_t |
412 ngx_http_upstream_set_round_robin_peer_session(ngx_peer_connection_t *pc, | |
413 void *data) | |
884 | 414 { |
415 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
416 | |
1346 | 417 ngx_int_t rc; |
884 | 418 ngx_ssl_session_t *ssl_session; |
419 ngx_http_upstream_rr_peer_t *peer; | |
420 | |
1346 | 421 peer = &rrp->peers->peer[rrp->current]; |
422 | |
423 /* TODO: threads only mutex */ | |
424 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
425 | |
426 ssl_session = peer->ssl_session; | |
427 | |
428 rc = ngx_ssl_set_session(pc->connection, ssl_session); | |
429 | |
430 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
1449 | 431 "set session: %p:%d", |
432 ssl_session, ssl_session ? ssl_session->references : 0); | |
1346 | 433 |
434 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
435 | |
436 return rc; | |
437 } | |
438 | |
439 | |
440 void | |
441 ngx_http_upstream_save_round_robin_peer_session(ngx_peer_connection_t *pc, | |
442 void *data) | |
443 { | |
444 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
445 | |
446 ngx_ssl_session_t *old_ssl_session, *ssl_session; | |
447 ngx_http_upstream_rr_peer_t *peer; | |
448 | |
884 | 449 ssl_session = ngx_ssl_get_session(pc->connection); |
450 | |
451 if (ssl_session == NULL) { | |
452 return; | |
453 } | |
454 | |
1346 | 455 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
1449 | 456 "save session: %p:%d", ssl_session, ssl_session->references); |
1346 | 457 |
884 | 458 peer = &rrp->peers->peer[rrp->current]; |
459 | |
1346 | 460 /* TODO: threads only mutex */ |
884 | 461 /* ngx_lock_mutex(rrp->peers->mutex); */ |
1346 | 462 |
463 old_ssl_session = peer->ssl_session; | |
884 | 464 peer->ssl_session = ssl_session; |
1346 | 465 |
884 | 466 /* ngx_unlock_mutex(rrp->peers->mutex); */ |
467 | |
1346 | 468 if (old_ssl_session) { |
469 | |
470 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
1449 | 471 "old session: %p:%d", |
1346 | 472 old_ssl_session, old_ssl_session->references); |
473 | |
884 | 474 /* TODO: may block */ |
1346 | 475 |
476 ngx_ssl_free_session(old_ssl_session); | |
884 | 477 } |
478 } | |
479 | |
480 #endif |