mirror of
https://github.com/nginx/nginx.git
synced 2025-02-25 18:55:26 -06:00
Upstream: track the number of active connections to upstreams.
This also simplifies the implementation of the least_conn module.
This commit is contained in:
parent
b25f0ddcf1
commit
18fa775b10
@ -262,6 +262,8 @@ ngx_http_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
pc->socklen = peer->socklen;
|
pc->socklen = peer->socklen;
|
||||||
pc->name = &peer->name;
|
pc->name = &peer->name;
|
||||||
|
|
||||||
|
peer->conns++;
|
||||||
|
|
||||||
if (now - peer->checked > peer->fail_timeout) {
|
if (now - peer->checked > peer->fail_timeout) {
|
||||||
peer->checked = now;
|
peer->checked = now;
|
||||||
}
|
}
|
||||||
@ -562,6 +564,8 @@ ngx_http_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
pc->socklen = best->socklen;
|
pc->socklen = best->socklen;
|
||||||
pc->name = &best->name;
|
pc->name = &best->name;
|
||||||
|
|
||||||
|
best->conns++;
|
||||||
|
|
||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -242,6 +242,8 @@ ngx_http_upstream_get_ip_hash_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
pc->socklen = peer->socklen;
|
pc->socklen = peer->socklen;
|
||||||
pc->name = &peer->name;
|
pc->name = &peer->name;
|
||||||
|
|
||||||
|
peer->conns++;
|
||||||
|
|
||||||
if (now - peer->checked > peer->fail_timeout) {
|
if (now - peer->checked > peer->fail_timeout) {
|
||||||
peer->checked = now;
|
peer->checked = now;
|
||||||
}
|
}
|
||||||
|
@ -10,29 +10,10 @@
|
|||||||
#include <ngx_http.h>
|
#include <ngx_http.h>
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
ngx_uint_t *conns;
|
|
||||||
} ngx_http_upstream_least_conn_conf_t;
|
|
||||||
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
/* the round robin data must be first */
|
|
||||||
ngx_http_upstream_rr_peer_data_t rrp;
|
|
||||||
|
|
||||||
ngx_uint_t *conns;
|
|
||||||
|
|
||||||
ngx_event_get_peer_pt get_rr_peer;
|
|
||||||
ngx_event_free_peer_pt free_rr_peer;
|
|
||||||
} ngx_http_upstream_lc_peer_data_t;
|
|
||||||
|
|
||||||
|
|
||||||
static ngx_int_t ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r,
|
static ngx_int_t ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r,
|
||||||
ngx_http_upstream_srv_conf_t *us);
|
ngx_http_upstream_srv_conf_t *us);
|
||||||
static ngx_int_t ngx_http_upstream_get_least_conn_peer(
|
static ngx_int_t ngx_http_upstream_get_least_conn_peer(
|
||||||
ngx_peer_connection_t *pc, void *data);
|
ngx_peer_connection_t *pc, void *data);
|
||||||
static void ngx_http_upstream_free_least_conn_peer(ngx_peer_connection_t *pc,
|
|
||||||
void *data, ngx_uint_t state);
|
|
||||||
static void *ngx_http_upstream_least_conn_create_conf(ngx_conf_t *cf);
|
|
||||||
static char *ngx_http_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd,
|
static char *ngx_http_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd,
|
||||||
void *conf);
|
void *conf);
|
||||||
|
|
||||||
@ -57,7 +38,7 @@ static ngx_http_module_t ngx_http_upstream_least_conn_module_ctx = {
|
|||||||
NULL, /* create main configuration */
|
NULL, /* create main configuration */
|
||||||
NULL, /* init main configuration */
|
NULL, /* init main configuration */
|
||||||
|
|
||||||
ngx_http_upstream_least_conn_create_conf, /* create server configuration */
|
NULL, /* create server configuration */
|
||||||
NULL, /* merge server configuration */
|
NULL, /* merge server configuration */
|
||||||
|
|
||||||
NULL, /* create location configuration */
|
NULL, /* create location configuration */
|
||||||
@ -85,10 +66,6 @@ static ngx_int_t
|
|||||||
ngx_http_upstream_init_least_conn(ngx_conf_t *cf,
|
ngx_http_upstream_init_least_conn(ngx_conf_t *cf,
|
||||||
ngx_http_upstream_srv_conf_t *us)
|
ngx_http_upstream_srv_conf_t *us)
|
||||||
{
|
{
|
||||||
ngx_uint_t n;
|
|
||||||
ngx_http_upstream_rr_peers_t *peers;
|
|
||||||
ngx_http_upstream_least_conn_conf_t *lcf;
|
|
||||||
|
|
||||||
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0,
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, cf->log, 0,
|
||||||
"init least conn");
|
"init least conn");
|
||||||
|
|
||||||
@ -96,22 +73,6 @@ ngx_http_upstream_init_least_conn(ngx_conf_t *cf,
|
|||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
peers = us->peer.data;
|
|
||||||
|
|
||||||
n = peers->number;
|
|
||||||
|
|
||||||
if (peers->next) {
|
|
||||||
n += peers->next->number;
|
|
||||||
}
|
|
||||||
|
|
||||||
lcf = ngx_http_conf_upstream_srv_conf(us,
|
|
||||||
ngx_http_upstream_least_conn_module);
|
|
||||||
|
|
||||||
lcf->conns = ngx_pcalloc(cf->pool, sizeof(ngx_uint_t) * n);
|
|
||||||
if (lcf->conns == NULL) {
|
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
us->peer.init = ngx_http_upstream_init_least_conn_peer;
|
us->peer.init = ngx_http_upstream_init_least_conn_peer;
|
||||||
|
|
||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
@ -122,33 +83,14 @@ static ngx_int_t
|
|||||||
ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r,
|
ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r,
|
||||||
ngx_http_upstream_srv_conf_t *us)
|
ngx_http_upstream_srv_conf_t *us)
|
||||||
{
|
{
|
||||||
ngx_http_upstream_lc_peer_data_t *lcp;
|
|
||||||
ngx_http_upstream_least_conn_conf_t *lcf;
|
|
||||||
|
|
||||||
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
|
||||||
"init least conn peer");
|
"init least conn peer");
|
||||||
|
|
||||||
lcf = ngx_http_conf_upstream_srv_conf(us,
|
|
||||||
ngx_http_upstream_least_conn_module);
|
|
||||||
|
|
||||||
lcp = ngx_palloc(r->pool, sizeof(ngx_http_upstream_lc_peer_data_t));
|
|
||||||
if (lcp == NULL) {
|
|
||||||
return NGX_ERROR;
|
|
||||||
}
|
|
||||||
|
|
||||||
lcp->conns = lcf->conns;
|
|
||||||
|
|
||||||
r->upstream->peer.data = &lcp->rrp;
|
|
||||||
|
|
||||||
if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) {
|
if (ngx_http_upstream_init_round_robin_peer(r, us) != NGX_OK) {
|
||||||
return NGX_ERROR;
|
return NGX_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
r->upstream->peer.get = ngx_http_upstream_get_least_conn_peer;
|
r->upstream->peer.get = ngx_http_upstream_get_least_conn_peer;
|
||||||
r->upstream->peer.free = ngx_http_upstream_free_least_conn_peer;
|
|
||||||
|
|
||||||
lcp->get_rr_peer = ngx_http_upstream_get_round_robin_peer;
|
|
||||||
lcp->free_rr_peer = ngx_http_upstream_free_round_robin_peer;
|
|
||||||
|
|
||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
}
|
}
|
||||||
@ -157,7 +99,7 @@ ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r,
|
|||||||
static ngx_int_t
|
static ngx_int_t
|
||||||
ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
||||||
{
|
{
|
||||||
ngx_http_upstream_lc_peer_data_t *lcp = data;
|
ngx_http_upstream_rr_peer_data_t *rrp = data;
|
||||||
|
|
||||||
time_t now;
|
time_t now;
|
||||||
uintptr_t m;
|
uintptr_t m;
|
||||||
@ -169,8 +111,8 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
||||||
"get least conn peer, try: %ui", pc->tries);
|
"get least conn peer, try: %ui", pc->tries);
|
||||||
|
|
||||||
if (lcp->rrp.peers->single) {
|
if (rrp->peers->single) {
|
||||||
return lcp->get_rr_peer(pc, &lcp->rrp);
|
return ngx_http_upstream_get_round_robin_peer(pc, rrp);
|
||||||
}
|
}
|
||||||
|
|
||||||
pc->cached = 0;
|
pc->cached = 0;
|
||||||
@ -178,7 +120,7 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
|
|
||||||
now = ngx_time();
|
now = ngx_time();
|
||||||
|
|
||||||
peers = lcp->rrp.peers;
|
peers = rrp->peers;
|
||||||
|
|
||||||
best = NULL;
|
best = NULL;
|
||||||
total = 0;
|
total = 0;
|
||||||
@ -193,7 +135,7 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
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));
|
||||||
|
|
||||||
if (lcp->rrp.tried[n] & m) {
|
if (rrp->tried[n] & m) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -217,15 +159,13 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
if (best == NULL
|
if (best == NULL
|
||||||
|| lcp->conns[i] * best->weight < lcp->conns[p] * peer->weight)
|
|| peer->conns * best->weight < best->conns * peer->weight)
|
||||||
{
|
{
|
||||||
best = peer;
|
best = peer;
|
||||||
many = 0;
|
many = 0;
|
||||||
p = i;
|
p = i;
|
||||||
|
|
||||||
} else if (lcp->conns[i] * best->weight
|
} else if (peer->conns * best->weight == best->conns * peer->weight) {
|
||||||
== lcp->conns[p] * peer->weight)
|
|
||||||
{
|
|
||||||
many = 1;
|
many = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -246,7 +186,7 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
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));
|
||||||
|
|
||||||
if (lcp->rrp.tried[n] & m) {
|
if (rrp->tried[n] & m) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -256,7 +196,7 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lcp->conns[i] * best->weight != lcp->conns[p] * peer->weight) {
|
if (peer->conns * best->weight != best->conns * peer->weight) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -291,13 +231,14 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
pc->socklen = best->socklen;
|
pc->socklen = best->socklen;
|
||||||
pc->name = &best->name;
|
pc->name = &best->name;
|
||||||
|
|
||||||
lcp->rrp.current = p;
|
best->conns++;
|
||||||
|
|
||||||
|
rrp->current = p;
|
||||||
|
|
||||||
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));
|
||||||
|
|
||||||
lcp->rrp.tried[n] |= m;
|
rrp->tried[n] |= m;
|
||||||
lcp->conns[p]++;
|
|
||||||
|
|
||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
|
|
||||||
@ -307,18 +248,16 @@ failed:
|
|||||||
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
||||||
"get least conn peer, backup servers");
|
"get least conn peer, backup servers");
|
||||||
|
|
||||||
lcp->conns += peers->number;
|
rrp->peers = peers->next;
|
||||||
|
|
||||||
lcp->rrp.peers = peers->next;
|
n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1))
|
||||||
|
|
||||||
n = (lcp->rrp.peers->number + (8 * sizeof(uintptr_t) - 1))
|
|
||||||
/ (8 * sizeof(uintptr_t));
|
/ (8 * sizeof(uintptr_t));
|
||||||
|
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
lcp->rrp.tried[i] = 0;
|
rrp->tried[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
rc = ngx_http_upstream_get_least_conn_peer(pc, lcp);
|
rc = ngx_http_upstream_get_least_conn_peer(pc, rrp);
|
||||||
|
|
||||||
if (rc != NGX_BUSY) {
|
if (rc != NGX_BUSY) {
|
||||||
return rc;
|
return rc;
|
||||||
@ -337,47 +276,6 @@ failed:
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
ngx_http_upstream_free_least_conn_peer(ngx_peer_connection_t *pc,
|
|
||||||
void *data, ngx_uint_t state)
|
|
||||||
{
|
|
||||||
ngx_http_upstream_lc_peer_data_t *lcp = data;
|
|
||||||
|
|
||||||
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, pc->log, 0,
|
|
||||||
"free least conn peer %ui %ui", pc->tries, state);
|
|
||||||
|
|
||||||
if (lcp->rrp.peers->single) {
|
|
||||||
lcp->free_rr_peer(pc, &lcp->rrp, state);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
lcp->conns[lcp->rrp.current]--;
|
|
||||||
|
|
||||||
lcp->free_rr_peer(pc, &lcp->rrp, state);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void *
|
|
||||||
ngx_http_upstream_least_conn_create_conf(ngx_conf_t *cf)
|
|
||||||
{
|
|
||||||
ngx_http_upstream_least_conn_conf_t *conf;
|
|
||||||
|
|
||||||
conf = ngx_pcalloc(cf->pool,
|
|
||||||
sizeof(ngx_http_upstream_least_conn_conf_t));
|
|
||||||
if (conf == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* set by ngx_pcalloc():
|
|
||||||
*
|
|
||||||
* conf->conns = NULL;
|
|
||||||
*/
|
|
||||||
|
|
||||||
return conf;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
ngx_http_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
ngx_http_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
|
||||||
{
|
{
|
||||||
|
@ -434,6 +434,8 @@ ngx_http_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data)
|
|||||||
pc->socklen = peer->socklen;
|
pc->socklen = peer->socklen;
|
||||||
pc->name = &peer->name;
|
pc->name = &peer->name;
|
||||||
|
|
||||||
|
peer->conns++;
|
||||||
|
|
||||||
/* ngx_unlock_mutex(peers->mutex); */
|
/* ngx_unlock_mutex(peers->mutex); */
|
||||||
|
|
||||||
return NGX_OK;
|
return NGX_OK;
|
||||||
@ -563,13 +565,16 @@ 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];
|
||||||
|
|
||||||
if (rrp->peers->single) {
|
if (rrp->peers->single) {
|
||||||
|
|
||||||
|
peer->conns--;
|
||||||
|
|
||||||
pc->tries = 0;
|
pc->tries = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
peer = &rrp->peers->peer[rrp->current];
|
|
||||||
|
|
||||||
if (state & NGX_PEER_FAILED) {
|
if (state & NGX_PEER_FAILED) {
|
||||||
now = ngx_time();
|
now = ngx_time();
|
||||||
|
|
||||||
@ -602,6 +607,8 @@ ngx_http_upstream_free_round_robin_peer(ngx_peer_connection_t *pc, void *data,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
peer->conns--;
|
||||||
|
|
||||||
if (pc->tries) {
|
if (pc->tries) {
|
||||||
pc->tries--;
|
pc->tries--;
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,8 @@ typedef struct {
|
|||||||
ngx_int_t effective_weight;
|
ngx_int_t effective_weight;
|
||||||
ngx_int_t weight;
|
ngx_int_t weight;
|
||||||
|
|
||||||
|
ngx_uint_t conns;
|
||||||
|
|
||||||
ngx_uint_t fails;
|
ngx_uint_t fails;
|
||||||
time_t accessed;
|
time_t accessed;
|
||||||
time_t checked;
|
time_t checked;
|
||||||
|
Loading…
Reference in New Issue
Block a user