summaryrefslogtreecommitdiffhomepage
path: root/src/stream
diff options
context:
space:
mode:
Diffstat (limited to 'src/stream')
-rw-r--r--src/stream/ngx_stream_upstream.c20
-rw-r--r--src/stream/ngx_stream_upstream.h2
-rw-r--r--src/stream/ngx_stream_upstream_hash_module.c9
-rw-r--r--src/stream/ngx_stream_upstream_least_conn_module.c9
-rw-r--r--src/stream/ngx_stream_upstream_round_robin.c11
-rw-r--r--src/stream/ngx_stream_upstream_round_robin.h1
6 files changed, 51 insertions, 1 deletions
diff --git a/src/stream/ngx_stream_upstream.c b/src/stream/ngx_stream_upstream.c
index 2a164fc8c..c9e17845e 100644
--- a/src/stream/ngx_stream_upstream.c
+++ b/src/stream/ngx_stream_upstream.c
@@ -322,6 +322,7 @@ ngx_stream_upstream(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy)
uscf = ngx_stream_upstream_add(cf, &u, NGX_STREAM_UPSTREAM_CREATE
|NGX_STREAM_UPSTREAM_WEIGHT
+ |NGX_STREAM_UPSTREAM_MAX_CONNS
|NGX_STREAM_UPSTREAM_MAX_FAILS
|NGX_STREAM_UPSTREAM_FAIL_TIMEOUT
|NGX_STREAM_UPSTREAM_DOWN
@@ -407,7 +408,7 @@ ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
time_t fail_timeout;
ngx_str_t *value, s;
ngx_url_t u;
- ngx_int_t weight, max_fails;
+ ngx_int_t weight, max_conns, max_fails;
ngx_uint_t i;
ngx_stream_upstream_server_t *us;
@@ -421,6 +422,7 @@ ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
value = cf->args->elts;
weight = 1;
+ max_conns = 0;
max_fails = 1;
fail_timeout = 10;
@@ -441,6 +443,21 @@ ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
continue;
}
+ if (ngx_strncmp(value[i].data, "max_conns=", 10) == 0) {
+
+ if (!(uscf->flags & NGX_STREAM_UPSTREAM_MAX_CONNS)) {
+ goto not_supported;
+ }
+
+ max_conns = ngx_atoi(&value[i].data[10], value[i].len - 10);
+
+ if (max_conns == NGX_ERROR) {
+ goto invalid;
+ }
+
+ continue;
+ }
+
if (ngx_strncmp(value[i].data, "max_fails=", 10) == 0) {
if (!(uscf->flags & NGX_STREAM_UPSTREAM_MAX_FAILS)) {
@@ -522,6 +539,7 @@ ngx_stream_upstream_server(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
us->addrs = u.addrs;
us->naddrs = u.naddrs;
us->weight = weight;
+ us->max_conns = max_conns;
us->max_fails = max_fails;
us->fail_timeout = fail_timeout;
diff --git a/src/stream/ngx_stream_upstream.h b/src/stream/ngx_stream_upstream.h
index 1e9f08fc6..578ae0004 100644
--- a/src/stream/ngx_stream_upstream.h
+++ b/src/stream/ngx_stream_upstream.h
@@ -21,6 +21,7 @@
#define NGX_STREAM_UPSTREAM_FAIL_TIMEOUT 0x0008
#define NGX_STREAM_UPSTREAM_DOWN 0x0010
#define NGX_STREAM_UPSTREAM_BACKUP 0x0020
+#define NGX_STREAM_UPSTREAM_MAX_CONNS 0x0100
typedef struct {
@@ -50,6 +51,7 @@ typedef struct {
ngx_addr_t *addrs;
ngx_uint_t naddrs;
ngx_uint_t weight;
+ ngx_uint_t max_conns;
ngx_uint_t max_fails;
time_t fail_timeout;
diff --git a/src/stream/ngx_stream_upstream_hash_module.c b/src/stream/ngx_stream_upstream_hash_module.c
index 0952f330a..cb44fcdad 100644
--- a/src/stream/ngx_stream_upstream_hash_module.c
+++ b/src/stream/ngx_stream_upstream_hash_module.c
@@ -241,6 +241,10 @@ ngx_stream_upstream_get_hash_peer(ngx_peer_connection_t *pc, void *data)
goto next;
}
+ if (peer->max_conns && peer->conns >= peer->max_conns) {
+ goto next;
+ }
+
break;
next:
@@ -549,6 +553,10 @@ ngx_stream_upstream_get_chash_peer(ngx_peer_connection_t *pc, void *data)
continue;
}
+ if (peer->max_conns && peer->conns >= peer->max_conns) {
+ continue;
+ }
+
peer->current_weight += peer->effective_weight;
total += peer->effective_weight;
@@ -646,6 +654,7 @@ ngx_stream_upstream_hash(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
uscf->flags = NGX_STREAM_UPSTREAM_CREATE
|NGX_STREAM_UPSTREAM_WEIGHT
+ |NGX_STREAM_UPSTREAM_MAX_CONNS
|NGX_STREAM_UPSTREAM_MAX_FAILS
|NGX_STREAM_UPSTREAM_FAIL_TIMEOUT
|NGX_STREAM_UPSTREAM_DOWN;
diff --git a/src/stream/ngx_stream_upstream_least_conn_module.c b/src/stream/ngx_stream_upstream_least_conn_module.c
index 1213bb53b..739b20a9f 100644
--- a/src/stream/ngx_stream_upstream_least_conn_module.c
+++ b/src/stream/ngx_stream_upstream_least_conn_module.c
@@ -150,6 +150,10 @@ ngx_stream_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
continue;
}
+ if (peer->max_conns && peer->conns >= peer->max_conns) {
+ continue;
+ }
+
/*
* select peer with least number of connections; if there are
* multiple peers with the same number of connections, select
@@ -205,6 +209,10 @@ ngx_stream_upstream_get_least_conn_peer(ngx_peer_connection_t *pc, void *data)
continue;
}
+ if (peer->max_conns && peer->conns >= peer->max_conns) {
+ continue;
+ }
+
peer->current_weight += peer->effective_weight;
total += peer->effective_weight;
@@ -292,6 +300,7 @@ ngx_stream_upstream_least_conn(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
uscf->flags = NGX_STREAM_UPSTREAM_CREATE
|NGX_STREAM_UPSTREAM_WEIGHT
+ |NGX_STREAM_UPSTREAM_MAX_CONNS
|NGX_STREAM_UPSTREAM_MAX_FAILS
|NGX_STREAM_UPSTREAM_FAIL_TIMEOUT
|NGX_STREAM_UPSTREAM_DOWN
diff --git a/src/stream/ngx_stream_upstream_round_robin.c b/src/stream/ngx_stream_upstream_round_robin.c
index 768aaff4e..3f4bfcfa5 100644
--- a/src/stream/ngx_stream_upstream_round_robin.c
+++ b/src/stream/ngx_stream_upstream_round_robin.c
@@ -96,6 +96,7 @@ ngx_stream_upstream_init_round_robin(ngx_conf_t *cf,
peer[n].weight = server[i].weight;
peer[n].effective_weight = server[i].weight;
peer[n].current_weight = 0;
+ peer[n].max_conns = server[i].max_conns;
peer[n].max_fails = server[i].max_fails;
peer[n].fail_timeout = server[i].fail_timeout;
peer[n].down = server[i].down;
@@ -159,6 +160,7 @@ ngx_stream_upstream_init_round_robin(ngx_conf_t *cf,
peer[n].weight = server[i].weight;
peer[n].effective_weight = server[i].weight;
peer[n].current_weight = 0;
+ peer[n].max_conns = server[i].max_conns;
peer[n].max_fails = server[i].max_fails;
peer[n].fail_timeout = server[i].fail_timeout;
peer[n].down = server[i].down;
@@ -227,6 +229,7 @@ ngx_stream_upstream_init_round_robin(ngx_conf_t *cf,
peer[i].weight = 1;
peer[i].effective_weight = 1;
peer[i].current_weight = 0;
+ peer[i].max_conns = 0;
peer[i].max_fails = 1;
peer[i].fail_timeout = 10;
*peerp = &peer[i];
@@ -438,6 +441,10 @@ ngx_stream_upstream_get_round_robin_peer(ngx_peer_connection_t *pc, void *data)
goto failed;
}
+ if (peer->max_conns && peer->conns >= peer->max_conns) {
+ goto failed;
+ }
+
rrp->current = peer;
} else {
@@ -539,6 +546,10 @@ ngx_stream_upstream_get_peer(ngx_stream_upstream_rr_peer_data_t *rrp)
continue;
}
+ if (peer->max_conns && peer->conns >= peer->max_conns) {
+ continue;
+ }
+
peer->current_weight += peer->effective_weight;
total += peer->effective_weight;
diff --git a/src/stream/ngx_stream_upstream_round_robin.h b/src/stream/ngx_stream_upstream_round_robin.h
index bdec0b826..3a1bf36d6 100644
--- a/src/stream/ngx_stream_upstream_round_robin.h
+++ b/src/stream/ngx_stream_upstream_round_robin.h
@@ -27,6 +27,7 @@ struct ngx_stream_upstream_rr_peer_s {
ngx_int_t weight;
ngx_uint_t conns;
+ ngx_uint_t max_conns;
ngx_uint_t fails;
time_t accessed;