summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2020-03-30 19:47:01 +0300
committerIgor Sysoev <igor@sysoev.ru>2020-03-30 19:47:01 +0300
commit01e957ef64b63403ac2e9107e2a84578d68a09b3 (patch)
tree7f7f1063c44e6903f88b5f3861f171b0d065ad8a
parent68c6b67ffc840c78eddd27a65e9bf1370aaf5631 (diff)
downloadunit-01e957ef64b63403ac2e9107e2a84578d68a09b3.tar.gz
unit-01e957ef64b63403ac2e9107e2a84578d68a09b3.tar.bz2
Rational number support in upstream server weight.
Diffstat (limited to '')
-rw-r--r--src/nxt_conf_validation.c14
-rw-r--r--src/nxt_upstream_round_robin.c48
2 files changed, 38 insertions, 24 deletions
diff --git a/src/nxt_conf_validation.c b/src/nxt_conf_validation.c
index aa48845a..bc03bdfb 100644
--- a/src/nxt_conf_validation.c
+++ b/src/nxt_conf_validation.c
@@ -732,7 +732,7 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_upstream_members[] = {
static nxt_conf_vldt_object_t nxt_conf_vldt_upstream_server_members[] = {
{ nxt_string("weight"),
- NXT_CONF_VLDT_INTEGER,
+ NXT_CONF_VLDT_NUMBER,
&nxt_conf_vldt_server_weight,
NULL },
@@ -2060,18 +2060,18 @@ static nxt_int_t
nxt_conf_vldt_server_weight(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data)
{
- int64_t int_value;
+ double num_value;
- int_value = nxt_conf_get_number(value);
+ num_value = nxt_conf_get_number(value);
- if (int_value <= 0) {
+ if (num_value < 0) {
return nxt_conf_vldt_error(vldt, "The \"weight\" number must be "
- "greater than 0.");
+ "positive.");
}
- if (int_value > NXT_INT32_T_MAX) {
+ if (num_value > 1000000) {
return nxt_conf_vldt_error(vldt, "The \"weight\" number must "
- "not exceed %d.", NXT_INT32_T_MAX);
+ "not exceed 1,000,000");
}
return NXT_OK;
diff --git a/src/nxt_upstream_round_robin.c b/src/nxt_upstream_round_robin.c
index fd76ecb5..31e2f48a 100644
--- a/src/nxt_upstream_round_robin.c
+++ b/src/nxt_upstream_round_robin.c
@@ -4,6 +4,7 @@
* Copyright (C) NGINX, Inc.
*/
+#include <math.h>
#include <nxt_router.h>
#include <nxt_http.h>
#include <nxt_upstream.h>
@@ -38,34 +39,47 @@ static const nxt_upstream_server_proto_t nxt_upstream_round_robin_proto = {
};
-static nxt_conf_map_t nxt_upstream_round_robin_server_conf[] = {
- {
- nxt_string("weight"),
- NXT_CONF_MAP_INT32,
- offsetof(nxt_upstream_round_robin_server_t, weight),
- },
-};
-
-
nxt_int_t
nxt_upstream_round_robin_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
nxt_conf_value_t *upstream_conf, nxt_upstream_t *upstream)
{
+ double total, k, w;
size_t size;
- uint32_t i, n, next;
+ uint32_t i, n, next, wt;
nxt_mp_t *mp;
nxt_str_t name;
nxt_sockaddr_t *sa;
- nxt_conf_value_t *servers_conf, *srvcf;
+ nxt_conf_value_t *servers_conf, *srvcf, *wtcf;
nxt_upstream_round_robin_t *urr;
static nxt_str_t servers = nxt_string("servers");
+ static nxt_str_t weight = nxt_string("weight");
mp = tmcf->router_conf->mem_pool;
servers_conf = nxt_conf_get_object_member(upstream_conf, &servers, NULL);
n = nxt_conf_object_members_count(servers_conf);
+ total = 0.0;
+ next = 0;
+
+ for (i = 0; i < n; i++) {
+ srvcf = nxt_conf_next_object_member(servers_conf, &name, &next);
+ wtcf = nxt_conf_get_object_member(srvcf, &weight, NULL);
+ w = (wtcf != NULL) ? nxt_conf_get_number(wtcf) : 1;
+ total += w;
+ }
+
+ /*
+ * This prevents overflow of int32_t
+ * in nxt_upstream_round_robin_server_get().
+ */
+ k = (total == 0) ? 0 : (NXT_INT32_T_MAX / 2) / total;
+
+ if (isinf(k)) {
+ k = 1;
+ }
+
size = sizeof(nxt_upstream_round_robin_t)
+ n * sizeof(nxt_upstream_round_robin_server_t);
@@ -88,14 +102,14 @@ nxt_upstream_round_robin_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
sa->type = SOCK_STREAM;
urr->server[i].sockaddr = sa;
- urr->server[i].weight = 1;
urr->server[i].protocol = NXT_HTTP_PROTO_H1;
- nxt_conf_map_object(mp, srvcf, nxt_upstream_round_robin_server_conf,
- nxt_nitems(nxt_upstream_round_robin_server_conf),
- &urr->server[i]);
+ wtcf = nxt_conf_get_object_member(srvcf, &weight, NULL);
+ w = (wtcf != NULL) ? k * nxt_conf_get_number(wtcf) : k;
+ wt = (w > 1 || w == 0) ? round(w) : 1;
- urr->server[i].effective_weight = urr->server[i].weight;
+ urr->server[i].weight = wt;
+ urr->server[i].effective_weight = wt;
}
upstream->proto = &nxt_upstream_round_robin_proto;
@@ -177,7 +191,7 @@ nxt_upstream_round_robin_server_get(nxt_task_t *task, nxt_upstream_server_t *us)
}
}
- if (best == NULL) {
+ if (best == NULL || total == 0) {
us->state->error(task, us);
return;
}