Mercurial > hg > nginx-quic
annotate src/http/ngx_http_upstream_round_robin.c @ 927:4f3d57408877
bump version
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 14 Dec 2006 23:22:34 +0000 |
parents | 327aef5c6ac4 |
children | 451e34738078 |
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", | |
67 &us->host, us->file_name.data, us->line); | |
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", |
884 | 80 u.err, &us->host, us->file_name.data, us->line); |
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) | |
155 r->upstream->peer.save_session = ngx_http_upstream_save_round_robin_peer; | |
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)); | |
218 m = 1 << rrp->current % (8 * sizeof(uintptr_t)); | |
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)); | |
271 m = 1 << rrp->current % (8 * sizeof(uintptr_t)); | |
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 #if (NGX_SSL) | |
332 pc->ssl_session = peer->ssl_session; | |
333 #endif | |
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 | |
411 void | |
412 ngx_http_upstream_save_round_robin_peer(ngx_peer_connection_t *pc, void *data) | |
413 { | |
414 ngx_http_upstream_rr_peer_data_t *rrp = data; | |
415 | |
416 ngx_ssl_session_t *ssl_session; | |
417 ngx_http_upstream_rr_peer_t *peer; | |
418 | |
419 ssl_session = ngx_ssl_get_session(pc->connection); | |
420 | |
421 if (ssl_session == NULL) { | |
422 return; | |
423 } | |
424 | |
425 peer = &rrp->peers->peer[rrp->current]; | |
426 | |
427 /* ngx_lock_mutex(rrp->peers->mutex); */ | |
428 peer->ssl_session = ssl_session; | |
429 /* ngx_unlock_mutex(rrp->peers->mutex); */ | |
430 | |
431 if (pc->ssl_session) { | |
432 /* TODO: may block */ | |
433 ngx_ssl_free_session(pc->ssl_session); | |
434 } | |
435 } | |
436 | |
437 #endif |