2006-12-05 00:46:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Igor Sysoev
|
2012-01-18 23:07:43 +08:00
|
|
|
* Copyright (C) Nginx, Inc.
|
2006-12-05 00:46:13 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
|
2014-09-12 22:50:46 +08:00
|
|
|
#define ngx_http_upstream_tries(p) ((p)->number \
|
|
|
|
+ ((p)->next ? (p)->next->number : 0))
|
|
|
|
|
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
static ngx_http_upstream_rr_peer_t *ngx_http_upstream_get_peer(
|
|
|
|
ngx_http_upstream_rr_peer_data_t *rrp);
|
2007-07-29 00:04:01 +08:00
|
|
|
|
2011-07-23 00:30:17 +08:00
|
|
|
#if (NGX_HTTP_SSL)
|
|
|
|
|
|
|
|
static ngx_int_t ngx_http_upstream_empty_set_session(ngx_peer_connection_t *pc,
|
|
|
|
void *data);
|
|
|
|
static void ngx_http_upstream_empty_save_session(ngx_peer_connection_t *pc,
|
|
|
|
void *data);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2007-07-29 00:04:01 +08:00
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
|
|
|
|
ngx_http_upstream_srv_conf_t *us)
|
|
|
|
{
|
|
|
|
ngx_url_t u;
|
2012-06-04 07:22:41 +08:00
|
|
|
ngx_uint_t i, j, n, w;
|
2006-12-05 00:46:13 +08:00
|
|
|
ngx_http_upstream_server_t *server;
|
2015-04-10 19:48:36 +08:00
|
|
|
ngx_http_upstream_rr_peer_t *peer, **peerp;
|
2007-08-09 21:54:33 +08:00
|
|
|
ngx_http_upstream_rr_peers_t *peers, *backup;
|
2006-12-05 00:46:13 +08:00
|
|
|
|
|
|
|
us->peer.init = ngx_http_upstream_init_round_robin_peer;
|
|
|
|
|
|
|
|
if (us->servers) {
|
|
|
|
server = us->servers->elts;
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
n = 0;
|
2012-06-04 07:22:41 +08:00
|
|
|
w = 0;
|
2007-08-09 21:54:33 +08:00
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
for (i = 0; i < us->servers->nelts; i++) {
|
2007-08-09 21:54:33 +08:00
|
|
|
if (server[i].backup) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
n += server[i].naddrs;
|
2012-06-04 07:22:41 +08:00
|
|
|
w += server[i].naddrs * server[i].weight;
|
2006-12-05 00:46:13 +08:00
|
|
|
}
|
|
|
|
|
2012-04-03 05:29:35 +08:00
|
|
|
if (n == 0) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"no servers in upstream \"%V\" in %s:%ui",
|
|
|
|
&us->host, us->file_name, us->line);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t));
|
2006-12-05 00:46:13 +08:00
|
|
|
if (peers == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
peer = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peer_t) * n);
|
|
|
|
if (peer == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
peers->single = (n == 1);
|
2006-12-05 00:46:13 +08:00
|
|
|
peers->number = n;
|
2012-06-04 07:22:41 +08:00
|
|
|
peers->weighted = (w != n);
|
|
|
|
peers->total_weight = w;
|
2006-12-05 00:46:13 +08:00
|
|
|
peers->name = &us->host;
|
|
|
|
|
|
|
|
n = 0;
|
2015-04-10 19:48:36 +08:00
|
|
|
peerp = &peers->peer;
|
2006-12-05 00:46:13 +08:00
|
|
|
|
|
|
|
for (i = 0; i < us->servers->nelts; i++) {
|
2013-10-21 18:20:32 +08:00
|
|
|
if (server[i].backup) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-08-09 21:54:33 +08:00
|
|
|
|
2013-10-21 18:20:32 +08:00
|
|
|
for (j = 0; j < server[i].naddrs; j++) {
|
2014-06-13 01:13:24 +08:00
|
|
|
peer[n].sockaddr = server[i].addrs[j].sockaddr;
|
|
|
|
peer[n].socklen = server[i].addrs[j].socklen;
|
|
|
|
peer[n].name = server[i].addrs[j].name;
|
|
|
|
peer[n].weight = server[i].weight;
|
|
|
|
peer[n].effective_weight = server[i].weight;
|
|
|
|
peer[n].current_weight = 0;
|
|
|
|
peer[n].max_fails = server[i].max_fails;
|
|
|
|
peer[n].fail_timeout = server[i].fail_timeout;
|
|
|
|
peer[n].down = server[i].down;
|
|
|
|
peer[n].server = server[i].name;
|
2015-04-10 19:48:36 +08:00
|
|
|
|
|
|
|
*peerp = &peer[n];
|
|
|
|
peerp = &peer[n].next;
|
2006-12-05 00:46:13 +08:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
us->peer.data = peers;
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
/* backup servers */
|
|
|
|
|
|
|
|
n = 0;
|
2012-06-04 07:22:41 +08:00
|
|
|
w = 0;
|
2007-08-09 21:54:33 +08:00
|
|
|
|
|
|
|
for (i = 0; i < us->servers->nelts; i++) {
|
|
|
|
if (!server[i].backup) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
n += server[i].naddrs;
|
2012-06-04 07:22:41 +08:00
|
|
|
w += server[i].naddrs * server[i].weight;
|
2007-08-09 21:54:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
backup = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t));
|
2007-08-09 21:54:33 +08:00
|
|
|
if (backup == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
peer = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peer_t) * n);
|
|
|
|
if (peer == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
peers->single = 0;
|
|
|
|
backup->single = 0;
|
|
|
|
backup->number = n;
|
2012-06-04 07:22:41 +08:00
|
|
|
backup->weighted = (w != n);
|
|
|
|
backup->total_weight = w;
|
2007-08-09 21:54:33 +08:00
|
|
|
backup->name = &us->host;
|
|
|
|
|
|
|
|
n = 0;
|
2015-04-10 19:48:36 +08:00
|
|
|
peerp = &backup->peer;
|
2007-08-09 21:54:33 +08:00
|
|
|
|
|
|
|
for (i = 0; i < us->servers->nelts; i++) {
|
2013-10-21 18:20:32 +08:00
|
|
|
if (!server[i].backup) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-08-09 21:54:33 +08:00
|
|
|
|
2013-10-21 18:20:32 +08:00
|
|
|
for (j = 0; j < server[i].naddrs; j++) {
|
2014-06-13 01:13:24 +08:00
|
|
|
peer[n].sockaddr = server[i].addrs[j].sockaddr;
|
|
|
|
peer[n].socklen = server[i].addrs[j].socklen;
|
|
|
|
peer[n].name = server[i].addrs[j].name;
|
|
|
|
peer[n].weight = server[i].weight;
|
|
|
|
peer[n].effective_weight = server[i].weight;
|
|
|
|
peer[n].current_weight = 0;
|
|
|
|
peer[n].max_fails = server[i].max_fails;
|
|
|
|
peer[n].fail_timeout = server[i].fail_timeout;
|
|
|
|
peer[n].down = server[i].down;
|
|
|
|
peer[n].server = server[i].name;
|
2015-04-10 19:48:36 +08:00
|
|
|
|
|
|
|
*peerp = &peer[n];
|
|
|
|
peerp = &peer[n].next;
|
2007-08-09 21:54:33 +08:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
peers->next = backup;
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* an upstream implicitly defined by proxy_pass, etc. */
|
|
|
|
|
2013-01-10 20:58:55 +08:00
|
|
|
if (us->port == 0) {
|
2006-12-13 00:46:16 +08:00
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"no port in upstream \"%V\" in %s:%ui",
|
2007-09-16 01:11:06 +08:00
|
|
|
&us->host, us->file_name, us->line);
|
2006-12-13 00:46:16 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
ngx_memzero(&u, sizeof(ngx_url_t));
|
|
|
|
|
|
|
|
u.host = us->host;
|
2013-01-10 20:58:55 +08:00
|
|
|
u.port = us->port;
|
2006-12-05 00:46:13 +08:00
|
|
|
|
2007-10-08 16:55:12 +08:00
|
|
|
if (ngx_inet_resolve_host(cf->pool, &u) != NGX_OK) {
|
2006-12-05 00:46:13 +08:00
|
|
|
if (u.err) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
2006-12-11 16:59:30 +08:00
|
|
|
"%s in upstream \"%V\" in %s:%ui",
|
2007-09-16 01:11:06 +08:00
|
|
|
u.err, &us->host, us->file_name, us->line);
|
2006-12-05 00:46:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = u.naddrs;
|
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t));
|
2006-12-05 00:46:13 +08:00
|
|
|
if (peers == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
peer = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peer_t) * n);
|
|
|
|
if (peer == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
peers->single = (n == 1);
|
2006-12-05 00:46:13 +08:00
|
|
|
peers->number = n;
|
2012-06-04 07:22:41 +08:00
|
|
|
peers->weighted = 0;
|
|
|
|
peers->total_weight = n;
|
2006-12-05 00:46:13 +08:00
|
|
|
peers->name = &us->host;
|
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
peerp = &peers->peer;
|
2014-06-13 01:13:24 +08:00
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
for (i = 0; i < u.naddrs; i++) {
|
2014-06-13 01:13:24 +08:00
|
|
|
peer[i].sockaddr = u.addrs[i].sockaddr;
|
|
|
|
peer[i].socklen = u.addrs[i].socklen;
|
|
|
|
peer[i].name = u.addrs[i].name;
|
|
|
|
peer[i].weight = 1;
|
|
|
|
peer[i].effective_weight = 1;
|
|
|
|
peer[i].current_weight = 0;
|
|
|
|
peer[i].max_fails = 1;
|
|
|
|
peer[i].fail_timeout = 10;
|
2015-04-10 19:48:36 +08:00
|
|
|
*peerp = &peer[i];
|
|
|
|
peerp = &peer[i].next;
|
2006-12-05 00:46:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
us->peer.data = peers;
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
/* implicitly defined upstream has no backup servers */
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t
|
|
|
|
ngx_http_upstream_init_round_robin_peer(ngx_http_request_t *r,
|
|
|
|
ngx_http_upstream_srv_conf_t *us)
|
|
|
|
{
|
|
|
|
ngx_uint_t n;
|
|
|
|
ngx_http_upstream_rr_peer_data_t *rrp;
|
|
|
|
|
|
|
|
rrp = r->upstream->peer.data;
|
|
|
|
|
|
|
|
if (rrp == NULL) {
|
|
|
|
rrp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_rr_peer_data_t));
|
|
|
|
if (rrp == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->upstream->peer.data = rrp;
|
|
|
|
}
|
|
|
|
|
|
|
|
rrp->peers = us->peer.data;
|
2015-04-10 19:48:36 +08:00
|
|
|
rrp->current = NULL;
|
2006-12-05 00:46:13 +08:00
|
|
|
|
2011-08-19 01:04:52 +08:00
|
|
|
n = rrp->peers->number;
|
|
|
|
|
|
|
|
if (rrp->peers->next && rrp->peers->next->number > n) {
|
|
|
|
n = rrp->peers->next->number;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n <= 8 * sizeof(uintptr_t)) {
|
2006-12-05 00:46:13 +08:00
|
|
|
rrp->tried = &rrp->data;
|
|
|
|
rrp->data = 0;
|
|
|
|
|
|
|
|
} else {
|
2011-08-19 01:04:52 +08:00
|
|
|
n = (n + (8 * sizeof(uintptr_t) - 1)) / (8 * sizeof(uintptr_t));
|
2006-12-05 00:46:13 +08:00
|
|
|
|
|
|
|
rrp->tried = ngx_pcalloc(r->pool, n * sizeof(uintptr_t));
|
|
|
|
if (rrp->tried == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r->upstream->peer.get = ngx_http_upstream_get_round_robin_peer;
|
2007-11-27 21:34:13 +08:00
|
|
|
r->upstream->peer.free = ngx_http_upstream_free_round_robin_peer;
|
2014-09-12 22:50:46 +08:00
|
|
|
r->upstream->peer.tries = ngx_http_upstream_tries(rrp->peers);
|
2007-11-27 21:34:13 +08:00
|
|
|
#if (NGX_HTTP_SSL)
|
|
|
|
r->upstream->peer.set_session =
|
|
|
|
ngx_http_upstream_set_round_robin_peer_session;
|
|
|
|
r->upstream->peer.save_session =
|
|
|
|
ngx_http_upstream_save_round_robin_peer_session;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t
|
|
|
|
ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
|
|
|
|
ngx_http_upstream_resolved_t *ur)
|
|
|
|
{
|
|
|
|
u_char *p;
|
|
|
|
size_t len;
|
2013-12-06 18:30:27 +08:00
|
|
|
socklen_t socklen;
|
2007-11-27 21:34:13 +08:00
|
|
|
ngx_uint_t i, n;
|
2013-12-06 18:30:27 +08:00
|
|
|
struct sockaddr *sockaddr;
|
2015-04-10 19:48:36 +08:00
|
|
|
ngx_http_upstream_rr_peer_t *peer, **peerp;
|
2007-11-27 21:34:13 +08:00
|
|
|
ngx_http_upstream_rr_peers_t *peers;
|
|
|
|
ngx_http_upstream_rr_peer_data_t *rrp;
|
|
|
|
|
|
|
|
rrp = r->upstream->peer.data;
|
|
|
|
|
|
|
|
if (rrp == NULL) {
|
|
|
|
rrp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_rr_peer_data_t));
|
|
|
|
if (rrp == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->upstream->peer.data = rrp;
|
|
|
|
}
|
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
peers = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peers_t));
|
2007-11-27 21:34:13 +08:00
|
|
|
if (peers == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
peer = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peer_t)
|
|
|
|
* ur->naddrs);
|
|
|
|
if (peer == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-11-27 21:34:13 +08:00
|
|
|
peers->single = (ur->naddrs == 1);
|
|
|
|
peers->number = ur->naddrs;
|
|
|
|
peers->name = &ur->host;
|
|
|
|
|
2008-12-24 03:35:12 +08:00
|
|
|
if (ur->sockaddr) {
|
2014-06-13 01:13:24 +08:00
|
|
|
peer[0].sockaddr = ur->sockaddr;
|
|
|
|
peer[0].socklen = ur->socklen;
|
|
|
|
peer[0].name = ur->host;
|
|
|
|
peer[0].weight = 1;
|
|
|
|
peer[0].effective_weight = 1;
|
|
|
|
peer[0].current_weight = 0;
|
|
|
|
peer[0].max_fails = 1;
|
|
|
|
peer[0].fail_timeout = 10;
|
2015-04-10 19:48:36 +08:00
|
|
|
peers->peer = peer;
|
2007-11-27 21:34:13 +08:00
|
|
|
|
2008-12-24 03:35:12 +08:00
|
|
|
} else {
|
2015-04-10 19:48:36 +08:00
|
|
|
peerp = &peers->peer;
|
2007-11-27 21:34:13 +08:00
|
|
|
|
2008-12-24 03:35:12 +08:00
|
|
|
for (i = 0; i < ur->naddrs; i++) {
|
2007-11-27 21:34:13 +08:00
|
|
|
|
2013-12-06 18:30:27 +08:00
|
|
|
socklen = ur->addrs[i].socklen;
|
2007-11-27 21:34:13 +08:00
|
|
|
|
2013-12-06 18:30:27 +08:00
|
|
|
sockaddr = ngx_palloc(r->pool, socklen);
|
|
|
|
if (sockaddr == NULL) {
|
2008-12-24 03:35:12 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
2007-11-27 21:34:13 +08:00
|
|
|
|
2013-12-06 18:30:27 +08:00
|
|
|
ngx_memcpy(sockaddr, ur->addrs[i].sockaddr, socklen);
|
2007-11-27 21:34:13 +08:00
|
|
|
|
2013-12-06 18:30:27 +08:00
|
|
|
switch (sockaddr->sa_family) {
|
|
|
|
#if (NGX_HAVE_INET6)
|
|
|
|
case AF_INET6:
|
|
|
|
((struct sockaddr_in6 *) sockaddr)->sin6_port = htons(ur->port);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default: /* AF_INET */
|
|
|
|
((struct sockaddr_in *) sockaddr)->sin_port = htons(ur->port);
|
|
|
|
}
|
|
|
|
|
|
|
|
p = ngx_pnalloc(r->pool, NGX_SOCKADDR_STRLEN);
|
|
|
|
if (p == NULL) {
|
2008-12-24 03:35:12 +08:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2013-12-06 18:30:27 +08:00
|
|
|
len = ngx_sock_ntop(sockaddr, socklen, p, NGX_SOCKADDR_STRLEN, 1);
|
2008-12-24 03:35:12 +08:00
|
|
|
|
2014-06-13 01:13:24 +08:00
|
|
|
peer[i].sockaddr = sockaddr;
|
|
|
|
peer[i].socklen = socklen;
|
|
|
|
peer[i].name.len = len;
|
|
|
|
peer[i].name.data = p;
|
|
|
|
peer[i].weight = 1;
|
|
|
|
peer[i].effective_weight = 1;
|
|
|
|
peer[i].current_weight = 0;
|
|
|
|
peer[i].max_fails = 1;
|
|
|
|
peer[i].fail_timeout = 10;
|
2015-04-10 19:48:36 +08:00
|
|
|
*peerp = &peer[i];
|
|
|
|
peerp = &peer[i].next;
|
2008-12-24 03:35:12 +08:00
|
|
|
}
|
2007-11-27 21:34:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rrp->peers = peers;
|
2015-04-10 19:48:36 +08:00
|
|
|
rrp->current = NULL;
|
2007-11-27 21:34:13 +08:00
|
|
|
|
|
|
|
if (rrp->peers->number <= 8 * sizeof(uintptr_t)) {
|
|
|
|
rrp->tried = &rrp->data;
|
|
|
|
rrp->data = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1))
|
|
|
|
/ (8 * sizeof(uintptr_t));
|
|
|
|
|
|
|
|
rrp->tried = ngx_pcalloc(r->pool, n * sizeof(uintptr_t));
|
|
|
|
if (rrp->tried == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r->upstream->peer.get = ngx_http_upstream_get_round_robin_peer;
|
2006-12-05 00:46:13 +08:00
|
|
|
r->upstream->peer.free = ngx_http_upstream_free_round_robin_peer;
|
2014-09-12 22:50:46 +08:00
|
|
|
r->upstream->peer.tries = ngx_http_upstream_tries(rrp->peers);
|
2006-12-05 00:46:13 +08:00
|
|
|
#if (NGX_HTTP_SSL)
|
2011-07-23 00:30:17 +08:00
|
|
|
r->upstream->peer.set_session = ngx_http_upstream_empty_set_session;
|
|
|
|
r->upstream->peer.save_session = ngx_http_upstream_empty_save_session;
|
2006-12-05 00:46:13 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t
|
|
|
|
ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data)
|
|
|
|
{
|
|
|
|
ngx_http_upstream_rr_peer_data_t *rrp = data;
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_uint_t i, n;
|
|
|
|
ngx_http_upstream_rr_peer_t *peer;
|
|
|
|
ngx_http_upstream_rr_peers_t *peers;
|
2006-12-05 00:46:13 +08:00
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
|
|
"get rr peer, try: %ui", pc->tries);
|
|
|
|
|
|
|
|
pc->cached = 0;
|
|
|
|
pc->connection = NULL;
|
|
|
|
|
2014-06-13 01:13:24 +08:00
|
|
|
peers = rrp->peers;
|
2015-04-15 00:01:23 +08:00
|
|
|
ngx_http_upstream_rr_peers_wlock(peers);
|
2014-06-13 01:13:24 +08:00
|
|
|
|
|
|
|
if (peers->single) {
|
2015-04-10 19:48:36 +08:00
|
|
|
peer = peers->peer;
|
2006-12-05 00:46:13 +08:00
|
|
|
|
2012-11-16 20:18:05 +08:00
|
|
|
if (peer->down) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
rrp->current = peer;
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
} else {
|
|
|
|
|
|
|
|
/* there are several peers */
|
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
peer = ngx_http_upstream_get_peer(rrp);
|
2006-12-05 00:46:13 +08:00
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
if (peer == NULL) {
|
|
|
|
goto failed;
|
2006-12-05 00:46:13 +08:00
|
|
|
}
|
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
2015-04-10 19:48:36 +08:00
|
|
|
"get rr peer, current: %p %i",
|
|
|
|
peer, peer->current_weight);
|
2006-12-05 00:46:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
pc->sockaddr = peer->sockaddr;
|
|
|
|
pc->socklen = peer->socklen;
|
|
|
|
pc->name = &peer->name;
|
|
|
|
|
2015-04-10 18:16:23 +08:00
|
|
|
peer->conns++;
|
|
|
|
|
2015-04-15 00:01:23 +08:00
|
|
|
ngx_http_upstream_rr_peers_unlock(peers);
|
2006-12-05 00:46:13 +08:00
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
if (peers->next) {
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, "backup servers");
|
|
|
|
|
|
|
|
rrp->peers = peers->next;
|
|
|
|
|
2012-12-25 16:02:21 +08:00
|
|
|
n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1))
|
|
|
|
/ (8 * sizeof(uintptr_t));
|
|
|
|
|
2007-08-14 21:35:52 +08:00
|
|
|
for (i = 0; i < n; i++) {
|
2016-03-30 16:52:16 +08:00
|
|
|
rrp->tried[i] = 0;
|
2007-08-09 21:54:33 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 00:01:23 +08:00
|
|
|
ngx_http_upstream_rr_peers_unlock(peers);
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
rc = ngx_http_upstream_get_round_robin_peer(pc, rrp);
|
|
|
|
|
|
|
|
if (rc != NGX_BUSY) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-04-15 00:01:23 +08:00
|
|
|
ngx_http_upstream_rr_peers_wlock(peers);
|
2007-08-09 21:54:33 +08:00
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
/* all peers failed, mark them as live for quick recovery */
|
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
for (peer = peers->peer; peer; peer = peer->next) {
|
|
|
|
peer->fails = 0;
|
2006-12-05 00:46:13 +08:00
|
|
|
}
|
|
|
|
|
2015-04-15 00:01:23 +08:00
|
|
|
ngx_http_upstream_rr_peers_unlock(peers);
|
2006-12-05 00:46:13 +08:00
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
pc->name = peers->name;
|
2006-12-05 00:46:13 +08:00
|
|
|
|
|
|
|
return NGX_BUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
static ngx_http_upstream_rr_peer_t *
|
|
|
|
ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp)
|
2007-07-29 00:04:01 +08:00
|
|
|
{
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
time_t now;
|
|
|
|
uintptr_t m;
|
|
|
|
ngx_int_t total;
|
2015-04-10 19:48:36 +08:00
|
|
|
ngx_uint_t i, n, p;
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
ngx_http_upstream_rr_peer_t *peer, *best;
|
2007-07-29 00:04:01 +08:00
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
now = ngx_time();
|
2007-07-29 00:04:01 +08:00
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
best = NULL;
|
|
|
|
total = 0;
|
2007-07-29 00:04:01 +08:00
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
#if (NGX_SUPPRESS_WARN)
|
|
|
|
p = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (peer = rrp->peers->peer, i = 0;
|
|
|
|
peer;
|
|
|
|
peer = peer->next, i++)
|
|
|
|
{
|
2007-07-29 00:04:01 +08:00
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
n = i / (8 * sizeof(uintptr_t));
|
|
|
|
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
2007-07-29 00:04:01 +08:00
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
if (rrp->tried[n] & m) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-07-29 00:04:01 +08:00
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
if (peer->down) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-07-29 00:04:01 +08:00
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
if (peer->max_fails
|
|
|
|
&& peer->fails >= peer->max_fails
|
|
|
|
&& now - peer->checked <= peer->fail_timeout)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2007-07-29 00:04:01 +08:00
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
peer->current_weight += peer->effective_weight;
|
|
|
|
total += peer->effective_weight;
|
2007-07-29 00:04:01 +08:00
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
if (peer->effective_weight < peer->weight) {
|
|
|
|
peer->effective_weight++;
|
2007-07-29 00:04:01 +08:00
|
|
|
}
|
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
if (best == NULL || peer->current_weight > best->current_weight) {
|
|
|
|
best = peer;
|
2015-04-10 19:48:36 +08:00
|
|
|
p = i;
|
2011-08-19 00:52:38 +08:00
|
|
|
}
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
}
|
2011-08-19 00:52:38 +08:00
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
if (best == NULL) {
|
|
|
|
return NULL;
|
2007-07-29 00:04:01 +08:00
|
|
|
}
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
rrp->current = best;
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
n = p / (8 * sizeof(uintptr_t));
|
|
|
|
m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
|
|
|
|
rrp->tried[n] |= m;
|
|
|
|
|
|
|
|
best->current_weight -= total;
|
2013-05-22 01:47:50 +08:00
|
|
|
|
|
|
|
if (now - best->checked > best->fail_timeout) {
|
|
|
|
best->checked = now;
|
|
|
|
}
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
|
|
|
|
return best;
|
2007-07-29 00:04:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
void
|
|
|
|
ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data,
|
|
|
|
ngx_uint_t state)
|
|
|
|
{
|
|
|
|
ngx_http_upstream_rr_peer_data_t *rrp = data;
|
|
|
|
|
|
|
|
time_t now;
|
|
|
|
ngx_http_upstream_rr_peer_t *peer;
|
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
|
|
"free rr peer %ui %ui", pc->tries, state);
|
|
|
|
|
|
|
|
/* TODO: NGX_PEER_KEEPALIVE */
|
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
peer = rrp->current;
|
2015-04-10 18:16:23 +08:00
|
|
|
|
2015-04-20 03:41:09 +08:00
|
|
|
ngx_http_upstream_rr_peers_rlock(rrp->peers);
|
|
|
|
ngx_http_upstream_rr_peer_lock(rrp->peers, peer);
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
if (rrp->peers->single) {
|
2015-04-10 18:16:23 +08:00
|
|
|
|
|
|
|
peer->conns--;
|
|
|
|
|
2015-04-20 03:41:09 +08:00
|
|
|
ngx_http_upstream_rr_peer_unlock(rrp->peers, peer);
|
|
|
|
ngx_http_upstream_rr_peers_unlock(rrp->peers);
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
pc->tries = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state & NGX_PEER_FAILED) {
|
|
|
|
now = ngx_time();
|
|
|
|
|
|
|
|
peer->fails++;
|
|
|
|
peer->accessed = now;
|
2011-10-12 22:22:48 +08:00
|
|
|
peer->checked = now;
|
2006-12-05 00:46:13 +08:00
|
|
|
|
2008-08-26 22:34:16 +08:00
|
|
|
if (peer->max_fails) {
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
peer->effective_weight -= peer->weight / peer->max_fails;
|
2015-05-21 03:44:00 +08:00
|
|
|
|
|
|
|
if (peer->fails >= peer->max_fails) {
|
|
|
|
ngx_log_error(NGX_LOG_WARN, pc->log, 0,
|
|
|
|
"upstream server temporarily disabled");
|
|
|
|
}
|
2008-08-26 22:34:16 +08:00
|
|
|
}
|
2007-07-29 00:04:01 +08:00
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
2015-04-10 19:48:36 +08:00
|
|
|
"free rr peer failed: %p %i",
|
|
|
|
peer, peer->effective_weight);
|
2007-07-29 00:04:01 +08:00
|
|
|
|
Upstream: smooth weighted round-robin balancing.
For edge case weights like { 5, 1, 1 } we now produce { a, a, b, a, c, a, a }
sequence instead of { c, b, a, a, a, a, a } produced previously.
Algorithm is as follows: on each peer selection we increase current_weight
of each eligible peer by its weight, select peer with greatest current_weight
and reduce its current_weight by total number of weight points distributed
among peers.
In case of { 5, 1, 1 } weights this gives the following sequence of
current_weight's:
a b c
0 0 0 (initial state)
5 1 1 (a selected)
-2 1 1
3 2 2 (a selected)
-4 2 2
1 3 3 (b selected)
1 -4 3
6 -3 4 (a selected)
-1 -3 4
4 -2 5 (c selected)
4 -2 -2
9 -1 -1 (a selected)
2 -1 -1
7 0 0 (a selected)
0 0 0
To preserve weight reduction in case of failures the effective_weight
variable was introduced, which usually matches peer's weight, but is
reduced temporarily on peer failures.
This change also fixes loop with backup servers and proxy_next_upstream
http_404 (ticket #47), and skipping alive upstreams in some cases if there
are multiple dead ones (ticket #64).
2012-05-14 17:57:20 +08:00
|
|
|
if (peer->effective_weight < 0) {
|
|
|
|
peer->effective_weight = 0;
|
2006-12-05 00:46:13 +08:00
|
|
|
}
|
|
|
|
|
2011-10-12 22:22:48 +08:00
|
|
|
} else {
|
|
|
|
|
|
|
|
/* mark peer live if check passed */
|
|
|
|
|
|
|
|
if (peer->accessed < peer->checked) {
|
|
|
|
peer->fails = 0;
|
|
|
|
}
|
2006-12-05 00:46:13 +08:00
|
|
|
}
|
|
|
|
|
2015-04-10 18:16:23 +08:00
|
|
|
peer->conns--;
|
|
|
|
|
2015-04-15 00:01:23 +08:00
|
|
|
ngx_http_upstream_rr_peer_unlock(rrp->peers, peer);
|
|
|
|
ngx_http_upstream_rr_peers_unlock(rrp->peers);
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
if (pc->tries) {
|
|
|
|
pc->tries--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if (NGX_HTTP_SSL)
|
|
|
|
|
2007-07-11 05:04:37 +08:00
|
|
|
ngx_int_t
|
|
|
|
ngx_http_upstream_set_round_robin_peer_session(ngx_peer_connection_t *pc,
|
|
|
|
void *data)
|
2006-12-05 00:46:13 +08:00
|
|
|
{
|
|
|
|
ngx_http_upstream_rr_peer_data_t *rrp = data;
|
|
|
|
|
2015-04-15 00:01:25 +08:00
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_ssl_session_t *ssl_session;
|
|
|
|
ngx_http_upstream_rr_peer_t *peer;
|
|
|
|
#if (NGX_HTTP_UPSTREAM_ZONE)
|
|
|
|
int len;
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x0090707fL
|
|
|
|
const
|
|
|
|
#endif
|
|
|
|
u_char *p;
|
|
|
|
ngx_http_upstream_rr_peers_t *peers;
|
|
|
|
u_char buf[NGX_SSL_MAX_SESSION_SIZE];
|
|
|
|
#endif
|
2006-12-05 00:46:13 +08:00
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
peer = rrp->current;
|
2007-07-11 05:04:37 +08:00
|
|
|
|
2015-04-15 00:01:25 +08:00
|
|
|
#if (NGX_HTTP_UPSTREAM_ZONE)
|
|
|
|
peers = rrp->peers;
|
|
|
|
|
|
|
|
if (peers->shpool) {
|
|
|
|
ngx_http_upstream_rr_peers_rlock(peers);
|
|
|
|
ngx_http_upstream_rr_peer_lock(peers, peer);
|
|
|
|
|
|
|
|
if (peer->ssl_session == NULL) {
|
|
|
|
ngx_http_upstream_rr_peer_unlock(peers, peer);
|
|
|
|
ngx_http_upstream_rr_peers_unlock(peers);
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
len = peer->ssl_session_len;
|
|
|
|
|
|
|
|
ngx_memcpy(buf, peer->ssl_session, len);
|
|
|
|
|
|
|
|
ngx_http_upstream_rr_peer_unlock(peers, peer);
|
|
|
|
ngx_http_upstream_rr_peers_unlock(peers);
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
ssl_session = d2i_SSL_SESSION(NULL, &p, len);
|
|
|
|
|
|
|
|
rc = ngx_ssl_set_session(pc->connection, ssl_session);
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
|
|
"set session: %p", ssl_session);
|
|
|
|
|
|
|
|
ngx_ssl_free_session(ssl_session);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-07-11 05:04:37 +08:00
|
|
|
ssl_session = peer->ssl_session;
|
|
|
|
|
|
|
|
rc = ngx_ssl_set_session(pc->connection, ssl_session);
|
|
|
|
|
2014-07-07 07:41:14 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
|
|
"set session: %p", ssl_session);
|
2007-07-11 05:04:37 +08:00
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ngx_http_upstream_save_round_robin_peer_session(ngx_peer_connection_t *pc,
|
|
|
|
void *data)
|
|
|
|
{
|
|
|
|
ngx_http_upstream_rr_peer_data_t *rrp = data;
|
|
|
|
|
2015-04-15 00:01:25 +08:00
|
|
|
ngx_ssl_session_t *old_ssl_session, *ssl_session;
|
|
|
|
ngx_http_upstream_rr_peer_t *peer;
|
|
|
|
#if (NGX_HTTP_UPSTREAM_ZONE)
|
|
|
|
int len;
|
|
|
|
u_char *p;
|
|
|
|
ngx_http_upstream_rr_peers_t *peers;
|
|
|
|
u_char buf[NGX_SSL_MAX_SESSION_SIZE];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (NGX_HTTP_UPSTREAM_ZONE)
|
|
|
|
peers = rrp->peers;
|
|
|
|
|
|
|
|
if (peers->shpool) {
|
|
|
|
|
|
|
|
ssl_session = SSL_get0_session(pc->connection->ssl->connection);
|
|
|
|
|
|
|
|
if (ssl_session == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
|
|
"save session: %p", ssl_session);
|
|
|
|
|
|
|
|
len = i2d_SSL_SESSION(ssl_session, NULL);
|
|
|
|
|
|
|
|
/* do not cache too big session */
|
|
|
|
|
|
|
|
if (len > NGX_SSL_MAX_SESSION_SIZE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
(void) i2d_SSL_SESSION(ssl_session, &p);
|
|
|
|
|
|
|
|
peer = rrp->current;
|
|
|
|
|
|
|
|
ngx_http_upstream_rr_peers_rlock(peers);
|
|
|
|
ngx_http_upstream_rr_peer_lock(peers, peer);
|
|
|
|
|
|
|
|
if (len > peer->ssl_session_len) {
|
|
|
|
ngx_shmtx_lock(&peers->shpool->mutex);
|
|
|
|
|
|
|
|
if (peer->ssl_session) {
|
|
|
|
ngx_slab_free_locked(peers->shpool, peer->ssl_session);
|
|
|
|
}
|
|
|
|
|
|
|
|
peer->ssl_session = ngx_slab_alloc_locked(peers->shpool, len);
|
|
|
|
|
|
|
|
ngx_shmtx_unlock(&peers->shpool->mutex);
|
|
|
|
|
|
|
|
if (peer->ssl_session == NULL) {
|
|
|
|
peer->ssl_session_len = 0;
|
|
|
|
|
|
|
|
ngx_http_upstream_rr_peer_unlock(peers, peer);
|
|
|
|
ngx_http_upstream_rr_peers_unlock(peers);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer->ssl_session_len = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_memcpy(peer->ssl_session, buf, len);
|
|
|
|
|
|
|
|
ngx_http_upstream_rr_peer_unlock(peers, peer);
|
|
|
|
ngx_http_upstream_rr_peers_unlock(peers);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2007-07-11 05:04:37 +08:00
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
ssl_session = ngx_ssl_get_session(pc->connection);
|
|
|
|
|
|
|
|
if (ssl_session == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-07 07:41:14 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
|
|
"save session: %p", ssl_session);
|
2007-07-11 05:04:37 +08:00
|
|
|
|
2015-04-10 19:48:36 +08:00
|
|
|
peer = rrp->current;
|
2006-12-05 00:46:13 +08:00
|
|
|
|
2007-07-11 05:04:37 +08:00
|
|
|
old_ssl_session = peer->ssl_session;
|
2006-12-05 00:46:13 +08:00
|
|
|
peer->ssl_session = ssl_session;
|
2007-07-11 05:04:37 +08:00
|
|
|
|
|
|
|
if (old_ssl_session) {
|
|
|
|
|
2014-07-07 07:41:14 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
|
|
"old session: %p", old_ssl_session);
|
2007-07-11 05:04:37 +08:00
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
/* TODO: may block */
|
2007-07-11 05:04:37 +08:00
|
|
|
|
|
|
|
ngx_ssl_free_session(old_ssl_session);
|
2006-12-05 00:46:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-23 00:30:17 +08:00
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_http_upstream_empty_set_session(ngx_peer_connection_t *pc, void *data)
|
|
|
|
{
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_http_upstream_empty_save_session(ngx_peer_connection_t *pc, void *data)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
#endif
|