diff --git a/include/haproxy/server.h b/include/haproxy/server.h index 79cf880b3..ad9036109 100644 --- a/include/haproxy/server.h +++ b/include/haproxy/server.h @@ -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); diff --git a/src/cfgparse.c b/src/cfgparse.c index 4e41daa54..b9806bd46 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -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); } diff --git a/src/server.c b/src/server.c index a9dad40aa..a5dc0ff5c 100644 --- a/src/server.c +++ b/src/server.c @@ -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 + * in the proxy . 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;