]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: proxy: add setup_new_proxy() function
authorAurelien DARRAGON <adarragon@haproxy.com>
Wed, 9 Apr 2025 19:05:35 +0000 (21:05 +0200)
committerAurelien DARRAGON <adarragon@haproxy.com>
Thu, 10 Apr 2025 20:10:31 +0000 (22:10 +0200)
Split alloc_new_proxy() in two functions: the preparing part is now
handled by setup_new_proxy() which can be called individually, while
alloc_new_proxy() takes care of allocating a new proxy struct and then
calling setup_new_proxy() with the freshly allocated proxy.

include/haproxy/proxy.h
src/proxy.c

index 5abbeb5f3e5b41f8cbc165c57cd5928102fdd840..1666b00ba90af1cd4928b2ee62f350e05bb28444 100644 (file)
@@ -70,6 +70,7 @@ void proxy_destroy_all_unref_defaults(void);
 void proxy_ref_defaults(struct proxy *px, struct proxy *defpx);
 void proxy_unref_defaults(struct proxy *px);
 void proxy_unref_or_destroy_defaults(struct proxy *px);
+int setup_new_proxy(struct proxy *px, const char *name, unsigned int cap, char **errmsg);
 struct proxy *alloc_new_proxy(const char *name, unsigned int cap,
                               char **errmsg);
 struct proxy *parse_new_proxy(const char *name, unsigned int cap,
index 13be57e40d0dc5aade44213fa148e7a54ba9e7d9..f1f2a3f031af44f0f51bc2e2c22488705f25ab4c 100644 (file)
@@ -1674,12 +1674,47 @@ void proxy_unref_defaults(struct proxy *px)
        px->defpx = NULL;
 }
 
+/* prepares a new proxy <name> of type <cap> from the provided <px>
+ * pointer.
+ * <px> is assumed to be freshly allocated
+ * <name> may be NULL: proxy id assigment will be skipped.
+ *
+ * Returns a 1 on success or 0 on failure (in which case errmsg must be checked
+ * then freed).
+ */
+int setup_new_proxy(struct proxy *px, const char *name, unsigned int cap, char **errmsg)
+{
+       uint last_change;
+
+       init_new_proxy(px);
+
+       last_change = ns_to_sec(now_ns);
+       if (cap & PR_CAP_FE)
+               px->fe_counters.last_change = last_change;
+       if (cap & PR_CAP_BE)
+               px->be_counters.last_change = last_change;
+
+       if (name) {
+               px->id = strdup(name);
+               if (!px->id) {
+                       memprintf(errmsg, "proxy '%s': out of memory", name);
+                       return 0;
+               }
+       }
+
+       px->cap = cap;
+
+       if (name && !(cap & PR_CAP_INT))
+               proxy_store_name(px);
+
+       return 1;
+}
+
 /* Allocates a new proxy <name> of type <cap>.
  * Returns the proxy instance on success. On error, NULL is returned.
  */
 struct proxy *alloc_new_proxy(const char *name, unsigned int cap, char **errmsg)
 {
-       uint last_change;
        struct proxy *curproxy;
 
        if ((curproxy = calloc(1, sizeof(*curproxy))) == NULL) {
@@ -1687,19 +1722,8 @@ struct proxy *alloc_new_proxy(const char *name, unsigned int cap, char **errmsg)
                goto fail;
        }
 
-       init_new_proxy(curproxy);
-
-       last_change = ns_to_sec(now_ns);
-       if (cap & PR_CAP_FE)
-               curproxy->fe_counters.last_change = last_change;
-       if (cap & PR_CAP_BE)
-               curproxy->be_counters.last_change = last_change;
-
-       curproxy->id = strdup(name);
-       curproxy->cap = cap;
-
-       if (!(cap & PR_CAP_INT))
-               proxy_store_name(curproxy);
+       if (!setup_new_proxy(curproxy, name, cap, errmsg))
+               goto fail;
 
  done:
        return curproxy;