Mercurial > hg > nginx-quic
annotate src/http/ngx_http_upstream_round_robin.c @ 1851:056347360e14 stable-0.5
r1755, r1756, r1757 merge:
*) SSL_shutdown() never returns -1, on error it returns 0.
This fixes incidental "bad write retry" errors.
*) cleaning stale global SSL error
*) remove SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER, we never need it
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 08 Jan 2008 17:30:39 +0000 |
parents | 20a0f2880d45 |
children |
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 |
1710 | 76 if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) { |
884 | 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 for (i = 0; i < u.naddrs; i++) { | |
1709 | 98 peers->peer[i].sockaddr = u.addrs[i].sockaddr; |
99 peers->peer[i].socklen = u.addrs[i].socklen; | |
100 peers->peer[i].name = u.addrs[i].name; | |
101 peers->peer[i].weight = 1; | |
102 peers->peer[i].current_weight = 1; | |
103 peers->peer[i].max_fails = 1; | |
104 peers->peer[i].fail_timeout = 10; | |
884 | 105 } |
106 | |
107 us->peer.data = peers; | |
108 | |
109 return NGX_OK; | |
110 } | |
111 | |
112 | |
113 ngx_int_t | |
114 ngx_http_upstream_init_round_robin_peer(ngx_http_request_t *r, | |
115 ngx_http_upstream_srv_conf_t *us) | |
116 { | |
117 ngx_uint_t n; | |
118 ngx_http_upstream_rr_peer_data_t *rrp; | |
119 | |
120 rrp = r->upstream->peer.data; | |
121 | |
122 if (rrp == NULL) { | |
123 rrp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_rr_peer_data_t)); | |
124 if (rrp == NULL) { | |
125 return NGX_ERROR; | |
126 } | |
127 | |
128 r->upstream->peer.data = rrp; | |
129 } | |
130 | |
131 rrp->peers = us->peer.data; | |
132 rrp->current = 0; | |
133 | |
134 if (rrp->peers->number <= 8 * sizeof(uintptr_t)) { | |
135 rrp->tried = &rrp->data; | |
136 rrp->data = 0; | |
137 | |
138 } else { | |
139 n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1)) | |
140 / (8 * sizeof(uintptr_t)); | |
141 | |
142 rrp->tried = ngx_pcalloc(r->pool, n * sizeof(uintptr_t)); | |
143 if (rrp->tried == NULL) { | |
144 return NGX_ERROR; | |
145 } | |
146 } | |
147 | |
148 r->upstream->peer.get = ngx_http_upstream_get_round_robin_peer; | |
149 r->upstream->peer.free = ngx_http_upstream_free_round_robin_peer; | |
150 r->upstream->peer.tries = rrp->peers->number; | |
151 #if (NGX_HTTP_SSL) | |
1346 | 152 r->upstream->peer.set_session = |
153 ngx_http_upstream_set_round_robin_peer_session; | |
154 r->upstream->peer.save_session = | |
155 ngx_http_upstream_save_round_robin_peer_session; | |
884 | 156 #endif |
157 | |
158 return NGX_OK; | |
159 } | |
160 | |
161 | |
162 ngx_int_t | |
163 ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data) | |
164 { | |
165 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
166 | |
167 time_t now; | |
168 uintptr_t m; | |
169 ngx_uint_t i, n; | |
170 ngx_connection_t *c; | |
171 ngx_http_upstream_rr_peer_t *peer; | |
172 | |
173 ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
174 "get rr peer, try: %ui", pc->tries); | |
175 | |
176 now = ngx_time(); | |
177 | |
178 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
179 | |
180 if (rrp->peers->last_cached) { | |
181 | |
182 /* cached connection */ | |
183 | |
184 c = rrp->peers->cached[rrp->peers->last_cached]; | |
185 rrp->peers->last_cached--; | |
186 | |
187 /* ngx_unlock_mutex(ppr->peers->mutex); */ | |
188 | |
189 #if (NGX_THREADS) | |
190 c->read->lock = c->read->own_lock; | |
191 c->write->lock = c->write->own_lock; | |
192 #endif | |
193 | |
194 pc->connection = c; | |
195 pc->cached = 1; | |
196 | |
197 return NGX_OK; | |
198 } | |
199 | |
200 pc->cached = 0; | |
201 pc->connection = NULL; | |
202 | |
203 if (rrp->peers->number == 1) { | |
204 peer = &rrp->peers->peer[0]; | |
205 | |
206 } else { | |
207 | |
208 /* there are several peers */ | |
209 | |
210 if (pc->tries == rrp->peers->number) { | |
211 | |
212 /* it's a first try - get a current peer */ | |
213 | |
214 for ( ;; ) { | |
215 rrp->current = rrp->peers->current; | |
216 | |
217 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
|
218 m = (uintptr_t) 1 << rrp->current % (8 * sizeof(uintptr_t)); |
884 | 219 |
220 if (!(rrp->tried[n] & m)) { | |
221 peer = &rrp->peers->peer[rrp->current]; | |
222 | |
223 if (!peer->down) { | |
224 | |
225 if (peer->max_fails == 0 | |
226 || peer->fails < peer->max_fails) | |
227 { | |
228 break; | |
229 } | |
230 | |
231 if (now - peer->accessed > peer->fail_timeout) { | |
232 peer->fails = 0; | |
233 break; | |
234 } | |
235 | |
236 } else { | |
237 rrp->tried[n] |= m; | |
238 } | |
239 | |
240 pc->tries--; | |
241 } | |
242 | |
243 rrp->peers->current++; | |
244 | |
245 if (rrp->peers->current >= rrp->peers->number) { | |
246 rrp->peers->current = 0; | |
247 } | |
248 | |
249 if (pc->tries) { | |
250 continue; | |
251 } | |
252 | |
253 goto failed; | |
254 } | |
255 | |
256 peer->current_weight--; | |
257 | |
258 if (peer->current_weight == 0) { | |
259 peer->current_weight = peer->weight; | |
260 | |
261 rrp->peers->current++; | |
262 | |
263 if (rrp->peers->current >= rrp->peers->number) { | |
264 rrp->peers->current = 0; | |
265 } | |
266 } | |
267 | |
268 } else { | |
269 for ( ;; ) { | |
270 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
|
271 m = (uintptr_t) 1 << rrp->current % (8 * sizeof(uintptr_t)); |
884 | 272 |
273 if (!(rrp->tried[n] & m)) { | |
274 | |
275 peer = &rrp->peers->peer[rrp->current]; | |
276 | |
277 if (!peer->down) { | |
278 | |
279 if (peer->max_fails == 0 | |
280 || peer->fails < peer->max_fails) | |
281 { | |
282 break; | |
283 } | |
284 | |
285 if (now - peer->accessed > peer->fail_timeout) { | |
286 peer->fails = 0; | |
287 break; | |
288 } | |
289 | |
290 } else { | |
291 rrp->tried[n] |= m; | |
292 } | |
293 | |
294 pc->tries--; | |
295 } | |
296 | |
297 rrp->current++; | |
298 | |
299 if (rrp->current >= rrp->peers->number) { | |
300 rrp->current = 0; | |
301 } | |
302 | |
303 if (pc->tries) { | |
304 continue; | |
305 } | |
306 | |
307 goto failed; | |
308 } | |
309 | |
310 peer->current_weight--; | |
311 | |
312 if (peer->current_weight == 0) { | |
313 peer->current_weight = peer->weight; | |
314 | |
315 if (rrp->current == rrp->peers->current) { | |
316 rrp->peers->current++; | |
317 | |
318 if (rrp->peers->current >= rrp->peers->number) { | |
319 rrp->peers->current = 0; | |
320 } | |
321 } | |
322 } | |
323 } | |
324 | |
325 rrp->tried[n] |= m; | |
326 } | |
327 | |
328 pc->sockaddr = peer->sockaddr; | |
329 pc->socklen = peer->socklen; | |
330 pc->name = &peer->name; | |
331 | |
332 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
333 | |
334 return NGX_OK; | |
335 | |
336 failed: | |
337 | |
338 /* all peers failed, mark them as live for quick recovery */ | |
339 | |
340 for (i = 0; i < rrp->peers->number; i++) { | |
341 rrp->peers->peer[i].fails = 0; | |
342 } | |
343 | |
344 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
345 | |
346 pc->name = rrp->peers->name; | |
347 | |
348 return NGX_BUSY; | |
349 } | |
350 | |
351 | |
352 void | |
353 ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data, | |
354 ngx_uint_t state) | |
355 { | |
356 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
357 | |
358 time_t now; | |
359 ngx_http_upstream_rr_peer_t *peer; | |
360 | |
361 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
362 "free rr peer %ui %ui", pc->tries, state); | |
363 | |
364 if (state == 0 && pc->tries == 0) { | |
365 return; | |
366 } | |
367 | |
368 /* TODO: NGX_PEER_KEEPALIVE */ | |
369 | |
370 if (rrp->peers->number == 1) { | |
371 pc->tries = 0; | |
372 return; | |
373 } | |
374 | |
375 if (state & NGX_PEER_FAILED) { | |
376 now = ngx_time(); | |
377 | |
378 peer = &rrp->peers->peer[rrp->current]; | |
379 | |
380 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
381 | |
382 peer->fails++; | |
383 peer->accessed = now; | |
384 | |
385 if (peer->current_weight > 1) { | |
386 peer->current_weight /= 2; | |
387 } | |
388 | |
389 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
390 } | |
391 | |
392 rrp->current++; | |
393 | |
394 if (rrp->current >= rrp->peers->number) { | |
395 rrp->current = 0; | |
396 } | |
397 | |
398 if (pc->tries) { | |
399 pc->tries--; | |
400 } | |
401 | |
402 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
403 } | |
404 | |
405 | |
406 #if (NGX_HTTP_SSL) | |
407 | |
1346 | 408 ngx_int_t |
409 ngx_http_upstream_set_round_robin_peer_session(ngx_peer_connection_t *pc, | |
410 void *data) | |
884 | 411 { |
412 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
413 | |
1346 | 414 ngx_int_t rc; |
884 | 415 ngx_ssl_session_t *ssl_session; |
416 ngx_http_upstream_rr_peer_t *peer; | |
417 | |
1346 | 418 peer = &rrp->peers->peer[rrp->current]; |
419 | |
420 /* TODO: threads only mutex */ | |
421 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
422 | |
423 ssl_session = peer->ssl_session; | |
424 | |
425 rc = ngx_ssl_set_session(pc->connection, ssl_session); | |
426 | |
427 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
1449 | 428 "set session: %p:%d", |
429 ssl_session, ssl_session ? ssl_session->references : 0); | |
1346 | 430 |
431 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
432 | |
433 return rc; | |
434 } | |
435 | |
436 | |
437 void | |
438 ngx_http_upstream_save_round_robin_peer_session(ngx_peer_connection_t *pc, | |
439 void *data) | |
440 { | |
441 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
442 | |
443 ngx_ssl_session_t *old_ssl_session, *ssl_session; | |
444 ngx_http_upstream_rr_peer_t *peer; | |
445 | |
884 | 446 ssl_session = ngx_ssl_get_session(pc->connection); |
447 | |
448 if (ssl_session == NULL) { | |
449 return; | |
450 } | |
451 | |
1346 | 452 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, |
1449 | 453 "save session: %p:%d", ssl_session, ssl_session->references); |
1346 | 454 |
884 | 455 peer = &rrp->peers->peer[rrp->current]; |
456 | |
1346 | 457 /* TODO: threads only mutex */ |
884 | 458 /* ngx_lock_mutex(rrp->peers->mutex); */ |
1346 | 459 |
460 old_ssl_session = peer->ssl_session; | |
884 | 461 peer->ssl_session = ssl_session; |
1346 | 462 |
884 | 463 /* ngx_unlock_mutex(rrp->peers->mutex); */ |
464 | |
1346 | 465 if (old_ssl_session) { |
466 | |
467 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0, | |
1449 | 468 "old session: %p:%d", |
1346 | 469 old_ssl_session, old_ssl_session->references); |
470 | |
884 | 471 /* TODO: may block */ |
1346 | 472 |
473 ngx_ssl_free_session(old_ssl_session); | |
884 | 474 } |
475 } | |
476 | |
477 #endif |