From: Willy Tarreau Date: Fri, 11 Jul 2025 06:40:17 +0000 (+0200) Subject: MEDIUM: proxy: no longer allocate the default-server entry by default X-Git-Tag: v3.3-dev4~84 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=49a619acae49321681b8e1b2b42d480a6a75ef46;p=thirdparty%2Fhaproxy.git MEDIUM: proxy: no longer allocate the default-server entry by default 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. --- diff --git a/src/proxy.c b/src/proxy.c index 0eea5de2d..c6f17eb19 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -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;