summaryrefslogtreecommitdiffhomepage
path: root/src/http/modules/ngx_http_upstream_least_conn_module.c
diff options
context:
space:
mode:
authorRuslan Ermilov <ru@nginx.com>2015-04-10 13:16:23 +0300
committerRuslan Ermilov <ru@nginx.com>2015-04-10 13:16:23 +0300
commit18fa775b1060a663e15473161c795992926e60e1 (patch)
tree573c21f6804ea6b8098dfc37c4d3fe8da38ff5b5 /src/http/modules/ngx_http_upstream_least_conn_module.c
parentb25f0ddcf1202a475dee0d0bb6b93c1272c99d17 (diff)
downloadnginx-18fa775b1060a663e15473161c795992926e60e1.tar.gz
nginx-18fa775b1060a663e15473161c795992926e60e1.tar.bz2
Upstream: track the number of active connections to upstreams.
This also simplifies the implementation of the least_conn module.
Diffstat (limited to 'src/http/modules/ngx_http_upstream_least_conn_module.c')
-rw-r--r--src/http/modules/ngx_http_upstream_least_conn_module.c138
1 files changed, 18 insertions, 120 deletions
diff --git a/src/http/modules/ngx_http_upstream_least_conn_module.c b/src/http/modules/ngx_http_upstream_least_conn_module.c
index 623bc9b10..f2a4ec9eb 100644
--- a/src/http/modules/ngx_http_upstream_least_conn_module.c
+++ b/src/http/modules/ngx_http_upstream_least_conn_module.c
@@ -10,29 +10,10 @@
#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,
ngx_http_upstream_srv_conf_t *us);
static ngx_int_t ngx_http_upstream_get_least_conn_peer(
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,
void *conf);
@@ -57,7 +38,7 @@ static ngx_http_module_t ngx_http_upstream_least_conn_module_ctx = {
NULL, /* create 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, /* create location configuration */
@@ -85,10 +66,6 @@ static ngx_int_t
ngx_http_upstream_init_least_conn(ngx_conf_t *cf,
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,
"init least conn");
@@ -96,22 +73,6 @@ ngx_http_upstream_init_least_conn(ngx_conf_t *cf,
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;
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_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,
"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) {
return NGX_ERROR;
}
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;
}
@@ -157,7 +99,7 @@ ngx_http_upstream_init_least_conn_peer(ngx_http_request_t *r,
static ngx_int_t
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;
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,
"get least conn peer, try: %ui", pc->tries);
- if (lcp->rrp.peers->single) {
- return lcp->get_rr_peer(pc, &lcp->rrp);
+ if (rrp->peers->single) {
+ return ngx_http_upstream_get_round_robin_peer(pc, rrp);
}
pc->cached = 0;
@@ -178,7 +120,7 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
now = ngx_time();
- peers = lcp->rrp.peers;
+ peers = rrp->peers;
best = NULL;
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));
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
- if (lcp->rrp.tried[n] & m) {
+ if (rrp->tried[n] & m) {
continue;
}
@@ -217,15 +159,13 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
*/
if (best == NULL
- || lcp->conns[i] * best->weight < lcp->conns[p] * peer->weight)
+ || peer->conns * best->weight < best->conns * peer->weight)
{
best = peer;
many = 0;
p = i;
- } else if (lcp->conns[i] * best->weight
- == lcp->conns[p] * peer->weight)
- {
+ } else if (peer->conns * best->weight == best->conns * peer->weight) {
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));
m = (uintptr_t) 1 << i % (8 * sizeof(uintptr_t));
- if (lcp->rrp.tried[n] & m) {
+ if (rrp->tried[n] & m) {
continue;
}
@@ -256,7 +196,7 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
continue;
}
- if (lcp->conns[i] * best->weight != lcp->conns[p] * peer->weight) {
+ if (peer->conns * best->weight != best->conns * peer->weight) {
continue;
}
@@ -291,13 +231,14 @@ ngx_http_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
pc->socklen = best->socklen;
pc->name = &best->name;
- lcp->rrp.current = p;
+ best->conns++;
+
+ rrp->current = p;
n = p / (8 * sizeof(uintptr_t));
m = (uintptr_t) 1 << p % (8 * sizeof(uintptr_t));
- lcp->rrp.tried[n] |= m;
- lcp->conns[p]++;
+ rrp->tried[n] |= m;
return NGX_OK;
@@ -307,18 +248,16 @@ failed:
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, pc->log, 0,
"get least conn peer, backup servers");
- lcp->conns += peers->number;
-
- lcp->rrp.peers = peers->next;
+ rrp->peers = peers->next;
- n = (lcp->rrp.peers->number + (8 * sizeof(uintptr_t) - 1))
+ n = (rrp->peers->number + (8 * sizeof(uintptr_t) - 1))
/ (8 * sizeof(uintptr_t));
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) {
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 *
ngx_http_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{