]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: server: add server_get_next_id() to find next free server ID
authorWilly Tarreau <w@1wt.eu>
Sat, 23 Aug 2025 17:26:08 +0000 (19:26 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 16 Sep 2025 07:23:46 +0000 (09:23 +0200)
This was previously achieved via the generic get_next_id() but we'll soon
get rid of generic ID trees so let's have a dedicated server_get_next_id().
As a bonus it reduces the exposure of the tree's root outside of the functions.

include/haproxy/server.h
src/cfgparse.c
src/server.c

index 79cf880b34b44803c42851e2007faeefbaecc474..ad9036109c6d392481485e75e00735225ca566b1 100644 (file)
@@ -66,6 +66,7 @@ struct server *server_find_by_addr(struct proxy *px, const char *addr);
 struct server *server_find(struct proxy *bk, const char *name);
 struct server *server_find_unique(struct proxy *bk, const char *name, uint32_t rid);
 struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff);
+uint server_get_next_id(const struct proxy *px, uint from);
 void apply_server_state(void);
 void srv_compute_all_admin_states(struct proxy *px);
 int srv_set_addr_via_libc(struct server *srv, int *err_code);
index 4e41daa54aff51db126443f8095c99291846cf2b..b9806bd4607ffe51895fefeaaf1014d46f4ab0a9 100644 (file)
@@ -3726,7 +3726,7 @@ out_uri_auth_compat:
                                /* server ID not set, use automatic numbering with first
                                 * spare entry starting with next_svid.
                                 */
-                               next_id = get_next_id(&curproxy->conf.used_server_id, next_id);
+                               next_id = server_get_next_id(curproxy, next_id);
                                newsrv->conf.id.key = newsrv->puid = next_id;
                                eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
                        }
index a9dad40aa7d6cdeb7fbcffa1117aa3275be8582a..a5dc0ff5c91520a3dd438996b0447c72281e9719 100644 (file)
@@ -866,6 +866,23 @@ static const char *srv_find_best_kw(const char *word)
        return best_ptr;
 }
 
+/* This function returns the first unused server ID greater than or equal to
+ * <from> in the proxy <px>. Zero is returned if no spare one is found (should
+ * never happen).
+ */
+uint server_get_next_id(const struct proxy *px, uint from)
+{
+       const struct eb32_node *used;
+
+       do {
+               used = eb32_lookup_ge((struct eb_root *)&px->conf.used_server_id, from);
+               if (!used || used->key > from)
+                       return from; /* available */
+               from++;
+       } while (from);
+       return from;
+}
+
 /* Parse the "backup" server keyword */
 static int srv_parse_backup(char **args, int *cur_arg,
                             struct proxy *curproxy, struct server *newsrv, char **err)
@@ -6228,7 +6245,7 @@ static int cli_parse_add_server(char **args, char *payload, struct appctx *appct
 
        /* generate the server id if not manually specified */
        if (!srv->puid) {
-               next_id = get_next_id(&be->conf.used_server_id, 1);
+               next_id = server_get_next_id(be, 1);
                if (!next_id) {
                        ha_alert("Cannot attach server : no id left in proxy\n");
                        goto out;