mirror of
https://github.com/nginx/nginx.git
synced 2024-12-19 21:53:31 -06:00
Upstream: store peers as a linked list.
This is an API change.
This commit is contained in:
parent
18fa775b10
commit
4d53631022
@ -211,11 +211,19 @@ ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
if (!hp->rrp.peers->weighted) {
|
if (!hp->rrp.peers->weighted) {
|
||||||
p = hp->hash % hp->rrp.peers->number;
|
p = hp->hash % hp->rrp.peers->number;
|
||||||
|
|
||||||
|
peer = hp->rrp.peers->peer;
|
||||||
|
for (i = 0; i < p; i++) {
|
||||||
|
peer = peer->next;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
w = hp->hash % hp->rrp.peers->total_weight;
|
w = hp->hash % hp->rrp.peers->total_weight;
|
||||||
|
|
||||||
for (i = 0; i < hp->rrp.peers->number; i++) {
|
for (peer = hp->rrp.peers->peer, i = 0;
|
||||||
w -= hp->rrp.peers->peer[i].weight;
|
peer;
|
||||||
|
peer = peer->next, i++)
|
||||||
|
{
|
||||||
|
w -= peer->weight;
|
||||||
if (w < 0) {
|
if (w < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -234,8 +242,6 @@ ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
||||||
"get hash peer, value:%uD, peer:%ui", hp->hash, p);
|
"get hash peer, value:%uD, peer:%ui", hp->hash, p);
|
||||||
|
|
||||||
peer = &hp->rrp.peers->peer[p];
|
|
||||||
|
|
||||||
if (peer->down) {
|
if (peer->down) {
|
||||||
goto next;
|
goto next;
|
||||||
}
|
}
|
||||||
@ -256,7 +262,7 @@ ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
hp->rrp.current = p;
|
hp->rrp.current = peer;
|
||||||
|
|
||||||
pc->sockaddr = peer->sockaddr;
|
pc->sockaddr = peer->sockaddr;
|
||||||
pc->socklen = peer->socklen;
|
pc->socklen = peer->socklen;
|
||||||
@ -306,8 +312,7 @@ ngx_http_upstream_init_chash(ngx_conf_t *cf, ngx_http_upstream_srv_conf_t *us)
|
|||||||
|
|
||||||
points->number = 0;
|
points->number = 0;
|
||||||
|
|
||||||
for (i = 0; i < peers->number; i++) {
|
for (peer = peers->peer; peer; peer = peer->next) {
|
||||||
peer = &peers->peer[i];
|
|
||||||
server = &peer->server;
|
server = &peer->server;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -475,7 +480,7 @@ ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
intptr_t m;
|
intptr_t m;
|
||||||
ngx_str_t *server;
|
ngx_str_t *server;
|
||||||
ngx_int_t total;
|
ngx_int_t total;
|
||||||
ngx_uint_t i, n;
|
ngx_uint_t i, n, best_i;
|
||||||
ngx_http_upstream_rr_peer_t *peer, *best;
|
ngx_http_upstream_rr_peer_t *peer, *best;
|
||||||
ngx_http_upstream_chash_point_t *point;
|
ngx_http_upstream_chash_point_t *point;
|
||||||
ngx_http_upstream_chash_points_t *points;
|
ngx_http_upstream_chash_points_t *points;
|
||||||
@ -501,9 +506,13 @@ ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
hp->hash, server);
|
hp->hash, server);
|
||||||
|
|
||||||
best = NULL;
|
best = NULL;
|
||||||
|
best_i = 0;
|
||||||
total = 0;
|
total = 0;
|
||||||
|
|
||||||
for (i = 0; i < hp->rrp.peers->number; i++) {
|
for (peer = hp->rrp.peers->peer, i = 0;
|
||||||
|
peer;
|
||||||
|
peer = peer->next, i++)
|
||||||
|
{
|
||||||
|
|
||||||
n = i / (8 * sizeof(uintptr_t));
|
n = i / (8 * sizeof(uintptr_t));
|
||||||
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
||||||
@ -512,8 +521,6 @@ ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
peer = &hp->rrp.peers->peer[i];
|
|
||||||
|
|
||||||
if (peer->down) {
|
if (peer->down) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -541,32 +548,13 @@ ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
|
|
||||||
if (best == NULL || peer->current_weight > best->current_weight) {
|
if (best == NULL || peer->current_weight > best->current_weight) {
|
||||||
best = peer;
|
best = peer;
|
||||||
|
best_i = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (best) {
|
if (best) {
|
||||||
best->current_weight -= total;
|
best->current_weight -= total;
|
||||||
|
goto found;
|
||||||
i = best - &hp->rrp.peers->peer[0];
|
|
||||||
|
|
||||||
hp->rrp.current = i;
|
|
||||||
|
|
||||||
n = i / (8 * sizeof(uintptr_t));
|
|
||||||
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
|
||||||
|
|
||||||
hp->rrp.tried[n] |= m;
|
|
||||||
|
|
||||||
if (now - best->checked > best->fail_timeout) {
|
|
||||||
best->checked = now;
|
|
||||||
}
|
|
||||||
|
|
||||||
pc->sockaddr = best->sockaddr;
|
|
||||||
pc->socklen = best->socklen;
|
|
||||||
pc->name = &best->name;
|
|
||||||
|
|
||||||
best->conns++;
|
|
||||||
|
|
||||||
return NGX_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hp->hash++;
|
hp->hash++;
|
||||||
@ -576,6 +564,27 @@ ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
return NGX_BUSY;
|
return NGX_BUSY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
found:
|
||||||
|
|
||||||
|
hp->rrp.current = best;
|
||||||
|
|
||||||
|
pc->sockaddr = best->sockaddr;
|
||||||
|
pc->socklen = best->socklen;
|
||||||
|
pc->name = &best->name;
|
||||||
|
|
||||||
|
best->conns++;
|
||||||
|
|
||||||
|
if (now - best->checked > best->fail_timeout) {
|
||||||
|
best->checked = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
n = best_i / (8 * sizeof(uintptr_t));
|
||||||
|
m = (uintptr_t) 1 << best_i % (8 * sizeof(uintptr_t));
|
||||||
|
|
||||||
|
hp->rrp.tried[n] |= m;
|
||||||
|
|
||||||
|
return NGX_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -181,11 +181,19 @@ ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
if (!iphp->rrp.peers->weighted) {
|
if (!iphp->rrp.peers->weighted) {
|
||||||
p = hash % iphp->rrp.peers->number;
|
p = hash % iphp->rrp.peers->number;
|
||||||
|
|
||||||
|
peer = iphp->rrp.peers->peer;
|
||||||
|
for (i = 0; i < p; i++) {
|
||||||
|
peer = peer->next;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
w = hash % iphp->rrp.peers->total_weight;
|
w = hash % iphp->rrp.peers->total_weight;
|
||||||
|
|
||||||
for (i = 0; i < iphp->rrp.peers->number; i++) {
|
for (peer = iphp->rrp.peers->peer, i = 0;
|
||||||
w -= iphp->rrp.peers->peer[i].weight;
|
peer;
|
||||||
|
peer = peer->next, i++)
|
||||||
|
{
|
||||||
|
w -= peer->weight;
|
||||||
if (w < 0) {
|
if (w < 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -204,8 +212,6 @@ ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
||||||
"get ip hash peer, hash: %ui %04XA", p, m);
|
"get ip hash peer, hash: %ui %04XA", p, m);
|
||||||
|
|
||||||
peer = &iphp->rrp.peers->peer[p];
|
|
||||||
|
|
||||||
/* ngx_lock_mutex(iphp->rrp.peers->mutex); */
|
/* ngx_lock_mutex(iphp->rrp.peers->mutex); */
|
||||||
|
|
||||||
if (peer->down) {
|
if (peer->down) {
|
||||||
@ -236,7 +242,7 @@ ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
iphp->rrp.current = p;
|
iphp->rrp.current = peer;
|
||||||
|
|
||||||
pc->sockaddr = peer->sockaddr;
|
pc->sockaddr = peer->sockaddr;
|
||||||
pc->socklen = peer->socklen;
|
pc->socklen = peer->socklen;
|
||||||
|
@ -130,7 +130,10 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
p = 0;
|
p = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (i = 0; i < peers->number; i++) {
|
for (peer = peers->peer, i = 0;
|
||||||
|
peer;
|
||||||
|
peer = peer->next, i++)
|
||||||
|
{
|
||||||
|
|
||||||
n = i / (8 * sizeof(uintptr_t));
|
n = i / (8 * sizeof(uintptr_t));
|
||||||
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
||||||
@ -139,8 +142,6 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
peer = &peers->peer[i];
|
|
||||||
|
|
||||||
if (peer->down) {
|
if (peer->down) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -181,8 +182,10 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
||||||
"get least conn peer, many");
|
"get least conn peer, many");
|
||||||
|
|
||||||
for (i = p; i < peers->number; i++) {
|
for (peer = best, i = p;
|
||||||
|
peer;
|
||||||
|
peer = peer->next, i++)
|
||||||
|
{
|
||||||
n = i / (8 * sizeof(uintptr_t));
|
n = i / (8 * sizeof(uintptr_t));
|
||||||
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
||||||
|
|
||||||
@ -190,8 +193,6 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
peer = &peers->peer[i];
|
|
||||||
|
|
||||||
if (peer->down) {
|
if (peer->down) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -233,7 +234,7 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
|
|
||||||
best->conns++;
|
best->conns++;
|
||||||
|
|
||||||
rrp->current = p;
|
rrp->current = best;
|
||||||
|
|
||||||
n = p / (8 * sizeof(uintptr_t));
|
n = p / (8 * sizeof(uintptr_t));
|
||||||
m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
|
m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
|
||||||
@ -266,8 +267,8 @@ failed:
|
|||||||
|
|
||||||
/* all peers failed, mark them as live for quick recovery */
|
/* all peers failed, mark them as live for quick recovery */
|
||||||
|
|
||||||
for (i = 0; i < peers->number; i++) {
|
for (peer = peers->peer; peer; peer = peer->next) {
|
||||||
peers->peer[i].fails = 0;
|
peer->fails = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
pc->name = peers->name;
|
pc->name = peers->name;
|
||||||
|
@ -34,7 +34,7 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
|
|||||||
ngx_url_t u;
|
ngx_url_t u;
|
||||||
ngx_uint_t i, j, n, w;
|
ngx_uint_t i, j, n, w;
|
||||||
ngx_http_upstream_server_t *server;
|
ngx_http_upstream_server_t *server;
|
||||||
ngx_http_upstream_rr_peer_t *peer;
|
ngx_http_upstream_rr_peer_t *peer, **peerp;
|
||||||
ngx_http_upstream_rr_peers_t *peers, *backup;
|
ngx_http_upstream_rr_peers_t *peers, *backup;
|
||||||
|
|
||||||
us->peer.init = ngx_http_upstream_init_round_robin_peer;
|
us->peer.init = ngx_http_upstream_init_round_robin_peer;
|
||||||
@ -61,12 +61,16 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
|
|||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t)
|
peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t));
|
||||||
+ sizeof(ngx_http_upstream_rr_peer_t) * (n - 1));
|
|
||||||
if (peers == NULL) {
|
if (peers == NULL) {
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
peer = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peer_t) * n);
|
||||||
|
if (peer == NULL) {
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
peers->single = (n == 1);
|
peers->single = (n == 1);
|
||||||
peers->number = n;
|
peers->number = n;
|
||||||
peers->weighted = (w != n);
|
peers->weighted = (w != n);
|
||||||
@ -74,7 +78,7 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
|
|||||||
peers->name = &us->host;
|
peers->name = &us->host;
|
||||||
|
|
||||||
n = 0;
|
n = 0;
|
||||||
peer = peers->peer;
|
peerp = &peers->peer;
|
||||||
|
|
||||||
for (i = 0; i < us->servers->nelts; i++) {
|
for (i = 0; i < us->servers->nelts; i++) {
|
||||||
if (server[i].backup) {
|
if (server[i].backup) {
|
||||||
@ -92,6 +96,9 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
|
|||||||
peer[n].fail_timeout = server[i].fail_timeout;
|
peer[n].fail_timeout = server[i].fail_timeout;
|
||||||
peer[n].down = server[i].down;
|
peer[n].down = server[i].down;
|
||||||
peer[n].server = server[i].name;
|
peer[n].server = server[i].name;
|
||||||
|
|
||||||
|
*peerp = &peer[n];
|
||||||
|
peerp = &peer[n].next;
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -116,12 +123,16 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
|
|||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
backup = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t)
|
backup = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t));
|
||||||
+ sizeof(ngx_http_upstream_rr_peer_t) * (n - 1));
|
|
||||||
if (backup == NULL) {
|
if (backup == NULL) {
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
peer = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peer_t) * n);
|
||||||
|
if (peer == NULL) {
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
peers->single = 0;
|
peers->single = 0;
|
||||||
backup->single = 0;
|
backup->single = 0;
|
||||||
backup->number = n;
|
backup->number = n;
|
||||||
@ -130,7 +141,7 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
|
|||||||
backup->name = &us->host;
|
backup->name = &us->host;
|
||||||
|
|
||||||
n = 0;
|
n = 0;
|
||||||
peer = backup->peer;
|
peerp = &backup->peer;
|
||||||
|
|
||||||
for (i = 0; i < us->servers->nelts; i++) {
|
for (i = 0; i < us->servers->nelts; i++) {
|
||||||
if (!server[i].backup) {
|
if (!server[i].backup) {
|
||||||
@ -148,6 +159,9 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
|
|||||||
peer[n].fail_timeout = server[i].fail_timeout;
|
peer[n].fail_timeout = server[i].fail_timeout;
|
||||||
peer[n].down = server[i].down;
|
peer[n].down = server[i].down;
|
||||||
peer[n].server = server[i].name;
|
peer[n].server = server[i].name;
|
||||||
|
|
||||||
|
*peerp = &peer[n];
|
||||||
|
peerp = &peer[n].next;
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -184,19 +198,23 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
|
|||||||
|
|
||||||
n = u.naddrs;
|
n = u.naddrs;
|
||||||
|
|
||||||
peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t)
|
peers = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peers_t));
|
||||||
+ sizeof(ngx_http_upstream_rr_peer_t) * (n - 1));
|
|
||||||
if (peers == NULL) {
|
if (peers == NULL) {
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
peer = ngx_pcalloc(cf->pool, sizeof(ngx_http_upstream_rr_peer_t) * n);
|
||||||
|
if (peer == NULL) {
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
peers->single = (n == 1);
|
peers->single = (n == 1);
|
||||||
peers->number = n;
|
peers->number = n;
|
||||||
peers->weighted = 0;
|
peers->weighted = 0;
|
||||||
peers->total_weight = n;
|
peers->total_weight = n;
|
||||||
peers->name = &us->host;
|
peers->name = &us->host;
|
||||||
|
|
||||||
peer = peers->peer;
|
peerp = &peers->peer;
|
||||||
|
|
||||||
for (i = 0; i < u.naddrs; i++) {
|
for (i = 0; i < u.naddrs; i++) {
|
||||||
peer[i].sockaddr = u.addrs[i].sockaddr;
|
peer[i].sockaddr = u.addrs[i].sockaddr;
|
||||||
@ -207,6 +225,8 @@ ngx_http_upstream_init_round_robin(ngx_conf_t *cf,
|
|||||||
peer[i].current_weight = 0;
|
peer[i].current_weight = 0;
|
||||||
peer[i].max_fails = 1;
|
peer[i].max_fails = 1;
|
||||||
peer[i].fail_timeout = 10;
|
peer[i].fail_timeout = 10;
|
||||||
|
*peerp = &peer[i];
|
||||||
|
peerp = &peer[i].next;
|
||||||
}
|
}
|
||||||
|
|
||||||
us->peer.data = peers;
|
us->peer.data = peers;
|
||||||
@ -236,7 +256,7 @@ ngx_http_upstream_init_round_robin_peer(ngx_http_request_t *r,
|
|||||||
}
|
}
|
||||||
|
|
||||||
rrp->peers = us->peer.data;
|
rrp->peers = us->peer.data;
|
||||||
rrp->current = 0;
|
rrp->current = NULL;
|
||||||
|
|
||||||
n = rrp->peers->number;
|
n = rrp->peers->number;
|
||||||
|
|
||||||
@ -280,7 +300,7 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
|
|||||||
socklen_t socklen;
|
socklen_t socklen;
|
||||||
ngx_uint_t i, n;
|
ngx_uint_t i, n;
|
||||||
struct sockaddr *sockaddr;
|
struct sockaddr *sockaddr;
|
||||||
ngx_http_upstream_rr_peer_t *peer;
|
ngx_http_upstream_rr_peer_t *peer, **peerp;
|
||||||
ngx_http_upstream_rr_peers_t *peers;
|
ngx_http_upstream_rr_peers_t *peers;
|
||||||
ngx_http_upstream_rr_peer_data_t *rrp;
|
ngx_http_upstream_rr_peer_data_t *rrp;
|
||||||
|
|
||||||
@ -295,18 +315,21 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
|
|||||||
r->upstream->peer.data = rrp;
|
r->upstream->peer.data = rrp;
|
||||||
}
|
}
|
||||||
|
|
||||||
peers = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peers_t)
|
peers = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peers_t));
|
||||||
+ sizeof(ngx_http_upstream_rr_peer_t) * (ur->naddrs - 1));
|
|
||||||
if (peers == NULL) {
|
if (peers == NULL) {
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
peer = ngx_pcalloc(r->pool, sizeof(ngx_http_upstream_rr_peer_t)
|
||||||
|
* ur->naddrs);
|
||||||
|
if (peer == NULL) {
|
||||||
|
return NGX_ERROR;
|
||||||
|
}
|
||||||
|
|
||||||
peers->single = (ur->naddrs == 1);
|
peers->single = (ur->naddrs == 1);
|
||||||
peers->number = ur->naddrs;
|
peers->number = ur->naddrs;
|
||||||
peers->name = &ur->host;
|
peers->name = &ur->host;
|
||||||
|
|
||||||
peer = peers->peer;
|
|
||||||
|
|
||||||
if (ur->sockaddr) {
|
if (ur->sockaddr) {
|
||||||
peer[0].sockaddr = ur->sockaddr;
|
peer[0].sockaddr = ur->sockaddr;
|
||||||
peer[0].socklen = ur->socklen;
|
peer[0].socklen = ur->socklen;
|
||||||
@ -316,8 +339,10 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
|
|||||||
peer[0].current_weight = 0;
|
peer[0].current_weight = 0;
|
||||||
peer[0].max_fails = 1;
|
peer[0].max_fails = 1;
|
||||||
peer[0].fail_timeout = 10;
|
peer[0].fail_timeout = 10;
|
||||||
|
peers->peer = peer;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
peerp = &peers->peer;
|
||||||
|
|
||||||
for (i = 0; i < ur->naddrs; i++) {
|
for (i = 0; i < ur->naddrs; i++) {
|
||||||
|
|
||||||
@ -356,11 +381,13 @@ ngx_http_upstream_create_round_robin_peer(ngx_http_request_t *r,
|
|||||||
peer[i].current_weight = 0;
|
peer[i].current_weight = 0;
|
||||||
peer[i].max_fails = 1;
|
peer[i].max_fails = 1;
|
||||||
peer[i].fail_timeout = 10;
|
peer[i].fail_timeout = 10;
|
||||||
|
*peerp = &peer[i];
|
||||||
|
peerp = &peer[i].next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
rrp->peers = peers;
|
rrp->peers = peers;
|
||||||
rrp->current = 0;
|
rrp->current = NULL;
|
||||||
|
|
||||||
if (rrp->peers->number <= 8 * sizeof(uintptr_t)) {
|
if (rrp->peers->number <= 8 * sizeof(uintptr_t)) {
|
||||||
rrp->tried = &rrp->data;
|
rrp->tried = &rrp->data;
|
||||||
@ -409,12 +436,14 @@ ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
/* ngx_lock_mutex(peers->mutex); */
|
/* ngx_lock_mutex(peers->mutex); */
|
||||||
|
|
||||||
if (peers->single) {
|
if (peers->single) {
|
||||||
peer = &peers->peer[0];
|
peer = peers->peer;
|
||||||
|
|
||||||
if (peer->down) {
|
if (peer->down) {
|
||||||
goto failed;
|
goto failed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rrp->current = peer;
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
/* there are several peers */
|
/* there are several peers */
|
||||||
@ -426,8 +455,8 @@ ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
||||||
"get rr peer, current: %ui %i",
|
"get rr peer, current: %p %i",
|
||||||
rrp->current, peer->current_weight);
|
peer, peer->current_weight);
|
||||||
}
|
}
|
||||||
|
|
||||||
pc->sockaddr = peer->sockaddr;
|
pc->sockaddr = peer->sockaddr;
|
||||||
@ -468,8 +497,8 @@ failed:
|
|||||||
|
|
||||||
/* all peers failed, mark them as live for quick recovery */
|
/* all peers failed, mark them as live for quick recovery */
|
||||||
|
|
||||||
for (i = 0; i < peers->number; i++) {
|
for (peer = peers->peer; peer; peer = peer->next) {
|
||||||
peers->peer[i].fails = 0;
|
peer->fails = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ngx_unlock_mutex(peers->mutex); */
|
/* ngx_unlock_mutex(peers->mutex); */
|
||||||
@ -486,7 +515,7 @@ ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp)
|
|||||||
time_t now;
|
time_t now;
|
||||||
uintptr_t m;
|
uintptr_t m;
|
||||||
ngx_int_t total;
|
ngx_int_t total;
|
||||||
ngx_uint_t i, n;
|
ngx_uint_t i, n, p;
|
||||||
ngx_http_upstream_rr_peer_t *peer, *best;
|
ngx_http_upstream_rr_peer_t *peer, *best;
|
||||||
|
|
||||||
now = ngx_time();
|
now = ngx_time();
|
||||||
@ -494,7 +523,14 @@ ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp)
|
|||||||
best = NULL;
|
best = NULL;
|
||||||
total = 0;
|
total = 0;
|
||||||
|
|
||||||
for (i = 0; i < rrp->peers->number; i++) {
|
#if (NGX_SUPPRESS_WARN)
|
||||||
|
p = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
for (peer = rrp->peers->peer, i = 0;
|
||||||
|
peer;
|
||||||
|
peer = peer->next, i++)
|
||||||
|
{
|
||||||
|
|
||||||
n = i / (8 * sizeof(uintptr_t));
|
n = i / (8 * sizeof(uintptr_t));
|
||||||
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
||||||
@ -503,8 +539,6 @@ ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
peer = &rrp->peers->peer[i];
|
|
||||||
|
|
||||||
if (peer->down) {
|
if (peer->down) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -525,6 +559,7 @@ ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp)
|
|||||||
|
|
||||||
if (best == NULL || peer->current_weight > best->current_weight) {
|
if (best == NULL || peer->current_weight > best->current_weight) {
|
||||||
best = peer;
|
best = peer;
|
||||||
|
p = i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -532,12 +567,10 @@ ngx_http_upstream_get_peer(ngx_http_upstream_rr_peer_data_t *rrp)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
i = best - &rrp->peers->peer[0];
|
rrp->current = best;
|
||||||
|
|
||||||
rrp->current = i;
|
n = p / (8 * sizeof(uintptr_t));
|
||||||
|
m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
|
||||||
n = i / (8 * sizeof(uintptr_t));
|
|
||||||
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
|
|
||||||
|
|
||||||
rrp->tried[n] |= m;
|
rrp->tried[n] |= m;
|
||||||
|
|
||||||
@ -565,7 +598,7 @@ ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data,
|
|||||||
|
|
||||||
/* TODO: NGX_PEER_KEEPALIVE */
|
/* TODO: NGX_PEER_KEEPALIVE */
|
||||||
|
|
||||||
peer = &rrp->peers->peer[rrp->current];
|
peer = rrp->current;
|
||||||
|
|
||||||
if (rrp->peers->single) {
|
if (rrp->peers->single) {
|
||||||
|
|
||||||
@ -589,8 +622,8 @@ ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data,
|
|||||||
}
|
}
|
||||||
|
|
||||||
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
||||||
"free rr peer failed: %ui %i",
|
"free rr peer failed: %p %i",
|
||||||
rrp->current, peer->effective_weight);
|
peer, peer->effective_weight);
|
||||||
|
|
||||||
if (peer->effective_weight < 0) {
|
if (peer->effective_weight < 0) {
|
||||||
peer->effective_weight = 0;
|
peer->effective_weight = 0;
|
||||||
@ -629,7 +662,7 @@ ngx_http_upstream_set_round_robin_peer_session(ngx_peer_connection_t *pc,
|
|||||||
ngx_ssl_session_t *ssl_session;
|
ngx_ssl_session_t *ssl_session;
|
||||||
ngx_http_upstream_rr_peer_t *peer;
|
ngx_http_upstream_rr_peer_t *peer;
|
||||||
|
|
||||||
peer = &rrp->peers->peer[rrp->current];
|
peer = rrp->current;
|
||||||
|
|
||||||
/* TODO: threads only mutex */
|
/* TODO: threads only mutex */
|
||||||
/* ngx_lock_mutex(rrp->peers->mutex); */
|
/* ngx_lock_mutex(rrp->peers->mutex); */
|
||||||
@ -665,7 +698,7 @@ ngx_http_upstream_save_round_robin_peer_session(ngx_peer_connection_t *pc,
|
|||||||
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
||||||
"save session: %p", ssl_session);
|
"save session: %p", ssl_session);
|
||||||
|
|
||||||
peer = &rrp->peers->peer[rrp->current];
|
peer = rrp->current;
|
||||||
|
|
||||||
/* TODO: threads only mutex */
|
/* TODO: threads only mutex */
|
||||||
/* ngx_lock_mutex(rrp->peers->mutex); */
|
/* ngx_lock_mutex(rrp->peers->mutex); */
|
||||||
|
@ -14,7 +14,9 @@
|
|||||||
#include <ngx_http.h>
|
#include <ngx_http.h>
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct ngx_http_upstream_rr_peer_s ngx_http_upstream_rr_peer_t;
|
||||||
|
|
||||||
|
struct ngx_http_upstream_rr_peer_s {
|
||||||
struct sockaddr *sockaddr;
|
struct sockaddr *sockaddr;
|
||||||
socklen_t socklen;
|
socklen_t socklen;
|
||||||
ngx_str_t name;
|
ngx_str_t name;
|
||||||
@ -38,7 +40,9 @@ typedef struct {
|
|||||||
#if (NGX_HTTP_SSL)
|
#if (NGX_HTTP_SSL)
|
||||||
ngx_ssl_session_t *ssl_session; /* local to a process */
|
ngx_ssl_session_t *ssl_session; /* local to a process */
|
||||||
#endif
|
#endif
|
||||||
} ngx_http_upstream_rr_peer_t;
|
|
||||||
|
ngx_http_upstream_rr_peer_t *next;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
typedef struct ngx_http_upstream_rr_peers_s ngx_http_upstream_rr_peers_t;
|
typedef struct ngx_http_upstream_rr_peers_s ngx_http_upstream_rr_peers_t;
|
||||||
@ -55,13 +59,13 @@ struct ngx_http_upstream_rr_peers_s {
|
|||||||
|
|
||||||
ngx_http_upstream_rr_peers_t *next;
|
ngx_http_upstream_rr_peers_t *next;
|
||||||
|
|
||||||
ngx_http_upstream_rr_peer_t peer[1];
|
ngx_http_upstream_rr_peer_t *peer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
ngx_http_upstream_rr_peers_t *peers;
|
ngx_http_upstream_rr_peers_t *peers;
|
||||||
ngx_uint_t current;
|
ngx_http_upstream_rr_peer_t *current;
|
||||||
uintptr_t *tried;
|
uintptr_t *tried;
|
||||||
uintptr_t data;
|
uintptr_t data;
|
||||||
} ngx_http_upstream_rr_peer_data_t;
|
} ngx_http_upstream_rr_peer_data_t;
|
||||||
|
Loading…
Reference in New Issue
Block a user