]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: server: add and use srv_init() function
authorAurelien DARRAGON <adarragon@haproxy.com>
Fri, 9 May 2025 18:27:29 +0000 (20:27 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Mon, 2 Jun 2025 15:51:33 +0000 (17:51 +0200)
rename _srv_postparse() internal function to srv_init() function and group
srv_init_per_thr() plus idle conns list init inside it. This way we can
perform some simplifications as srv_init() performs multiple server
init steps after parsing.

SRV_F_CHECKED flag was added, it is automatically set when srv_init()
runs successfully. If the flag is already set and srv_init() is called
again, nothing is done. This permis to manually call srv_init() earlier
than the default POST_CHECK hook when needed without risking to do things
twice.

include/haproxy/server-t.h
include/haproxy/server.h
src/cfgparse.c
src/server.c
src/sink.c

index 19b84a5f4fb6d6aa876feb4a19623489a8325296..dc238ec255aa6618d124ce98d6f9201706fafee5 100644 (file)
@@ -171,6 +171,7 @@ enum srv_init_state {
 #define SRV_F_DEFSRV_USE_SSL 0x4000      /* default-server uses SSL */
 #define SRV_F_DELETED 0x8000             /* srv is deleted but not yet purged */
 #define SRV_F_STRICT_MAXCONN 0x10000     /* maxconn is to be strictly enforced, as a limit of outbound connections */
+#define SRV_F_CHECKED      0x20000       /* set once server was postparsed */
 
 /* configured server options for send-proxy (server->pp_opts) */
 #define SRV_PP_V1               0x0001   /* proxy protocol version 1 */
index f085074fcd2d3c21b4e8af5857025a53d4cb28f4..deb6ec2f7d86d1b87638028a87342b63565cd531 100644 (file)
@@ -73,7 +73,7 @@ struct server *new_server(struct proxy *proxy);
 void srv_take(struct server *srv);
 struct server *srv_drop(struct server *srv);
 void srv_free_params(struct server *srv);
-int srv_init_per_thr(struct server *srv);
+int srv_init(struct server *srv);
 void srv_set_ssl(struct server *s, int use_ssl);
 const char *srv_adm_st_chg_cause(enum srv_adm_st_chg_cause cause);
 const char *srv_op_st_chg_cause(enum srv_op_st_chg_cause cause);
index 9c334a22727f316e2601e20923efcad24db74992..887669a43d275ecdb1235943eacd64e7ded89632 100644 (file)
@@ -2817,28 +2817,13 @@ int check_config_validity()
 
        /*
         * we must finish to initialize certain things on the servers,
-        * as some of the per_thr/per_tgrp fields may be accessed soon
+        * as some of the fields may be accessed soon
         */
-
        MT_LIST_FOR_EACH_ENTRY_LOCKED(newsrv, &servers_list, global_list, back) {
-               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);
+               if (srv_init(newsrv) & ERR_CODE) {
                        cfgerr++;
                        continue;
                }
-
-               /* initialize idle conns lists */
-               if (newsrv->max_idle_conns != 0) {
-                       newsrv->curr_idle_thr = calloc(global.nbthread, sizeof(*newsrv->curr_idle_thr));
-                       if (!newsrv->curr_idle_thr) {
-                               ha_alert("parsing [%s:%d] : failed to allocate idle connection tasks for server '%s'.\n",
-                                        newsrv->conf.file, newsrv->conf.line, newsrv->id);
-                               cfgerr++;
-                               continue;
-                       }
-
-               }
        }
 
        /* starting to initialize the main proxies list */
index 1cf324f79e4cd88d22d3e6acad5bf1f6fc8d511d..aae4cfa78cd534e9a70505796af87711cd6753ce 100644 (file)
@@ -3343,7 +3343,7 @@ static int _srv_parse_tmpl_init(struct server *srv, struct proxy *px)
  * This function is expected to be called after _srv_parse_init() initialization
  * but only when the effective server's proxy mode is known, which is not always
  * the case during parsing time, in which case the function will be called during
- * postparsing thanks to the _srv_postparse() below.
+ * postparsing thanks to the srv_init() below.
  *
  * Returns ERR_NONE on success else a combination or ERR_CODE.
  */
@@ -3399,8 +3399,8 @@ static int _srv_check_proxy_mode(struct server *srv, char postparse)
 
        return err_code;
 }
-
-/* Perform some server postparsing checks / tasks:
+/* Finish initializing the server after parsing
+ *
  * We must be careful that checks / postinits performed within this function
  * don't depend or conflict with other postcheck functions that are registered
  * using REGISTER_POST_SERVER_CHECK() hook.
@@ -3409,10 +3409,14 @@ static int _srv_check_proxy_mode(struct server *srv, char postparse)
  */
 static int init_srv_requeue(struct server *srv);
 static int init_srv_slowstart(struct server *srv);
-static int _srv_postparse(struct server *srv)
+static int srv_init_per_thr(struct server *srv);
+int srv_init(struct server *srv)
 {
        int err_code = ERR_NONE;
 
+       if (srv->flags & SRV_F_CHECKED)
+               return ERR_NONE; // nothing to do
+
        err_code |= _srv_check_proxy_mode(srv, 1);
 
        if (err_code & ERR_CODE)
@@ -3428,10 +3432,29 @@ static int _srv_postparse(struct server *srv)
        if (err_code & ERR_CODE)
                goto out;
 
+       if (srv_init_per_thr(srv) == -1) {
+               ha_alert("error during per-thread init for %s/%s server\n", srv->proxy->id, srv->id);
+               err_code |= ERR_ALERT | ERR_FATAL;
+               goto out;
+       }
+
+       /* initialize idle conns lists */
+       if (srv->max_idle_conns != 0) {
+               srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
+               if (!srv->curr_idle_thr) {
+                       ha_alert("memory error during idle conn list init for %s/%s server\n",
+                                srv->proxy->id, srv->id);
+                       err_code |= ERR_ALERT | ERR_FATAL;
+                       goto out;
+               }
+       }
+
  out:
+       if (!(err_code & ERR_CODE))
+               srv->flags |= SRV_F_CHECKED;
        return err_code;
 }
-REGISTER_POST_SERVER_CHECK(_srv_postparse);
+REGISTER_POST_SERVER_CHECK(srv_init);
 
 /* Allocate a new server pointed by <srv> and try to parse the first arguments
  * in <args> as an address for a server or an address-range for a template or
@@ -5793,7 +5816,7 @@ static int init_srv_requeue(struct server *srv)
 /* 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)
+static int srv_init_per_thr(struct server *srv)
 {
        int i;
 
@@ -5992,19 +6015,6 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct
                }
        }
 
-       if (srv_init_per_thr(srv) == -1) {
-               ha_alert("failed to allocate per-thread lists for server.\n");
-               goto out;
-       }
-
-       if (srv->max_idle_conns != 0) {
-               srv->curr_idle_thr = calloc(global.nbthread, sizeof(*srv->curr_idle_thr));
-               if (!srv->curr_idle_thr) {
-                       ha_alert("failed to allocate counters for server.\n");
-                       goto out;
-               }
-       }
-
        if (!srv_alloc_lb(srv, be)) {
                ha_alert("Failed to initialize load-balancing data.\n");
                goto out;
@@ -6051,7 +6061,7 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct
                srv->agent.state &= ~CHK_ST_ENABLED;
        }
 
-       errcode = _srv_postparse(srv);
+       errcode = srv_init(srv);
        if (errcode)
                goto out;
 
index 1a6f5bfa2a508a9aa9827e5e82634dd3f8ba6fa4..b05e55dad95f8a4fcd17f59bb4949e188b6ba924 100644 (file)
@@ -1267,10 +1267,6 @@ struct sink *sink_new_from_logger(struct logger *logger)
        srv->svc_port = get_host_port(logger->target.addr);
        HA_SPIN_INIT(&srv->lock);
 
-       /* process per thread init */
-       if (srv_init_per_thr(srv) == -1)
-               goto error;
-
        if (sink_finalize(sink) & ERR_CODE)
                goto error_final;