Hello all,

To avoid repeating the same source code, allocating memory and
initializing the per_thr field from the server structure is
transferred to a separate function.

It seems to me that this code is only valid for the current version of haproxy, ie v2.4.

Best regards,

--
Zaga    <miros...@zagorac.name>

What can change the nature of a man?
>From 81ec1afe2a7da947952deedb394970248df6cd8b Mon Sep 17 00:00:00 2001
From: Miroslav Zagorac <mzago...@haproxy.com>
Date: Tue, 15 Jun 2021 15:33:20 +0200
Subject: [PATCH] CLEANUP: server: a separate function for initializing the
 per_thr field

To avoid repeating the same source code, allocating memory and initializing
the per_thr field from the server structure is transferred to a separate
function.
---
 include/haproxy/server.h |  1 +
 src/cfgparse.c           | 10 +---------
 src/server.c             | 32 ++++++++++++++++++++++----------
 src/sink.c               | 11 +----------
 4 files changed, 25 insertions(+), 29 deletions(-)

diff --git a/include/haproxy/server.h b/include/haproxy/server.h
index 74ac74053..b807a8a04 100644
--- a/include/haproxy/server.h
+++ b/include/haproxy/server.h
@@ -60,6 +60,7 @@ int srv_init_addr(void);
 struct server *cli_find_server(struct appctx *appctx, char *arg);
 struct server *new_server(struct proxy *proxy);
 void free_server(struct server *srv);
+int srv_init_per_thr(struct server *srv);
 
 /* functions related to server name resolution */
 int srv_prepare_for_resolution(struct server *srv, const char *hostname);
diff --git a/src/cfgparse.c b/src/cfgparse.c
index 906ab076f..800c61d24 100644
--- a/src/cfgparse.c
+++ b/src/cfgparse.c
@@ -3930,21 +3930,13 @@ int check_config_validity()
 
 	list_for_each_entry(newsrv, &servers_list, global_list) {
 		/* initialize idle conns lists */
-		newsrv->per_thr = calloc(global.nbthread, sizeof(*newsrv->per_thr));
-		if (!newsrv->per_thr) {
+		if (srv_init_per_thr(newsrv) == -1) {
 			ha_alert("parsing [%s:%d] : failed to allocate per-thread lists for server '%s'.\n",
 			         newsrv->conf.file, newsrv->conf.line, newsrv->id);
 			cfgerr++;
 			continue;
 		}
 
-		for (i = 0; i < global.nbthread; i++) {
-			newsrv->per_thr[i].idle_conns = EB_ROOT;
-			newsrv->per_thr[i].safe_conns = EB_ROOT;
-			newsrv->per_thr[i].avail_conns = EB_ROOT;
-			MT_LIST_INIT(&newsrv->per_thr[i].streams);
-		}
-
 		if (newsrv->max_idle_conns != 0) {
 			newsrv->curr_idle_thr = calloc(global.nbthread, sizeof(*newsrv->curr_idle_thr));
 			if (!newsrv->curr_idle_thr) {
diff --git a/src/server.c b/src/server.c
index 57b3feecb..2c55e320e 100644
--- a/src/server.c
+++ b/src/server.c
@@ -4308,6 +4308,27 @@ static int srv_alloc_lb(struct server *sv, struct proxy *be)
 	return 1;
 }
 
+/* Memory allocation and initialization of the per_thr field.
+ * Returns 0 if the field has been successfully initialized, -1 on failure.
+ */
+int srv_init_per_thr(struct server *srv)
+{
+	int i;
+
+	srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
+	if (!srv->per_thr)
+		return -1;
+
+	for (i = 0; i < global.nbthread; i++) {
+		srv->per_thr[i].idle_conns = EB_ROOT;
+		srv->per_thr[i].safe_conns = EB_ROOT;
+		srv->per_thr[i].avail_conns = EB_ROOT;
+		MT_LIST_INIT(&srv->per_thr[i].streams);
+	}
+
+	return 0;
+}
+
 /* Parse a "add server" command
  * Returns 0 if the server has been successfully initialized, 1 on failure.
  */
@@ -4317,7 +4338,6 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct
 	struct server *srv;
 	char *be_name, *sv_name;
 	int errcode, argc;
-	int i;
 	const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
 
 	usermsgs_clr("CLI");
@@ -4379,19 +4399,11 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct
 		}
 	}
 
-	srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
-	if (!srv->per_thr) {
+	if (srv_init_per_thr(srv) == -1) {
 		ha_alert("failed to allocate per-thread lists for server.\n");
 		goto out;
 	}
 
-	for (i = 0; i < global.nbthread; i++) {
-		srv->per_thr[i].idle_conns = EB_ROOT;
-		srv->per_thr[i].safe_conns = EB_ROOT;
-		srv->per_thr[i].avail_conns = EB_ROOT;
-		MT_LIST_INIT(&srv->per_thr[i].streams);
-	}
-
 	if (srv->max_idle_conns != 0) {
 		srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
 		if (!srv->curr_idle_thr) {
diff --git a/src/sink.c b/src/sink.c
index 6358da53a..119c4e045 100644
--- a/src/sink.c
+++ b/src/sink.c
@@ -940,7 +940,6 @@ struct sink *sink_new_from_logsrv(struct logsrv *logsrv)
 	struct sink *sink = NULL;
 	struct server *srv = NULL;
 	struct sink_forward_target *sft = NULL;
-	int i;
 
 	/* allocate new proxy to handle
 	 * forward to a stream server
@@ -972,17 +971,9 @@ struct sink *sink_new_from_logsrv(struct logsrv *logsrv)
 	HA_SPIN_INIT(&srv->lock);
 
 	/* process per thread init */
-	srv->per_thr = calloc(global.nbthread, sizeof(*srv->per_thr));
-	if (!srv->per_thr)
+	if (srv_init_per_thr(srv) == -1)
 		goto error;
 
-	for (i = 0; i < global.nbthread; i++) {
-		srv->per_thr[i].idle_conns = EB_ROOT;
-		srv->per_thr[i].safe_conns = EB_ROOT;
-		srv->per_thr[i].avail_conns = EB_ROOT;
-		MT_LIST_INIT(&srv->per_thr[i].streams);
-	}
-
 	/* the servers are linked backwards
 	 * first into proxy
 	 */
-- 
2.30.1

Reply via email to