]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: server: a separate function for initializing the per_thr field
authorMiroslav Zagorac <mzagorac@haproxy.com>
Tue, 15 Jun 2021 13:33:20 +0000 (15:33 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 17 Jun 2021 14:07:10 +0000 (16:07 +0200)
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
src/cfgparse.c
src/server.c
src/sink.c

index 74ac740537f3ea3388d4a35a3f4950c5cd57f97d..b807a8a04c1c165485044e39a97d6917854700e9 100644 (file)
@@ -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);
index 4fa5bcc59bcbabe9b5ffdef5ab5b75fb118f41ff..aa07a2fd10e1edd74cfc57362be89367a5f59f29 100644 (file)
@@ -3832,21 +3832,13 @@ out_uri_auth_compat:
 
        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) {
index b3d82f10b1f996ddd4d1ddcacb4ecc4cc4e1f616..471139bbaa8e888338f4074c9b9428837bdbecc1 100644 (file)
@@ -4326,6 +4326,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.
  */
@@ -4335,7 +4356,7 @@ 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 next_id, i;
+       int next_id;
        const int parse_flags = SRV_PARSE_DYNAMIC|SRV_PARSE_PARSE_ADDR;
 
        usermsgs_clr("CLI");
@@ -4405,19 +4426,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) {
index 6b8a234fb561727bfc313132f800a421dfaf4c09..30dcfd59581b6b4fa9b4d1308d4cc9cebfad822a 100644 (file)
@@ -939,7 +939,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
@@ -971,17 +970,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
         */