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