Back to home page

Nginx displayed by LXR

Source navigation ]
Diff markup ]
Identifier search ]
general search ]
 
 
Version: nginx-1.13.12 ]​[ nginx-1.12.2 ]​

0001 
0002 /*
0003  * Copyright (C) Igor Sysoev
0004  * Copyright (C) Nginx, Inc.
0005  */
0006 
0007 
0008 #include <ngx_config.h>
0009 #include <ngx_core.h>
0010 #include <ngx_stream.h>
0011 
0012 
0013 #define ngx_stream_upstream_tries(p) ((p)->number                             \
0014                                       + ((p)->next ? (p)->next->number : 0))
0015 
0016 
0017 static ngx_stream_upstream_rr_peer_t *ngx_stream_upstream_get_peer(
0018     ngx_stream_upstream_rr_peer_data_t *rrp);
0019 static void ngx_stream_upstream_notify_round_robin_peer(
0020     ngx_peer_connection_t *pc, void *data, ngx_uint_t state);
0021 
0022 #if (NGX_STREAM_SSL)
0023 
0024 static ngx_int_t ngx_stream_upstream_set_round_robin_peer_session(
0025     ngx_peer_connection_t *pc, void *data);
0026 static void ngx_stream_upstream_save_round_robin_peer_session(
0027     ngx_peer_connection_t *pc, void *data);
0028 static ngx_int_t ngx_stream_upstream_empty_set_session(
0029     ngx_peer_connection_t *pc, void *data);
0030 static void ngx_stream_upstream_empty_save_session(ngx_peer_connection_t *pc,
0031     void *data);
0032 
0033 #endif
0034 
0035 
0036 ngx_int_t
0037 ngx_stream_upstream_init_round_robin(ngx_conf_t *cf,
0038     ngx_stream_upstream_srv_conf_t *us)
0039 {
0040     ngx_url_t                        u;
0041     ngx_uint_t                       i, j, n, w;
0042     ngx_stream_upstream_server_t    *server;
0043     ngx_stream_upstream_rr_peer_t   *peer, **peerp;
0044     ngx_stream_upstream_rr_peers_t  *peers, *backup;
0045 
0046     us->peer.init = ngx_stream_upstream_init_round_robin_peer;
0047 
0048     if (us->servers) {
0049         server = us->servers->elts;
0050 
0051         n = 0;
0052         w = 0;
0053 
0054         for (i = 0; i < us->servers->nelts; i++) {
0055             if (server[i].backup) {
0056                 continue;
0057             }
0058 
0059             n += server[i].naddrs;
0060             w += server[i].naddrs * server[i].weight;
0061         }
0062 
0063         if (n == 0) {
0064             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
0065                           "no servers in upstream \"%V\" in %s:%ui",
0066                           &us->host, us->file_name, us->line);
0067             return NGX_ERROR;
0068         }
0069 
0070         peers = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_rr_peers_t));
0071         if (peers == NULL) {
0072             return NGX_ERROR;
0073         }
0074 
0075         peer = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_rr_peer_t) * n);
0076         if (peer == NULL) {
0077             return NGX_ERROR;
0078         }
0079 
0080         peers->single = (n == 1);
0081         peers->number = n;
0082         peers->weighted = (w != n);
0083         peers->total_weight = w;
0084         peers->name = &us->host;
0085 
0086         n = 0;
0087         peerp = &peers->peer;
0088 
0089         for (i = 0; i < us->servers->nelts; i++) {
0090             if (server[i].backup) {
0091                 continue;
0092             }
0093 
0094             for (j = 0; j < server[i].naddrs; j++) {
0095                 peer[n].sockaddr = server[i].addrs[j].sockaddr;
0096                 peer[n].socklen = server[i].addrs[j].socklen;
0097                 peer[n].name = server[i].addrs[j].name;
0098                 peer[n].weight = server[i].weight;
0099                 peer[n].effective_weight = server[i].weight;
0100                 peer[n].current_weight = 0;
0101                 peer[n].max_conns = server[i].max_conns;
0102                 peer[n].max_fails = server[i].max_fails;
0103                 peer[n].fail_timeout = server[i].fail_timeout;
0104                 peer[n].down = server[i].down;
0105                 peer[n].server = server[i].name;
0106 
0107                 *peerp = &peer[n];
0108                 peerp = &peer[n].next;
0109                 n++;
0110             }
0111         }
0112 
0113         us->peer.data = peers;
0114 
0115         /* backup servers */
0116 
0117         n = 0;
0118         w = 0;
0119 
0120         for (i = 0; i < us->servers->nelts; i++) {
0121             if (!server[i].backup) {
0122                 continue;
0123             }
0124 
0125             n += server[i].naddrs;
0126             w += server[i].naddrs * server[i].weight;
0127         }
0128 
0129         if (n == 0) {
0130             return NGX_OK;
0131         }
0132 
0133         backup = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_rr_peers_t));
0134         if (backup == NULL) {
0135             return NGX_ERROR;
0136         }
0137 
0138         peer = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_rr_peer_t) * n);
0139         if (peer == NULL) {
0140             return NGX_ERROR;
0141         }
0142 
0143         peers->single = 0;
0144         backup->single = 0;
0145         backup->number = n;
0146         backup->weighted = (w != n);
0147         backup->total_weight = w;
0148         backup->name = &us->host;
0149 
0150         n = 0;
0151         peerp = &backup->peer;
0152 
0153         for (i = 0; i < us->servers->nelts; i++) {
0154             if (!server[i].backup) {
0155                 continue;
0156             }
0157 
0158             for (j = 0; j < server[i].naddrs; j++) {
0159                 peer[n].sockaddr = server[i].addrs[j].sockaddr;
0160                 peer[n].socklen = server[i].addrs[j].socklen;
0161                 peer[n].name = server[i].addrs[j].name;
0162                 peer[n].weight = server[i].weight;
0163                 peer[n].effective_weight = server[i].weight;
0164                 peer[n].current_weight = 0;
0165                 peer[n].max_conns = server[i].max_conns;
0166                 peer[n].max_fails = server[i].max_fails;
0167                 peer[n].fail_timeout = server[i].fail_timeout;
0168                 peer[n].down = server[i].down;
0169                 peer[n].server = server[i].name;
0170 
0171                 *peerp = &peer[n];
0172                 peerp = &peer[n].next;
0173                 n++;
0174             }
0175         }
0176 
0177         peers->next = backup;
0178 
0179         return NGX_OK;
0180     }
0181 
0182 
0183     /* an upstream implicitly defined by proxy_pass, etc. */
0184 
0185     if (us->port == 0) {
0186         ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
0187                       "no port in upstream \"%V\" in %s:%ui",
0188                       &us->host, us->file_name, us->line);
0189         return NGX_ERROR;
0190     }
0191 
0192     ngx_memzero(&u, sizeof(ngx_url_t));
0193 
0194     u.host = us->host;
0195     u.port = us->port;
0196 
0197     if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
0198         if (u.err) {
0199             ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
0200                           "%s in upstream \"%V\" in %s:%ui",
0201                           u.err, &us->host, us->file_name, us->line);
0202         }
0203 
0204         return NGX_ERROR;
0205     }
0206 
0207     n = u.naddrs;
0208 
0209     peers = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_rr_peers_t));
0210     if (peers == NULL) {
0211         return NGX_ERROR;
0212     }
0213 
0214     peer = ngx_pcalloc(cf->pool, sizeof(ngx_stream_upstream_rr_peer_t) * n);
0215     if (peer == NULL) {
0216         return NGX_ERROR;
0217     }
0218 
0219     peers->single = (n == 1);
0220     peers->number = n;
0221     peers->weighted = 0;
0222     peers->total_weight = n;
0223     peers->name = &us->host;
0224 
0225     peerp = &peers->peer;
0226 
0227     for (i = 0; i < u.naddrs; i++) {
0228         peer[i].sockaddr = u.addrs[i].sockaddr;
0229         peer[i].socklen = u.addrs[i].socklen;
0230         peer[i].name = u.addrs[i].name;
0231         peer[i].weight = 1;
0232         peer[i].effective_weight = 1;
0233         peer[i].current_weight = 0;
0234         peer[i].max_conns = 0;
0235         peer[i].max_fails = 1;
0236         peer[i].fail_timeout = 10;
0237         *peerp = &peer[i];
0238         peerp = &peer[i].next;
0239     }
0240 
0241     us->peer.data = peers;
0242 
0243     /* implicitly defined upstream has no backup servers */
0244 
0245     return NGX_OK;
0246 }
0247 
0248 
0249 ngx_int_t
0250 ngx_stream_upstream_init_round_robin_peer(ngx_stream_session_t *s,
0251     ngx_stream_upstream_srv_conf_t *us)
0252 {
0253     ngx_uint_t                           n;
0254     ngx_stream_upstream_rr_peer_data_t  *rrp;
0255 
0256     rrp = s->upstream->peer.data;
0257 
0258     if (rrp == NULL) {
0259         rrp = ngx_palloc(s->connection->pool,
0260                          sizeof(ngx_stream_upstream_rr_peer_data_t));
0261         if (rrp == NULL) {
0262             return NGX_ERROR;
0263         }
0264 
0265         s->upstream->peer.data = rrp;
0266     }
0267 
0268     rrp->peers = us->peer.data;
0269     rrp->current = NULL;
0270     rrp->config = 0;
0271 
0272     n = rrp->peers->number;
0273 
0274     if (rrp->peers->next && rrp->peers->next->number > n) {
0275         n = rrp->peers->next->number;
0276     }
0277 
0278     if (n <= 8 * sizeof(uintptr_t)) {
0279         rrp->tried = &rrp->data;
0280         rrp->data = 0;
0281 
0282     } else {
0283         n = (n + (8 * sizeof(uintptr_t) - 1)) / (8 * sizeof(uintptr_t));
0284 
0285         rrp->tried = ngx_pcalloc(s->connection->pool, n * sizeof(uintptr_t));
0286         if (rrp->tried == NULL) {
0287             return NGX_ERROR;
0288         }
0289     }
0290 
0291     s->upstream->peer.get = ngx_stream_upstream_get_round_robin_peer;
0292     s->upstream->peer.free = ngx_stream_upstream_free_round_robin_peer;
0293     s->upstream->peer.notify = ngx_stream_upstream_notify_round_robin_peer;
0294     s->upstream->peer.tries = ngx_stream_upstream_tries(rrp->peers);
0295 #if (NGX_STREAM_SSL)
0296     s->upstream->peer.set_session =
0297                              ngx_stream_upstream_set_round_robin_peer_session;
0298     s->upstream->peer.save_session =
0299                              ngx_stream_upstream_save_round_robin_peer_session;
0300 #endif
0301 
0302     return NGX_OK;
0303 }
0304 
0305 
0306 ngx_int_t
0307 ngx_stream_upstream_create_round_robin_peer(ngx_stream_session_t *s,
0308     ngx_stream_upstream_resolved_t *ur)
0309 {
0310     u_char                              *p;
0311     size_t                               len;
0312     socklen_t                            socklen;
0313     ngx_uint_t                           i, n;
0314     struct sockaddr                     *sockaddr;
0315     ngx_stream_upstream_rr_peer_t       *peer, **peerp;
0316     ngx_stream_upstream_rr_peers_t      *peers;
0317     ngx_stream_upstream_rr_peer_data_t  *rrp;
0318 
0319     rrp = s->upstream->peer.data;
0320 
0321     if (rrp == NULL) {
0322         rrp = ngx_palloc(s->connection->pool,
0323                          sizeof(ngx_stream_upstream_rr_peer_data_t));
0324         if (rrp == NULL) {
0325             return NGX_ERROR;
0326         }
0327 
0328         s->upstream->peer.data = rrp;
0329     }
0330 
0331     peers = ngx_pcalloc(s->connection->pool,
0332                         sizeof(ngx_stream_upstream_rr_peers_t));
0333     if (peers == NULL) {
0334         return NGX_ERROR;
0335     }
0336 
0337     peer = ngx_pcalloc(s->connection->pool,
0338                        sizeof(ngx_stream_upstream_rr_peer_t) * ur->naddrs);
0339     if (peer == NULL) {
0340         return NGX_ERROR;
0341     }
0342 
0343     peers->single = (ur->naddrs == 1);
0344     peers->number = ur->naddrs;
0345     peers->name = &ur->host;
0346 
0347     if (ur->sockaddr) {
0348         peer[0].sockaddr = ur->sockaddr;
0349         peer[0].socklen = ur->socklen;
0350         peer[0].name = ur->name;
0351         peer[0].weight = 1;
0352         peer[0].effective_weight = 1;
0353         peer[0].current_weight = 0;
0354         peer[0].max_conns = 0;
0355         peer[0].max_fails = 1;
0356         peer[0].fail_timeout = 10;
0357         peers->peer = peer;
0358 
0359     } else {
0360         peerp = &peers->peer;
0361 
0362         for (i = 0; i < ur->naddrs; i++) {
0363 
0364             socklen = ur->addrs[i].socklen;
0365 
0366             sockaddr = ngx_palloc(s->connection->pool, socklen);
0367             if (sockaddr == NULL) {
0368                 return NGX_ERROR;
0369             }
0370 
0371             ngx_memcpy(sockaddr, ur->addrs[i].sockaddr, socklen);
0372             ngx_inet_set_port(sockaddr, ur->port);
0373 
0374             p = ngx_pnalloc(s->connection->pool, NGX_SOCKADDR_STRLEN);
0375             if (p == NULL) {
0376                 return NGX_ERROR;
0377             }
0378 
0379             len = ngx_sock_ntop(sockaddr, socklen, p, NGX_SOCKADDR_STRLEN, 1);
0380 
0381             peer[i].sockaddr = sockaddr;
0382             peer[i].socklen = socklen;
0383             peer[i].name.len = len;
0384             peer[i].name.data = p;
0385             peer[i].weight = 1;
0386             peer[i].effective_weight = 1;
0387             peer[i].current_weight = 0;
0388             peer[i].max_conns = 0;
0389             peer[i].max_fails = 1;
0390             peer[i].fail_timeout = 10;
0391             *peerp = &peer[i];
0392             peerp = &peer[i].next;
0393         }
0394     }
0395 
0396     rrp->peers = peers;
0397     rrp->current = NULL;
0398     rrp->config = 0;
0399 
0400     if (rrp->peers->number <= 8 * sizeof(uintptr_t)) {
0401         rrp->tried = &rrp->data;
0402         rrp->data = 0;
0403 
0404     } else {
0405         n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1))
0406                 / (8 * sizeof(uintptr_t));
0407 
0408         rrp->tried = ngx_pcalloc(s->connection->pool, n * sizeof(uintptr_t));
0409         if (rrp->tried == NULL) {
0410             return NGX_ERROR;
0411         }
0412     }
0413 
0414     s->upstream->peer.get = ngx_stream_upstream_get_round_robin_peer;
0415     s->upstream->peer.free = ngx_stream_upstream_free_round_robin_peer;
0416     s->upstream->peer.tries = ngx_stream_upstream_tries(rrp->peers);
0417 #if (NGX_STREAM_SSL)
0418     s->upstream->peer.set_session = ngx_stream_upstream_empty_set_session;
0419     s->upstream->peer.save_session = ngx_stream_upstream_empty_save_session;
0420 #endif
0421 
0422     return NGX_OK;
0423 }
0424 
0425 
0426 ngx_int_t
0427 ngx_stream_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data)
0428 {
0429     ngx_stream_upstream_rr_peer_data_t *rrp = data;
0430 
0431     ngx_int_t                        rc;
0432     ngx_uint_t                       i, n;
0433     ngx_stream_upstream_rr_peer_t   *peer;
0434     ngx_stream_upstream_rr_peers_t  *peers;
0435 
0436     ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0,
0437                    "get rr peer, try: %ui", pc->tries);
0438 
0439     pc->connection = NULL;
0440 
0441     peers = rrp->peers;
0442     ngx_stream_upstream_rr_peers_wlock(peers);
0443 
0444     if (peers->single) {
0445         peer = peers->peer;
0446 
0447         if (peer->down) {
0448             goto failed;
0449         }
0450 
0451         if (peer->max_conns && peer->conns >= peer->max_conns) {
0452             goto failed;
0453         }
0454 
0455         rrp->current = peer;
0456 
0457     } else {
0458 
0459         /* there are several peers */
0460 
0461         peer = ngx_stream_upstream_get_peer(rrp);
0462 
0463         if (peer == NULL) {
0464             goto failed;
0465         }
0466 
0467         ngx_log_debug2(NGX_LOG_DEBUG_STREAM, pc->log, 0,
0468                        "get rr peer, current: %p %i",
0469                        peer, peer->current_weight);
0470     }
0471 
0472     pc->sockaddr = peer->sockaddr;
0473     pc->socklen = peer->socklen;
0474     pc->name = &peer->name;
0475 
0476     peer->conns++;
0477 
0478     ngx_stream_upstream_rr_peers_unlock(peers);
0479 
0480     return NGX_OK;
0481 
0482 failed:
0483 
0484     if (peers->next) {
0485 
0486         ngx_log_debug0(NGX_LOG_DEBUG_STREAM, pc->log, 0, "backup servers");
0487 
0488         rrp->peers = peers->next;
0489 
0490         n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1))
0491                 / (8 * sizeof(uintptr_t));
0492 
0493         for (i = 0; i < n; i++) {
0494             rrp->tried[i] = 0;
0495         }
0496 
0497         ngx_stream_upstream_rr_peers_unlock(peers);
0498 
0499         rc = ngx_stream_upstream_get_round_robin_peer(pc, rrp);
0500 
0501         if (rc != NGX_BUSY) {
0502             return rc;
0503         }
0504 
0505         ngx_stream_upstream_rr_peers_wlock(peers);
0506     }
0507 
0508     ngx_stream_upstream_rr_peers_unlock(peers);
0509 
0510     pc->name = peers->name;
0511 
0512     return NGX_BUSY;
0513 }
0514 
0515 
0516 static ngx_stream_upstream_rr_peer_t *
0517 ngx_stream_upstream_get_peer(ngx_stream_upstream_rr_peer_data_t *rrp)
0518 {
0519     time_t                          now;
0520     uintptr_t                       m;
0521     ngx_int_t                       total;
0522     ngx_uint_t                      i, n, p;
0523     ngx_stream_upstream_rr_peer_t  *peer, *best;
0524 
0525     now = ngx_time();
0526 
0527     best = NULL;
0528     total = 0;
0529 
0530 #if (NGX_SUPPRESS_WARN)
0531     p = 0;
0532 #endif
0533 
0534     for (peer = rrp->peers->peer, i = 0;
0535          peer;
0536          peer = peer->next, i++)
0537     {
0538         n = i / (8 * sizeof(uintptr_t));
0539         m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
0540 
0541         if (rrp->tried[n] & m) {
0542             continue;
0543         }
0544 
0545         if (peer->down) {
0546             continue;
0547         }
0548 
0549         if (peer->max_fails
0550             && peer->fails >= peer->max_fails
0551             && now - peer->checked <= peer->fail_timeout)
0552         {
0553             continue;
0554         }
0555 
0556         if (peer->max_conns && peer->conns >= peer->max_conns) {
0557             continue;
0558         }
0559 
0560         peer->current_weight += peer->effective_weight;
0561         total += peer->effective_weight;
0562 
0563         if (peer->effective_weight < peer->weight) {
0564             peer->effective_weight++;
0565         }
0566 
0567         if (best == NULL || peer->current_weight > best->current_weight) {
0568             best = peer;
0569             p = i;
0570         }
0571     }
0572 
0573     if (best == NULL) {
0574         return NULL;
0575     }
0576 
0577     rrp->current = best;
0578 
0579     n = p / (8 * sizeof(uintptr_t));
0580     m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
0581 
0582     rrp->tried[n] |= m;
0583 
0584     best->current_weight -= total;
0585 
0586     if (now - best->checked > best->fail_timeout) {
0587         best->checked = now;
0588     }
0589 
0590     return best;
0591 }
0592 
0593 
0594 void
0595 ngx_stream_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data,
0596     ngx_uint_t state)
0597 {
0598     ngx_stream_upstream_rr_peer_data_t  *rrp = data;
0599 
0600     time_t                          now;
0601     ngx_stream_upstream_rr_peer_t  *peer;
0602 
0603     ngx_log_debug2(NGX_LOG_DEBUG_STREAM, pc->log, 0,
0604                    "free rr peer %ui %ui", pc->tries, state);
0605 
0606     peer = rrp->current;
0607 
0608     ngx_stream_upstream_rr_peers_rlock(rrp->peers);
0609     ngx_stream_upstream_rr_peer_lock(rrp->peers, peer);
0610 
0611     if (rrp->peers->single) {
0612         peer->conns--;
0613 
0614         ngx_stream_upstream_rr_peer_unlock(rrp->peers, peer);
0615         ngx_stream_upstream_rr_peers_unlock(rrp->peers);
0616 
0617         pc->tries = 0;
0618         return;
0619     }
0620 
0621     if (state & NGX_PEER_FAILED) {
0622         now = ngx_time();
0623 
0624         peer->fails++;
0625         peer->accessed = now;
0626         peer->checked = now;
0627 
0628         if (peer->max_fails) {
0629             peer->effective_weight -= peer->weight / peer->max_fails;
0630 
0631             if (peer->fails >= peer->max_fails) {
0632                 ngx_log_error(NGX_LOG_WARN, pc->log, 0,
0633                               "upstream server temporarily disabled");
0634             }
0635         }
0636 
0637         ngx_log_debug2(NGX_LOG_DEBUG_STREAM, pc->log, 0,
0638                        "free rr peer failed: %p %i",
0639                        peer, peer->effective_weight);
0640 
0641         if (peer->effective_weight < 0) {
0642             peer->effective_weight = 0;
0643         }
0644 
0645     } else {
0646 
0647         /* mark peer live if check passed */
0648 
0649         if (peer->accessed < peer->checked) {
0650             peer->fails = 0;
0651         }
0652     }
0653 
0654     peer->conns--;
0655 
0656     ngx_stream_upstream_rr_peer_unlock(rrp->peers, peer);
0657     ngx_stream_upstream_rr_peers_unlock(rrp->peers);
0658 
0659     if (pc->tries) {
0660         pc->tries--;
0661     }
0662 }
0663 
0664 
0665 static void
0666 ngx_stream_upstream_notify_round_robin_peer(ngx_peer_connection_t *pc,
0667     void *data, ngx_uint_t type)
0668 {
0669     ngx_stream_upstream_rr_peer_data_t  *rrp = data;
0670 
0671     ngx_stream_upstream_rr_peer_t  *peer;
0672 
0673     peer = rrp->current;
0674 
0675     if (type == NGX_STREAM_UPSTREAM_NOTIFY_CONNECT
0676         && pc->connection->type == SOCK_STREAM)
0677     {
0678         ngx_stream_upstream_rr_peers_rlock(rrp->peers);
0679         ngx_stream_upstream_rr_peer_lock(rrp->peers, peer);
0680 
0681         if (peer->accessed < peer->checked) {
0682             peer->fails = 0;
0683         }
0684 
0685         ngx_stream_upstream_rr_peer_unlock(rrp->peers, peer);
0686         ngx_stream_upstream_rr_peers_unlock(rrp->peers);
0687     }
0688 }
0689 
0690 
0691 #if (NGX_STREAM_SSL)
0692 
0693 static ngx_int_t
0694 ngx_stream_upstream_set_round_robin_peer_session(ngx_peer_connection_t *pc,
0695     void *data)
0696 {
0697     ngx_stream_upstream_rr_peer_data_t  *rrp = data;
0698 
0699     ngx_int_t                        rc;
0700     ngx_ssl_session_t               *ssl_session;
0701     ngx_stream_upstream_rr_peer_t   *peer;
0702 #if (NGX_STREAM_UPSTREAM_ZONE)
0703     int                              len;
0704 #if OPENSSL_VERSION_NUMBER >= 0x0090707fL
0705     const
0706 #endif
0707     u_char                          *p;
0708     ngx_stream_upstream_rr_peers_t  *peers;
0709     u_char                           buf[NGX_SSL_MAX_SESSION_SIZE];
0710 #endif
0711 
0712     peer = rrp->current;
0713 
0714 #if (NGX_STREAM_UPSTREAM_ZONE)
0715     peers = rrp->peers;
0716 
0717     if (peers->shpool) {
0718         ngx_stream_upstream_rr_peers_rlock(peers);
0719         ngx_stream_upstream_rr_peer_lock(peers, peer);
0720 
0721         if (peer->ssl_session == NULL) {
0722             ngx_stream_upstream_rr_peer_unlock(peers, peer);
0723             ngx_stream_upstream_rr_peers_unlock(peers);
0724             return NGX_OK;
0725         }
0726 
0727         len = peer->ssl_session_len;
0728 
0729         ngx_memcpy(buf, peer->ssl_session, len);
0730 
0731         ngx_stream_upstream_rr_peer_unlock(peers, peer);
0732         ngx_stream_upstream_rr_peers_unlock(peers);
0733 
0734         p = buf;
0735         ssl_session = d2i_SSL_SESSION(NULL, &p, len);
0736 
0737         rc = ngx_ssl_set_session(pc->connection, ssl_session);
0738 
0739         ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0,
0740                        "set session: %p", ssl_session);
0741 
0742         ngx_ssl_free_session(ssl_session);
0743 
0744         return rc;
0745     }
0746 #endif
0747 
0748     ssl_session = peer->ssl_session;
0749 
0750     rc = ngx_ssl_set_session(pc->connection, ssl_session);
0751 
0752     ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0,
0753                    "set session: %p", ssl_session);
0754 
0755     return rc;
0756 }
0757 
0758 
0759 static void
0760 ngx_stream_upstream_save_round_robin_peer_session(ngx_peer_connection_t *pc,
0761     void *data)
0762 {
0763     ngx_stream_upstream_rr_peer_data_t  *rrp = data;
0764 
0765     ngx_ssl_session_t               *old_ssl_session, *ssl_session;
0766     ngx_stream_upstream_rr_peer_t   *peer;
0767 #if (NGX_STREAM_UPSTREAM_ZONE)
0768     int                              len;
0769     u_char                          *p;
0770     ngx_stream_upstream_rr_peers_t  *peers;
0771     u_char                           buf[NGX_SSL_MAX_SESSION_SIZE];
0772 #endif
0773 
0774 #if (NGX_STREAM_UPSTREAM_ZONE)
0775     peers = rrp->peers;
0776 
0777     if (peers->shpool) {
0778 
0779         ssl_session = SSL_get0_session(pc->connection->ssl->connection);
0780 
0781         if (ssl_session == NULL) {
0782             return;
0783         }
0784 
0785         ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0,
0786                        "save session: %p", ssl_session);
0787 
0788         len = i2d_SSL_SESSION(ssl_session, NULL);
0789 
0790         /* do not cache too big session */
0791 
0792         if (len > NGX_SSL_MAX_SESSION_SIZE) {
0793             return;
0794         }
0795 
0796         p = buf;
0797         (void) i2d_SSL_SESSION(ssl_session, &p);
0798 
0799         peer = rrp->current;
0800 
0801         ngx_stream_upstream_rr_peers_rlock(peers);
0802         ngx_stream_upstream_rr_peer_lock(peers, peer);
0803 
0804         if (len > peer->ssl_session_len) {
0805             ngx_shmtx_lock(&peers->shpool->mutex);
0806 
0807             if (peer->ssl_session) {
0808                 ngx_slab_free_locked(peers->shpool, peer->ssl_session);
0809             }
0810 
0811             peer->ssl_session = ngx_slab_alloc_locked(peers->shpool, len);
0812 
0813             ngx_shmtx_unlock(&peers->shpool->mutex);
0814 
0815             if (peer->ssl_session == NULL) {
0816                 peer->ssl_session_len = 0;
0817 
0818                 ngx_stream_upstream_rr_peer_unlock(peers, peer);
0819                 ngx_stream_upstream_rr_peers_unlock(peers);
0820                 return;
0821             }
0822 
0823             peer->ssl_session_len = len;
0824         }
0825 
0826         ngx_memcpy(peer->ssl_session, buf, len);
0827 
0828         ngx_stream_upstream_rr_peer_unlock(peers, peer);
0829         ngx_stream_upstream_rr_peers_unlock(peers);
0830 
0831         return;
0832     }
0833 #endif
0834 
0835     ssl_session = ngx_ssl_get_session(pc->connection);
0836 
0837     if (ssl_session == NULL) {
0838         return;
0839     }
0840 
0841     ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0,
0842                    "save session: %p", ssl_session);
0843 
0844     peer = rrp->current;
0845 
0846     old_ssl_session = peer->ssl_session;
0847     peer->ssl_session = ssl_session;
0848 
0849     if (old_ssl_session) {
0850 
0851         ngx_log_debug1(NGX_LOG_DEBUG_STREAM, pc->log, 0,
0852                        "old session: %p", old_ssl_session);
0853 
0854         /* TODO: may block */
0855 
0856         ngx_ssl_free_session(old_ssl_session);
0857     }
0858 }
0859 
0860 
0861 static ngx_int_t
0862 ngx_stream_upstream_empty_set_session(ngx_peer_connection_t *pc, void *data)
0863 {
0864     return NGX_OK;
0865 }
0866 
0867 
0868 static void
0869 ngx_stream_upstream_empty_save_session(ngx_peer_connection_t *pc, void *data)
0870 {
0871     return;
0872 }
0873 
0874 #endif