Mercurial > hg > nginx-quic
annotate src/stream/ngx_stream_upstream_round_robin.c @ 6682:db422604ceb0
Stream: allow using the session context inside handlers.
Previously, it was not possible to use the stream context
inside ngx_stream_init_connection() handlers. Now, limit_conn,
access handlers, as well as those added later, can create
their own contexts.
author | Dmitry Volyntsev <xeioex@nginx.com> |
---|---|
date | Tue, 06 Sep 2016 21:28:17 +0300 |
parents | 9757cffc1e2f |
children | 914d8a63dbad |
rev | line source |
---|---|
6115 | 1 |
2 /* | |
3 * Copyright (C) Igor Sysoev | |
4 * Copyright (C) Nginx, Inc. | |
5 */ | |
6 | |
7 | |
8 #include <ngx_config.h> | |
9 #include <ngx_core.h> | |
10 #include <ngx_stream.h> | |
11 | |
12 | |
13 #define ngx_stream_upstream_tries(p) ((p)->number \ | |
14 + ((p)->next ? (p)->next->number : 0)) | |
15 | |
16 | |
17 static ngx_stream_upstream_rr_peer_t *ngx_stream_upstream_get_peer( | |
18 ngx_stream_upstream_rr_peer_data_t *rrp); | |
19 | |
20 #if (NGX_STREAM_SSL) | |
21 | |
22 static ngx_int_t ngx_stream_upstream_set_round_robin_peer_session( | |
23 ngx_peer_connection_t *pc, void *data); | |
24 static void ngx_stream_upstream_save_round_robin_peer_session( | |
25 ngx_peer_connection_t *pc, void *data); | |
6643
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
26 static ngx_int_t ngx_stream_upstream_empty_set_session( |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
27 ngx_peer_connection_t *pc, void *data); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
28 static void ngx_stream_upstream_empty_save_session(ngx_peer_connection_t *pc, |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
29 void *data); |
6115 | 30 |
31 #endif | |
32 | |
33 | |
34 ngx_int_t | |
35 ngx_stream_upstream_init_round_robin(ngx_conf_t *cf, | |
36 ngx_stream_upstream_srv_conf_t *us) | |
37 { | |
38 ngx_url_t u; | |
39 ngx_uint_t i, j, n, w; | |
40 ngx_stream_upstream_server_t *server; | |
41 ngx_stream_upstream_rr_peer_t *peer, **peerp; | |
42 ngx_stream_upstream_rr_peers_t *peers, *backup; | |
43 | |
44 us->peer.init = ngx_stream_upstream_init_round_robin_peer; | |
45 | |
46 if (us->servers) { | |
47 server = us->servers->elts; | |
48 | |
49 n = 0; | |
50 w = 0; | |
51 | |
52 for (i = 0; i < us->servers->nelts; i++) { | |
53 if (server[i].backup) { | |
54 continue; | |
55 } | |
56 | |
57 n += server[i].naddrs; | |
58 w += server[i].naddrs * server[i].weight; | |
59 } | |
60 | |
61 if (n == 0) { | |
62 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
63 "no servers in upstream \"%V\" in %s:%ui", | |
64 &us->host, us->file_name, us->line); | |
65 return NGX_ERROR; | |
66 } | |
67 | |
68 peers = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_rr_peers_t)); | |
69 if (peers == NULL) { | |
70 return NGX_ERROR; | |
71 } | |
72 | |
73 peer = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_rr_peer_t) * n); | |
74 if (peer == NULL) { | |
75 return NGX_ERROR; | |
76 } | |
77 | |
78 peers->single = (n == 1); | |
79 peers->number = n; | |
80 peers->weighted = (w != n); | |
81 peers->total_weight = w; | |
82 peers->name = &us->host; | |
83 | |
84 n = 0; | |
85 peerp = &peers->peer; | |
86 | |
87 for (i = 0; i < us->servers->nelts; i++) { | |
88 if (server[i].backup) { | |
89 continue; | |
90 } | |
91 | |
92 for (j = 0; j < server[i].naddrs; j++) { | |
93 peer[n].sockaddr = server[i].addrs[j].sockaddr; | |
94 peer[n].socklen = server[i].addrs[j].socklen; | |
95 peer[n].name = server[i].addrs[j].name; | |
96 peer[n].weight = server[i].weight; | |
97 peer[n].effective_weight = server[i].weight; | |
98 peer[n].current_weight = 0; | |
99 peer[n].max_fails = server[i].max_fails; | |
100 peer[n].fail_timeout = server[i].fail_timeout; | |
101 peer[n].down = server[i].down; | |
102 peer[n].server = server[i].name; | |
103 | |
104 *peerp = &peer[n]; | |
105 peerp = &peer[n].next; | |
106 n++; | |
107 } | |
108 } | |
109 | |
110 us->peer.data = peers; | |
111 | |
112 /* backup servers */ | |
113 | |
114 n = 0; | |
115 w = 0; | |
116 | |
117 for (i = 0; i < us->servers->nelts; i++) { | |
118 if (!server[i].backup) { | |
119 continue; | |
120 } | |
121 | |
122 n += server[i].naddrs; | |
123 w += server[i].naddrs * server[i].weight; | |
124 } | |
125 | |
126 if (n == 0) { | |
127 return NGX_OK; | |
128 } | |
129 | |
130 backup = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_rr_peers_t)); | |
131 if (backup == NULL) { | |
132 return NGX_ERROR; | |
133 } | |
134 | |
135 peer = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_rr_peer_t) * n); | |
136 if (peer == NULL) { | |
137 return NGX_ERROR; | |
138 } | |
139 | |
140 peers->single = 0; | |
141 backup->single = 0; | |
142 backup->number = n; | |
143 backup->weighted = (w != n); | |
144 backup->total_weight = w; | |
145 backup->name = &us->host; | |
146 | |
147 n = 0; | |
148 peerp = &backup->peer; | |
149 | |
150 for (i = 0; i < us->servers->nelts; i++) { | |
151 if (!server[i].backup) { | |
152 continue; | |
153 } | |
154 | |
155 for (j = 0; j < server[i].naddrs; j++) { | |
156 peer[n].sockaddr = server[i].addrs[j].sockaddr; | |
157 peer[n].socklen = server[i].addrs[j].socklen; | |
158 peer[n].name = server[i].addrs[j].name; | |
159 peer[n].weight = server[i].weight; | |
160 peer[n].effective_weight = server[i].weight; | |
161 peer[n].current_weight = 0; | |
162 peer[n].max_fails = server[i].max_fails; | |
163 peer[n].fail_timeout = server[i].fail_timeout; | |
164 peer[n].down = server[i].down; | |
165 peer[n].server = server[i].name; | |
166 | |
167 *peerp = &peer[n]; | |
168 peerp = &peer[n].next; | |
169 n++; | |
170 } | |
171 } | |
172 | |
173 peers->next = backup; | |
174 | |
175 return NGX_OK; | |
176 } | |
177 | |
178 | |
179 /* an upstream implicitly defined by proxy_pass, etc. */ | |
180 | |
181 if (us->port == 0) { | |
182 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
183 "no port in upstream \"%V\" in %s:%ui", | |
184 &us->host, us->file_name, us->line); | |
185 return NGX_ERROR; | |
186 } | |
187 | |
188 ngx_memzero(&u, sizeof(ngx_url_t)); | |
189 | |
190 u.host = us->host; | |
191 u.port = us->port; | |
192 | |
193 if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) { | |
194 if (u.err) { | |
195 ngx_log_error(NGX_LOG_EMERG, cf->log, 0, | |
196 "%s in upstream \"%V\" in %s:%ui", | |
197 u.err, &us->host, us->file_name, us->line); | |
198 } | |
199 | |
200 return NGX_ERROR; | |
201 } | |
202 | |
203 n = u.naddrs; | |
204 | |
205 peers = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_rr_peers_t)); | |
206 if (peers == NULL) { | |
207 return NGX_ERROR; | |
208 } | |
209 | |
210 peer = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_rr_peer_t) * n); | |
211 if (peer == NULL) { | |
212 return NGX_ERROR; | |
213 } | |
214 | |
215 peers->single = (n == 1); | |
216 peers->number = n; | |
217 peers->weighted = 0; | |
218 peers->total_weight = n; | |
219 peers->name = &us->host; | |
220 | |
221 peerp = &peers->peer; | |
222 | |
223 for (i = 0; i < u.naddrs; i++) { | |
224 peer[i].sockaddr = u.addrs[i].sockaddr; | |
225 peer[i].socklen = u.addrs[i].socklen; | |
226 peer[i].name = u.addrs[i].name; | |
227 peer[i].weight = 1; | |
228 peer[i].effective_weight = 1; | |
229 peer[i].current_weight = 0; | |
230 peer[i].max_fails = 1; | |
231 peer[i].fail_timeout = 10; | |
232 *peerp = &peer[i]; | |
233 peerp = &peer[i].next; | |
234 } | |
235 | |
236 us->peer.data = peers; | |
237 | |
238 /* implicitly defined upstream has no backup servers */ | |
239 | |
240 return NGX_OK; | |
241 } | |
242 | |
243 | |
244 ngx_int_t | |
245 ngx_stream_upstream_init_round_robin_peer(ngx_stream_session_t *s, | |
246 ngx_stream_upstream_srv_conf_t *us) | |
247 { | |
248 ngx_uint_t n; | |
249 ngx_stream_upstream_rr_peer_data_t *rrp; | |
250 | |
251 rrp = s->upstream->peer.data; | |
252 | |
253 if (rrp == NULL) { | |
254 rrp = ngx_palloc(s->connection->pool, | |
255 sizeof(ngx_stream_upstream_rr_peer_data_t)); | |
256 if (rrp == NULL) { | |
257 return NGX_ERROR; | |
258 } | |
259 | |
260 s->upstream->peer.data = rrp; | |
261 } | |
262 | |
263 rrp->peers = us->peer.data; | |
264 rrp->current = NULL; | |
265 | |
266 n = rrp->peers->number; | |
267 | |
268 if (rrp->peers->next && rrp->peers->next->number > n) { | |
269 n = rrp->peers->next->number; | |
270 } | |
271 | |
272 if (n <= 8 * sizeof(uintptr_t)) { | |
273 rrp->tried = &rrp->data; | |
274 rrp->data = 0; | |
275 | |
276 } else { | |
277 n = (n + (8 * sizeof(uintptr_t) - 1)) / (8 * sizeof(uintptr_t)); | |
278 | |
279 rrp->tried = ngx_pcalloc(s->connection->pool, n * sizeof(uintptr_t)); | |
280 if (rrp->tried == NULL) { | |
281 return NGX_ERROR; | |
282 } | |
283 } | |
284 | |
285 s->upstream->peer.get = ngx_stream_upstream_get_round_robin_peer; | |
286 s->upstream->peer.free = ngx_stream_upstream_free_round_robin_peer; | |
287 s->upstream->peer.tries = ngx_stream_upstream_tries(rrp->peers); | |
288 #if (NGX_STREAM_SSL) | |
289 s->upstream->peer.set_session = | |
290 ngx_stream_upstream_set_round_robin_peer_session; | |
291 s->upstream->peer.save_session = | |
292 ngx_stream_upstream_save_round_robin_peer_session; | |
293 #endif | |
294 | |
295 return NGX_OK; | |
296 } | |
297 | |
298 | |
299 ngx_int_t | |
6643
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
300 ngx_stream_upstream_create_round_robin_peer(ngx_stream_session_t *s, |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
301 ngx_stream_upstream_resolved_t *ur) |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
302 { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
303 u_char *p; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
304 size_t len; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
305 socklen_t socklen; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
306 ngx_uint_t i, n; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
307 struct sockaddr *sockaddr; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
308 ngx_stream_upstream_rr_peer_t *peer, **peerp; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
309 ngx_stream_upstream_rr_peers_t *peers; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
310 ngx_stream_upstream_rr_peer_data_t *rrp; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
311 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
312 rrp = s->upstream->peer.data; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
313 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
314 if (rrp == NULL) { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
315 rrp = ngx_palloc(s->connection->pool, |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
316 sizeof(ngx_stream_upstream_rr_peer_data_t)); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
317 if (rrp == NULL) { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
318 return NGX_ERROR; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
319 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
320 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
321 s->upstream->peer.data = rrp; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
322 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
323 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
324 peers = ngx_pcalloc(s->connection->pool, |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
325 sizeof(ngx_stream_upstream_rr_peers_t)); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
326 if (peers == NULL) { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
327 return NGX_ERROR; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
328 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
329 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
330 peer = ngx_pcalloc(s->connection->pool, |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
331 sizeof(ngx_stream_upstream_rr_peer_t) * ur->naddrs); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
332 if (peer == NULL) { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
333 return NGX_ERROR; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
334 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
335 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
336 peers->single = (ur->naddrs == 1); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
337 peers->number = ur->naddrs; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
338 peers->name = &ur->host; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
339 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
340 if (ur->sockaddr) { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
341 peer[0].sockaddr = ur->sockaddr; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
342 peer[0].socklen = ur->socklen; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
343 peer[0].name = ur->host; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
344 peer[0].weight = 1; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
345 peer[0].effective_weight = 1; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
346 peer[0].current_weight = 0; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
347 peer[0].max_fails = 1; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
348 peer[0].fail_timeout = 10; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
349 peers->peer = peer; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
350 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
351 } else { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
352 peerp = &peers->peer; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
353 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
354 for (i = 0; i < ur->naddrs; i++) { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
355 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
356 socklen = ur->addrs[i].socklen; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
357 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
358 sockaddr = ngx_palloc(s->connection->pool, socklen); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
359 if (sockaddr == NULL) { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
360 return NGX_ERROR; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
361 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
362 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
363 ngx_memcpy(sockaddr, ur->addrs[i].sockaddr, socklen); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
364 ngx_inet_set_port(sockaddr, ur->port); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
365 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
366 p = ngx_pnalloc(s->connection->pool, NGX_SOCKADDR_STRLEN); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
367 if (p == NULL) { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
368 return NGX_ERROR; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
369 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
370 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
371 len = ngx_sock_ntop(sockaddr, socklen, p, NGX_SOCKADDR_STRLEN, 1); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
372 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
373 peer[i].sockaddr = sockaddr; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
374 peer[i].socklen = socklen; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
375 peer[i].name.len = len; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
376 peer[i].name.data = p; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
377 peer[i].weight = 1; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
378 peer[i].effective_weight = 1; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
379 peer[i].current_weight = 0; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
380 peer[i].max_fails = 1; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
381 peer[i].fail_timeout = 10; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
382 *peerp = &peer[i]; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
383 peerp = &peer[i].next; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
384 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
385 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
386 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
387 rrp->peers = peers; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
388 rrp->current = NULL; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
389 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
390 if (rrp->peers->number <= 8 * sizeof(uintptr_t)) { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
391 rrp->tried = &rrp->data; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
392 rrp->data = 0; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
393 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
394 } else { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
395 n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1)) |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
396 / (8 * sizeof(uintptr_t)); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
397 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
398 rrp->tried = ngx_pcalloc(s->connection->pool, n * sizeof(uintptr_t)); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
399 if (rrp->tried == NULL) { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
400 return NGX_ERROR; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
401 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
402 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
403 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
404 s->upstream->peer.get = ngx_stream_upstream_get_round_robin_peer; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
405 s->upstream->peer.free = ngx_stream_upstream_free_round_robin_peer; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
406 s->upstream->peer.tries = ngx_stream_upstream_tries(rrp->peers); |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
407 #if (NGX_STREAM_SSL) |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
408 s->upstream->peer.set_session = ngx_stream_upstream_empty_set_session; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
409 s->upstream->peer.save_session = ngx_stream_upstream_empty_save_session; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
410 #endif |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
411 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
412 return NGX_OK; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
413 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
414 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
415 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
416 ngx_int_t |
6115 | 417 ngx_stream_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data) |
418 { | |
419 ngx_stream_upstream_rr_peer_data_t *rrp = data; | |
420 | |
421 ngx_int_t rc; | |
422 ngx_uint_t i, n; | |
423 ngx_stream_upstream_rr_peer_t *peer; | |
424 ngx_stream_upstream_rr_peers_t *peers; | |
425 | |
426 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
427 "get rr peer, try: %ui", pc->tries); | |
428 | |
429 pc->connection = NULL; | |
430 | |
431 peers = rrp->peers; | |
432 ngx_stream_upstream_rr_peers_wlock(peers); | |
433 | |
434 if (peers->single) { | |
435 peer = peers->peer; | |
436 | |
437 if (peer->down) { | |
438 goto failed; | |
439 } | |
440 | |
441 rrp->current = peer; | |
442 | |
443 } else { | |
444 | |
445 /* there are several peers */ | |
446 | |
447 peer = ngx_stream_upstream_get_peer(rrp); | |
448 | |
449 if (peer == NULL) { | |
450 goto failed; | |
451 } | |
452 | |
453 ngx_log_debug2(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
454 "get rr peer, current: %p %i", | |
455 peer, peer->current_weight); | |
456 } | |
457 | |
458 pc->sockaddr = peer->sockaddr; | |
459 pc->socklen = peer->socklen; | |
460 pc->name = &peer->name; | |
461 | |
462 peer->conns++; | |
463 | |
464 ngx_stream_upstream_rr_peers_unlock(peers); | |
465 | |
466 return NGX_OK; | |
467 | |
468 failed: | |
469 | |
470 if (peers->next) { | |
471 | |
472 ngx_log_debug0(NGX_LOG_DEBUG_STREAM, pc->log, 0, "backup servers"); | |
473 | |
474 rrp->peers = peers->next; | |
475 | |
476 n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1)) | |
477 / (8 * sizeof(uintptr_t)); | |
478 | |
479 for (i = 0; i < n; i++) { | |
6474 | 480 rrp->tried[i] = 0; |
6115 | 481 } |
482 | |
483 ngx_stream_upstream_rr_peers_unlock(peers); | |
484 | |
485 rc = ngx_stream_upstream_get_round_robin_peer(pc, rrp); | |
486 | |
487 if (rc != NGX_BUSY) { | |
488 return rc; | |
489 } | |
490 | |
491 ngx_stream_upstream_rr_peers_wlock(peers); | |
492 } | |
493 | |
494 /* all peers failed, mark them as live for quick recovery */ | |
495 | |
496 for (peer = peers->peer; peer; peer = peer->next) { | |
497 peer->fails = 0; | |
498 } | |
499 | |
500 ngx_stream_upstream_rr_peers_unlock(peers); | |
501 | |
502 pc->name = peers->name; | |
503 | |
504 return NGX_BUSY; | |
505 } | |
506 | |
507 | |
508 static ngx_stream_upstream_rr_peer_t * | |
509 ngx_stream_upstream_get_peer(ngx_stream_upstream_rr_peer_data_t *rrp) | |
510 { | |
511 time_t now; | |
512 uintptr_t m; | |
513 ngx_int_t total; | |
514 ngx_uint_t i, n, p; | |
515 ngx_stream_upstream_rr_peer_t *peer, *best; | |
516 | |
517 now = ngx_time(); | |
518 | |
519 best = NULL; | |
520 total = 0; | |
521 | |
522 #if (NGX_SUPPRESS_WARN) | |
523 p = 0; | |
524 #endif | |
525 | |
526 for (peer = rrp->peers->peer, i = 0; | |
527 peer; | |
528 peer = peer->next, i++) | |
529 { | |
530 | |
531 n = i / (8 * sizeof(uintptr_t)); | |
532 m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t)); | |
533 | |
534 if (rrp->tried[n] & m) { | |
535 continue; | |
536 } | |
537 | |
538 if (peer->down) { | |
539 continue; | |
540 } | |
541 | |
542 if (peer->max_fails | |
543 && peer->fails >= peer->max_fails | |
544 && now - peer->checked <= peer->fail_timeout) | |
545 { | |
546 continue; | |
547 } | |
548 | |
549 peer->current_weight += peer->effective_weight; | |
550 total += peer->effective_weight; | |
551 | |
552 if (peer->effective_weight < peer->weight) { | |
553 peer->effective_weight++; | |
554 } | |
555 | |
556 if (best == NULL || peer->current_weight > best->current_weight) { | |
557 best = peer; | |
558 p = i; | |
559 } | |
560 } | |
561 | |
562 if (best == NULL) { | |
563 return NULL; | |
564 } | |
565 | |
566 rrp->current = best; | |
567 | |
568 n = p / (8 * sizeof(uintptr_t)); | |
569 m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t)); | |
570 | |
571 rrp->tried[n] |= m; | |
572 | |
573 best->current_weight -= total; | |
574 | |
575 if (now - best->checked > best->fail_timeout) { | |
576 best->checked = now; | |
577 } | |
578 | |
579 return best; | |
580 } | |
581 | |
582 | |
583 void | |
584 ngx_stream_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data, | |
585 ngx_uint_t state) | |
586 { | |
587 ngx_stream_upstream_rr_peer_data_t *rrp = data; | |
588 | |
589 time_t now; | |
590 ngx_stream_upstream_rr_peer_t *peer; | |
591 | |
592 ngx_log_debug2(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
593 "free rr peer %ui %ui", pc->tries, state); | |
594 | |
595 peer = rrp->current; | |
596 | |
597 ngx_stream_upstream_rr_peers_rlock(rrp->peers); | |
598 ngx_stream_upstream_rr_peer_lock(rrp->peers, peer); | |
599 | |
600 if (rrp->peers->single) { | |
601 peer->conns--; | |
602 | |
603 ngx_stream_upstream_rr_peer_unlock(rrp->peers, peer); | |
604 ngx_stream_upstream_rr_peers_unlock(rrp->peers); | |
605 | |
606 pc->tries = 0; | |
607 return; | |
608 } | |
609 | |
610 if (state & NGX_PEER_FAILED) { | |
611 now = ngx_time(); | |
612 | |
613 peer->fails++; | |
614 peer->accessed = now; | |
615 peer->checked = now; | |
616 | |
617 if (peer->max_fails) { | |
618 peer->effective_weight -= peer->weight / peer->max_fails; | |
6154
cca856715722
Upstream: report to error_log when max_fails is reached.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
619 |
cca856715722
Upstream: report to error_log when max_fails is reached.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
620 if (peer->fails >= peer->max_fails) { |
cca856715722
Upstream: report to error_log when max_fails is reached.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
621 ngx_log_error(NGX_LOG_WARN, pc->log, 0, |
cca856715722
Upstream: report to error_log when max_fails is reached.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
622 "upstream server temporarily disabled"); |
cca856715722
Upstream: report to error_log when max_fails is reached.
Ruslan Ermilov <ru@nginx.com>
parents:
6115
diff
changeset
|
623 } |
6115 | 624 } |
625 | |
626 ngx_log_debug2(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
627 "free rr peer failed: %p %i", | |
628 peer, peer->effective_weight); | |
629 | |
630 if (peer->effective_weight < 0) { | |
631 peer->effective_weight = 0; | |
632 } | |
633 | |
634 } else { | |
635 | |
636 /* mark peer live if check passed */ | |
637 | |
638 if (peer->accessed < peer->checked) { | |
639 peer->fails = 0; | |
640 } | |
641 } | |
642 | |
643 peer->conns--; | |
644 | |
645 ngx_stream_upstream_rr_peer_unlock(rrp->peers, peer); | |
646 ngx_stream_upstream_rr_peers_unlock(rrp->peers); | |
647 | |
648 if (pc->tries) { | |
649 pc->tries--; | |
650 } | |
651 } | |
652 | |
653 | |
654 #if (NGX_STREAM_SSL) | |
655 | |
656 static ngx_int_t | |
657 ngx_stream_upstream_set_round_robin_peer_session(ngx_peer_connection_t *pc, | |
658 void *data) | |
659 { | |
660 ngx_stream_upstream_rr_peer_data_t *rrp = data; | |
661 | |
662 ngx_int_t rc; | |
663 ngx_ssl_session_t *ssl_session; | |
664 ngx_stream_upstream_rr_peer_t *peer; | |
665 #if (NGX_STREAM_UPSTREAM_ZONE) | |
666 int len; | |
667 #if OPENSSL_VERSION_NUMBER >= 0x0090707fL | |
668 const | |
669 #endif | |
670 u_char *p; | |
671 ngx_stream_upstream_rr_peers_t *peers; | |
672 u_char buf[NGX_SSL_MAX_SESSION_SIZE]; | |
673 #endif | |
674 | |
675 peer = rrp->current; | |
676 | |
677 #if (NGX_STREAM_UPSTREAM_ZONE) | |
678 peers = rrp->peers; | |
679 | |
680 if (peers->shpool) { | |
681 ngx_stream_upstream_rr_peers_rlock(peers); | |
682 ngx_stream_upstream_rr_peer_lock(peers, peer); | |
683 | |
684 if (peer->ssl_session == NULL) { | |
685 ngx_stream_upstream_rr_peer_unlock(peers, peer); | |
686 ngx_stream_upstream_rr_peers_unlock(peers); | |
687 return NGX_OK; | |
688 } | |
689 | |
690 len = peer->ssl_session_len; | |
691 | |
692 ngx_memcpy(buf, peer->ssl_session, len); | |
693 | |
694 ngx_stream_upstream_rr_peer_unlock(peers, peer); | |
695 ngx_stream_upstream_rr_peers_unlock(peers); | |
696 | |
697 p = buf; | |
698 ssl_session = d2i_SSL_SESSION(NULL, &p, len); | |
699 | |
700 rc = ngx_ssl_set_session(pc->connection, ssl_session); | |
701 | |
702 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
703 "set session: %p", ssl_session); | |
704 | |
705 ngx_ssl_free_session(ssl_session); | |
706 | |
707 return rc; | |
708 } | |
709 #endif | |
710 | |
711 ssl_session = peer->ssl_session; | |
712 | |
713 rc = ngx_ssl_set_session(pc->connection, ssl_session); | |
714 | |
715 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
716 "set session: %p", ssl_session); | |
717 | |
718 return rc; | |
719 } | |
720 | |
721 | |
722 static void | |
723 ngx_stream_upstream_save_round_robin_peer_session(ngx_peer_connection_t *pc, | |
724 void *data) | |
725 { | |
726 ngx_stream_upstream_rr_peer_data_t *rrp = data; | |
727 | |
728 ngx_ssl_session_t *old_ssl_session, *ssl_session; | |
729 ngx_stream_upstream_rr_peer_t *peer; | |
730 #if (NGX_STREAM_UPSTREAM_ZONE) | |
731 int len; | |
732 u_char *p; | |
733 ngx_stream_upstream_rr_peers_t *peers; | |
734 u_char buf[NGX_SSL_MAX_SESSION_SIZE]; | |
735 #endif | |
736 | |
737 #if (NGX_STREAM_UPSTREAM_ZONE) | |
738 peers = rrp->peers; | |
739 | |
740 if (peers->shpool) { | |
741 | |
742 ssl_session = SSL_get0_session(pc->connection->ssl->connection); | |
743 | |
744 if (ssl_session == NULL) { | |
745 return; | |
746 } | |
747 | |
748 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
749 "save session: %p", ssl_session); | |
750 | |
751 len = i2d_SSL_SESSION(ssl_session, NULL); | |
752 | |
753 /* do not cache too big session */ | |
754 | |
755 if (len > NGX_SSL_MAX_SESSION_SIZE) { | |
756 return; | |
757 } | |
758 | |
759 p = buf; | |
760 (void) i2d_SSL_SESSION(ssl_session, &p); | |
761 | |
762 peer = rrp->current; | |
763 | |
764 ngx_stream_upstream_rr_peers_rlock(peers); | |
765 ngx_stream_upstream_rr_peer_lock(peers, peer); | |
766 | |
767 if (len > peer->ssl_session_len) { | |
768 ngx_shmtx_lock(&peers->shpool->mutex); | |
769 | |
770 if (peer->ssl_session) { | |
771 ngx_slab_free_locked(peers->shpool, peer->ssl_session); | |
772 } | |
773 | |
774 peer->ssl_session = ngx_slab_alloc_locked(peers->shpool, len); | |
775 | |
776 ngx_shmtx_unlock(&peers->shpool->mutex); | |
777 | |
778 if (peer->ssl_session == NULL) { | |
779 peer->ssl_session_len = 0; | |
780 | |
781 ngx_stream_upstream_rr_peer_unlock(peers, peer); | |
782 ngx_stream_upstream_rr_peers_unlock(peers); | |
783 return; | |
784 } | |
785 | |
786 peer->ssl_session_len = len; | |
787 } | |
788 | |
789 ngx_memcpy(peer->ssl_session, buf, len); | |
790 | |
791 ngx_stream_upstream_rr_peer_unlock(peers, peer); | |
792 ngx_stream_upstream_rr_peers_unlock(peers); | |
793 | |
794 return; | |
795 } | |
796 #endif | |
797 | |
798 ssl_session = ngx_ssl_get_session(pc->connection); | |
799 | |
800 if (ssl_session == NULL) { | |
801 return; | |
802 } | |
803 | |
804 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
805 "save session: %p", ssl_session); | |
806 | |
807 peer = rrp->current; | |
808 | |
809 old_ssl_session = peer->ssl_session; | |
810 peer->ssl_session = ssl_session; | |
811 | |
812 if (old_ssl_session) { | |
813 | |
814 ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0, | |
815 "old session: %p", old_ssl_session); | |
816 | |
817 /* TODO: may block */ | |
818 | |
819 ngx_ssl_free_session(old_ssl_session); | |
820 } | |
821 } | |
822 | |
6643
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
823 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
824 static ngx_int_t |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
825 ngx_stream_upstream_empty_set_session(ngx_peer_connection_t *pc, void *data) |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
826 { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
827 return NGX_OK; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
828 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
829 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
830 |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
831 static void |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
832 ngx_stream_upstream_empty_save_session(ngx_peer_connection_t *pc, void *data) |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
833 { |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
834 return; |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
835 } |
9757cffc1e2f
Stream: variables in proxy_pass and proxy_ssl_name.
Vladimir Homutov <vl@nginx.com>
parents:
6474
diff
changeset
|
836 |
6115 | 837 #endif |