2006-12-05 00:46:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Igor Sysoev
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_http.h>
|
|
|
|
|
|
|
|
|
2007-07-29 00:04:01 +08:00
|
|
|
static ngx_uint_t
|
|
|
|
ngx_http_upstream_get_peer(ngx_http_upstream_rr_peers_t *peers);
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
ngx_uint_t i, j, n;
|
|
|
|
ngx_http_upstream_server_t *server;
|
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;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t)
|
|
|
|
+ sizeof(ngx_http_upstream_rr_peer_t) * (n - 1));
|
|
|
|
if (peers == 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;
|
|
|
|
peers->name = &us->host;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < us->servers->nelts; i++) {
|
|
|
|
for (j = 0; j < server[i].naddrs; j++) {
|
2007-08-09 21:54:33 +08:00
|
|
|
if (server[i].backup) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
peers->peer[n].sockaddr = server[i].addrs[j].sockaddr;
|
|
|
|
peers->peer[n].socklen = server[i].addrs[j].socklen;
|
|
|
|
peers->peer[n].name = server[i].addrs[j].name;
|
|
|
|
peers->peer[n].max_fails = server[i].max_fails;
|
|
|
|
peers->peer[n].fail_timeout = server[i].fail_timeout;
|
|
|
|
peers->peer[n].down = server[i].down;
|
2007-08-07 20:34:20 +08:00
|
|
|
peers->peer[n].weight = server[i].down ? 0 : server[i].weight;
|
|
|
|
peers->peer[n].current_weight = peers->peer[n].weight;
|
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;
|
|
|
|
|
|
|
|
for (i = 0; i < us->servers->nelts; i++) {
|
|
|
|
if (!server[i].backup) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
n += server[i].naddrs;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
backup = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t)
|
|
|
|
+ sizeof(ngx_http_upstream_rr_peer_t) * (n - 1));
|
|
|
|
if (backup == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
peers->single = 0;
|
|
|
|
backup->single = 0;
|
|
|
|
backup->number = n;
|
|
|
|
backup->name = &us->host;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < us->servers->nelts; i++) {
|
|
|
|
for (j = 0; j < server[i].naddrs; j++) {
|
|
|
|
if (!server[i].backup) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
backup->peer[n].sockaddr = server[i].addrs[j].sockaddr;
|
|
|
|
backup->peer[n].socklen = server[i].addrs[j].socklen;
|
|
|
|
backup->peer[n].name = server[i].addrs[j].name;
|
|
|
|
backup->peer[n].weight = server[i].weight;
|
|
|
|
backup->peer[n].current_weight = server[i].weight;
|
|
|
|
backup->peer[n].max_fails = server[i].max_fails;
|
|
|
|
backup->peer[n].fail_timeout = server[i].fail_timeout;
|
|
|
|
backup->peer[n].down = server[i].down;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
peers->next = backup;
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* an upstream implicitly defined by proxy_pass, etc. */
|
|
|
|
|
2006-12-13 00:46:16 +08:00
|
|
|
if (us->port == 0 && us->default_port == 0) {
|
|
|
|
ngx_log_error(NGX_LOG_EMERG, cf->log, 0,
|
|
|
|
"no port in upstream \"%V\" in %s:%ui",
|
|
|
|
&us->host, us->file_name.data, us->line);
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
ngx_memzero(&u, sizeof(ngx_url_t));
|
|
|
|
|
|
|
|
u.host = us->host;
|
2006-12-13 23:06:46 +08:00
|
|
|
u.port = (in_port_t) (us->port ? us->port : us->default_port);
|
2006-12-05 00:46:13 +08:00
|
|
|
|
|
|
|
if (ngx_inet_resolve_host(cf, &u) != NGX_OK) {
|
|
|
|
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",
|
2006-12-05 00:46:13 +08:00
|
|
|
u.err, &us->host, us->file_name.data, us->line);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = u.naddrs;
|
|
|
|
|
|
|
|
peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t)
|
|
|
|
+ sizeof(ngx_http_upstream_rr_peer_t) * (n - 1));
|
|
|
|
if (peers == 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;
|
|
|
|
peers->name = &us->host;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < u.naddrs; i++) {
|
|
|
|
peers->peer[n].sockaddr = u.addrs[i].sockaddr;
|
|
|
|
peers->peer[n].socklen = u.addrs[i].socklen;
|
|
|
|
peers->peer[n].name = u.addrs[i].name;
|
|
|
|
peers->peer[n].weight = 1;
|
|
|
|
peers->peer[n].current_weight = 1;
|
|
|
|
peers->peer[n].max_fails = 1;
|
|
|
|
peers->peer[n].fail_timeout = 10;
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
rrp->current = 0;
|
|
|
|
|
|
|
|
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;
|
|
|
|
r->upstream->peer.free = ngx_http_upstream_free_round_robin_peer;
|
|
|
|
r->upstream->peer.tries = rrp->peers->number;
|
|
|
|
#if (NGX_HTTP_SSL)
|
2007-07-11 05:04:37 +08:00
|
|
|
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;
|
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
|
|
|
time_t now;
|
|
|
|
uintptr_t m;
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_uint_t i, n;
|
|
|
|
ngx_connection_t *c;
|
|
|
|
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);
|
|
|
|
|
|
|
|
now = ngx_time();
|
|
|
|
|
|
|
|
/* ngx_lock_mutex(rrp->peers->mutex); */
|
|
|
|
|
|
|
|
if (rrp->peers->last_cached) {
|
|
|
|
|
|
|
|
/* cached connection */
|
|
|
|
|
|
|
|
c = rrp->peers->cached[rrp->peers->last_cached];
|
|
|
|
rrp->peers->last_cached--;
|
|
|
|
|
|
|
|
/* ngx_unlock_mutex(ppr->peers->mutex); */
|
|
|
|
|
|
|
|
#if (NGX_THREADS)
|
|
|
|
c->read->lock = c->read->own_lock;
|
|
|
|
c->write->lock = c->write->own_lock;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
pc->connection = c;
|
|
|
|
pc->cached = 1;
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
pc->cached = 0;
|
|
|
|
pc->connection = NULL;
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
if (rrp->peers->single) {
|
2006-12-05 00:46:13 +08:00
|
|
|
peer = &rrp->peers->peer[0];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* there are several peers */
|
|
|
|
|
|
|
|
if (pc->tries == rrp->peers->number) {
|
|
|
|
|
|
|
|
/* it's a first try - get a current peer */
|
|
|
|
|
|
|
|
for ( ;; ) {
|
2007-07-29 00:04:01 +08:00
|
|
|
rrp->current = ngx_http_upstream_get_peer(rrp->peers);
|
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
|
|
"get rr peer, current: %ui %i",
|
|
|
|
rrp->current,
|
|
|
|
rrp->peers->peer[rrp->current].current_weight);
|
2006-12-05 00:46:13 +08:00
|
|
|
|
|
|
|
n = rrp->current / (8 * sizeof(uintptr_t));
|
2007-05-21 21:48:14 +08:00
|
|
|
m = (uintptr_t) 1 << rrp->current % (8 * sizeof(uintptr_t));
|
2006-12-05 00:46:13 +08:00
|
|
|
|
|
|
|
if (!(rrp->tried[n] & m)) {
|
|
|
|
peer = &rrp->peers->peer[rrp->current];
|
|
|
|
|
|
|
|
if (!peer->down) {
|
|
|
|
|
|
|
|
if (peer->max_fails == 0
|
|
|
|
|| peer->fails < peer->max_fails)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (now - peer->accessed > peer->fail_timeout) {
|
|
|
|
peer->fails = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-07-29 00:04:01 +08:00
|
|
|
peer->current_weight = 0;
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
} else {
|
|
|
|
rrp->tried[n] |= m;
|
|
|
|
}
|
|
|
|
|
|
|
|
pc->tries--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pc->tries) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer->current_weight--;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
for ( ;; ) {
|
|
|
|
n = rrp->current / (8 * sizeof(uintptr_t));
|
2007-05-21 21:48:14 +08:00
|
|
|
m = (uintptr_t) 1 << rrp->current % (8 * sizeof(uintptr_t));
|
2006-12-05 00:46:13 +08:00
|
|
|
|
|
|
|
if (!(rrp->tried[n] & m)) {
|
|
|
|
|
|
|
|
peer = &rrp->peers->peer[rrp->current];
|
|
|
|
|
|
|
|
if (!peer->down) {
|
|
|
|
|
|
|
|
if (peer->max_fails == 0
|
|
|
|
|| peer->fails < peer->max_fails)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (now - peer->accessed > peer->fail_timeout) {
|
|
|
|
peer->fails = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-07-29 00:04:01 +08:00
|
|
|
peer->current_weight = 0;
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
} else {
|
|
|
|
rrp->tried[n] |= m;
|
|
|
|
}
|
|
|
|
|
|
|
|
pc->tries--;
|
|
|
|
}
|
|
|
|
|
|
|
|
rrp->current++;
|
|
|
|
|
|
|
|
if (rrp->current >= rrp->peers->number) {
|
|
|
|
rrp->current = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pc->tries) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
peer->current_weight--;
|
|
|
|
}
|
|
|
|
|
|
|
|
rrp->tried[n] |= m;
|
|
|
|
}
|
|
|
|
|
|
|
|
pc->sockaddr = peer->sockaddr;
|
|
|
|
pc->socklen = peer->socklen;
|
|
|
|
pc->name = &peer->name;
|
|
|
|
|
|
|
|
/* ngx_unlock_mutex(rrp->peers->mutex); */
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
if (pc->tries == 1 && rrp->peers->next) {
|
|
|
|
pc->tries += rrp->peers->next->number;
|
|
|
|
|
|
|
|
n = rrp->peers->next->number / (8 * sizeof(uintptr_t)) + 1;
|
|
|
|
for (i = 0; i < n; n++) {
|
|
|
|
rrp->tried[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
return NGX_OK;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
peers = rrp->peers;
|
|
|
|
|
|
|
|
if (peers->next) {
|
|
|
|
|
|
|
|
/* ngx_unlock_mutex(peers->mutex); */
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0, "backup servers");
|
|
|
|
|
|
|
|
rrp->peers = peers->next;
|
|
|
|
pc->tries = rrp->peers->number;
|
|
|
|
|
|
|
|
n = rrp->peers->number / (8 * sizeof(uintptr_t)) + 1;
|
|
|
|
for (i = 0; i < n; n++) {
|
|
|
|
rrp->tried[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ngx_http_upstream_get_round_robin_peer(pc, rrp);
|
|
|
|
|
|
|
|
if (rc != NGX_BUSY) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ngx_lock_mutex(peers->mutex); */
|
|
|
|
}
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
/* all peers failed, mark them as live for quick recovery */
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
for (i = 0; i < peers->number; i++) {
|
|
|
|
peers->peer[i].fails = 0;
|
2006-12-05 00:46:13 +08:00
|
|
|
}
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
/* ngx_unlock_mutex(peers->mutex); */
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-29 00:04:01 +08:00
|
|
|
static ngx_uint_t
|
|
|
|
ngx_http_upstream_get_peer(ngx_http_upstream_rr_peers_t *peers)
|
|
|
|
{
|
|
|
|
ngx_uint_t i, n;
|
|
|
|
ngx_http_upstream_rr_peer_t *peer;
|
|
|
|
|
|
|
|
peer = &peers->peer[0];
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
for (i = 0; i < peers->number; i++) {
|
|
|
|
|
|
|
|
if (peer[i].current_weight <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = i;
|
|
|
|
|
|
|
|
while (i < peers->number - 1) {
|
|
|
|
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (peer[i].current_weight <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peer[n].current_weight * 1000 / peer[i].current_weight
|
|
|
|
>= peer[n].weight * 1000 / peer[i].weight)
|
|
|
|
{
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (peer[i].current_weight > 0) {
|
|
|
|
n = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < peers->number; i++) {
|
|
|
|
if (peer[i].fails == 0) {
|
|
|
|
peer[i].current_weight += peer[i].weight;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* 1 allows to go to quick recovery when all peers failed */
|
|
|
|
peer[i].current_weight = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (state == 0 && pc->tries == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: NGX_PEER_KEEPALIVE */
|
|
|
|
|
2007-08-09 21:54:33 +08:00
|
|
|
if (rrp->peers->single) {
|
2006-12-05 00:46:13 +08:00
|
|
|
pc->tries = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state & NGX_PEER_FAILED) {
|
|
|
|
now = ngx_time();
|
|
|
|
|
|
|
|
peer = &rrp->peers->peer[rrp->current];
|
|
|
|
|
|
|
|
/* ngx_lock_mutex(rrp->peers->mutex); */
|
|
|
|
|
|
|
|
peer->fails++;
|
|
|
|
peer->accessed = now;
|
|
|
|
|
2007-07-29 00:04:01 +08:00
|
|
|
peer->current_weight -= peer->weight / peer->max_fails;
|
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
|
|
"free rr peer failed: %ui %i",
|
|
|
|
rrp->current, peer->current_weight);
|
|
|
|
|
|
|
|
if (peer->current_weight < 0) {
|
|
|
|
peer->current_weight = 0;
|
2006-12-05 00:46:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ngx_unlock_mutex(rrp->peers->mutex); */
|
|
|
|
}
|
|
|
|
|
|
|
|
rrp->current++;
|
|
|
|
|
|
|
|
if (rrp->current >= rrp->peers->number) {
|
|
|
|
rrp->current = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pc->tries) {
|
|
|
|
pc->tries--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ngx_unlock_mutex(rrp->peers->mutex); */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
2007-07-11 05:04:37 +08:00
|
|
|
ngx_int_t rc;
|
2006-12-05 00:46:13 +08:00
|
|
|
ngx_ssl_session_t *ssl_session;
|
|
|
|
ngx_http_upstream_rr_peer_t *peer;
|
|
|
|
|
2007-07-11 05:04:37 +08:00
|
|
|
peer = &rrp->peers->peer[rrp->current];
|
|
|
|
|
|
|
|
/* TODO: threads only mutex */
|
|
|
|
/* ngx_lock_mutex(rrp->peers->mutex); */
|
|
|
|
|
|
|
|
ssl_session = peer->ssl_session;
|
|
|
|
|
|
|
|
rc = ngx_ssl_set_session(pc->connection, ssl_session);
|
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
|
|
"set session: %p:%d",
|
|
|
|
ssl_session, ssl_session ? ssl_session->references : 0);
|
|
|
|
|
|
|
|
/* ngx_unlock_mutex(rrp->peers->mutex); */
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
ngx_ssl_session_t *old_ssl_session, *ssl_session;
|
|
|
|
ngx_http_upstream_rr_peer_t *peer;
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
ssl_session = ngx_ssl_get_session(pc->connection);
|
|
|
|
|
|
|
|
if (ssl_session == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-07-11 05:04:37 +08:00
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
|
|
"save session: %p:%d", ssl_session, ssl_session->references);
|
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
peer = &rrp->peers->peer[rrp->current];
|
|
|
|
|
2007-07-11 05:04:37 +08:00
|
|
|
/* TODO: threads only mutex */
|
2006-12-05 00:46:13 +08:00
|
|
|
/* ngx_lock_mutex(rrp->peers->mutex); */
|
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
|
|
|
|
2006-12-05 00:46:13 +08:00
|
|
|
/* ngx_unlock_mutex(rrp->peers->mutex); */
|
|
|
|
|
2007-07-11 05:04:37 +08:00
|
|
|
if (old_ssl_session) {
|
|
|
|
|
|
|
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
|
|
"old session: %p:%d",
|
|
|
|
old_ssl_session, old_ssl_session->references);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|