MINOR: server: add server_get_next_id() to find next free server ID

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.
This commit is contained in:
Willy Tarreau 2025-08-23 19:26:08 +02:00
parent 23605eddb1
commit 0b0aefe19b
3 changed files with 20 additions and 2 deletions

View 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(struct proxy *bk, const char *name);
struct server *server_find_unique(struct proxy *bk, const char *name, uint32_t rid); 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); 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 apply_server_state(void);
void srv_compute_all_admin_states(struct proxy *px); void srv_compute_all_admin_states(struct proxy *px);
int srv_set_addr_via_libc(struct server *srv, int *err_code); int srv_set_addr_via_libc(struct server *srv, int *err_code);

View File

@ -3726,7 +3726,7 @@ out_uri_auth_compat:
/* server ID not set, use automatic numbering with first /* server ID not set, use automatic numbering with first
* spare entry starting with next_svid. * 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; newsrv->conf.id.key = newsrv->puid = next_id;
eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id); eb32_insert(&curproxy->conf.used_server_id, &newsrv->conf.id);
} }

View File

@ -866,6 +866,23 @@ static const char *srv_find_best_kw(const char *word)
return best_ptr; 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 */ /* Parse the "backup" server keyword */
static int srv_parse_backup(char **args, int *cur_arg, static int srv_parse_backup(char **args, int *cur_arg,
struct proxy *curproxy, struct server *newsrv, char **err) 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 */ /* generate the server id if not manually specified */
if (!srv->puid) { 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) { if (!next_id) {
ha_alert("Cannot attach server : no id left in proxy\n"); ha_alert("Cannot attach server : no id left in proxy\n");
goto out; goto out;