From: Willy Tarreau Date: Sat, 23 Aug 2025 17:25:03 +0000 (+0200) Subject: MINOR: listener: add listener_get_next_id() to find next free listener ID X-Git-Tag: v3.3-dev9~103 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=23605eddb1f7d359281d544dadbfbff1d3e0c843;p=thirdparty%2Fhaproxy.git MINOR: listener: add listener_get_next_id() to find next free listener 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 listener_get_next_id(). As a bonus it reduces the exposure of the tree's root outside of the functions. --- diff --git a/include/haproxy/listener.h b/include/haproxy/listener.h index fb32f2e89..296fbd139 100644 --- a/include/haproxy/listener.h +++ b/include/haproxy/listener.h @@ -82,6 +82,12 @@ int relax_listener(struct listener *l, int lpx, int lli); */ void stop_listener(struct listener *l, int lpx, int lpr, int lli); +/* This function returns the first unused listener ID greater than or equal to + * in the proxy . Zero is returned if no spare one is found (should + * never happen). + */ +uint listener_get_next_id(const struct proxy *px, uint from); + /* This function adds the specified listener's file descriptor to the polling * lists if it is in the LI_LISTEN state. The listener enters LI_READY or * LI_FULL state depending on its number of connections. In daemon mode, we diff --git a/src/cfgparse.c b/src/cfgparse.c index 059e557a4..4e41daa54 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -4290,7 +4290,7 @@ init_proxies_list_stage2: if (prev_li->luid) next_id = prev_li->luid + 1; } - next_id = get_next_id(&curproxy->conf.used_listener_id, next_id); + next_id = listener_get_next_id(curproxy, next_id); listener->conf.id.key = listener->luid = next_id; eb32_insert(&curproxy->conf.used_listener_id, &listener->conf.id); } diff --git a/src/listener.c b/src/listener.c index 9520a3893..5f74abe5a 100644 --- a/src/listener.c +++ b/src/listener.c @@ -399,6 +399,23 @@ void stop_listener(struct listener *l, int lpx, int lpr, int lli) HA_RWLOCK_WRUNLOCK(PROXY_LOCK, &px->lock); } +/* This function returns the first unused listener ID greater than or equal to + * in the proxy . Zero is returned if no spare one is found (should + * never happen). + */ +uint listener_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_listener_id, from); + if (!used || used->key > from) + return from; /* available */ + from++; + } while (from); + return from; +} + /* This function adds the specified to the protocol . It * does nothing if the protocol was already added. The listener's state is * automatically updated from LI_INIT to LI_ASSIGNED. The number of listeners