]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: proxy: no longer allocate the default-server entry by default
authorWilly Tarreau <w@1wt.eu>
Fri, 11 Jul 2025 06:40:17 +0000 (08:40 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 15 Jul 2025 08:39:44 +0000 (10:39 +0200)
The default-server entry used to always be allocated. Now we'll postpone
its allocation for the first time we need it, i.e. during a "default-server"
directive, or when inheriting a defaults section which has one. The memory
savings are significant, on a large configuration with 100k backends and
no default-server directive, the memory usage dropped from 800MB RSS to
420MB (380 MB saved). It should be possible to also address configs using
default-server by releasing this entry when leaving the proxy section,
which is not done yet.

src/proxy.c

index 0eea5de2db2810bdc975a48421b876b95ce3495e..c6f17eb19c8e6d5f8d34a60aecf4e858d4d2338c 100644 (file)
@@ -1685,16 +1685,6 @@ int setup_new_proxy(struct proxy *px, const char *name, unsigned int cap, char *
 {
        init_new_proxy(px);
 
-       /* allocate the default server section */
-       px->defsrv = calloc(1, sizeof(*px->defsrv));
-       if (!px->defsrv) {
-               memprintf(errmsg, "out of memory");
-               goto fail;
-       }
-
-       px->defsrv->id = "default-server";
-       srv_settings_init(px->defsrv);
-
        if (name) {
                px->id = strdup(name);
                if (!px->id) {
@@ -1807,8 +1797,24 @@ static int proxy_defproxy_cpy(struct proxy *curproxy, const struct proxy *defpro
        struct eb32_node *node;
 
        /* set default values from the specified default proxy */
-       if (curproxy->defsrv)
+
+       if (defproxy->defsrv) {
+               if (!curproxy->defsrv) {
+                       /* there's a default-server in the defaults proxy but
+                        * none allocated yet in the current proxy so we have
+                        * to allocate and pre-initialize it right now.
+                        */
+                       curproxy->defsrv = calloc(1, sizeof(*curproxy->defsrv));
+                       if (!curproxy->defsrv) {
+                               memprintf(errmsg, "proxy '%s': out of memory allocating default-server", curproxy->id);
+                               return 1;
+                       }
+
+                       curproxy->defsrv->id = "default-server";
+                       srv_settings_init(curproxy->defsrv);
+               }
                srv_settings_cpy(curproxy->defsrv, defproxy->defsrv, 0);
+       }
 
        curproxy->flags = (defproxy->flags & PR_FL_DISABLED); /* Only inherit from disabled flag */
        curproxy->options = defproxy->options;