]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: server: add server_find_by_addr()
authorWilly Tarreau <w@1wt.eu>
Thu, 10 Jul 2025 12:43:28 +0000 (14:43 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 15 Jul 2025 08:30:28 +0000 (10:30 +0200)
Server lookup by address requires locking and manipulation of the tree
from user code. Let's provide server_find_by_addr() which does that for
us.

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

index b1b1588eb32ef391f65904649c0a0defebc29409..15d4c7e0c2a61bffe542c45afa6a72d0b17d75e9 100644 (file)
@@ -61,6 +61,7 @@ const char *srv_update_check_addr_port(struct server *s, const char *addr, const
 const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port);
 struct server *server_find_by_id_unique(struct proxy *bk, int id, uint32_t rid);
 struct server *server_find_by_name(struct proxy *px, const char *name);
+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);
index f92787483ae48132dae05ecab41ca4f0a1002ba9..c77ff5e48ab6601631a37408643231f80dfc783a 100644 (file)
@@ -3994,6 +3994,23 @@ struct server *server_find_by_name(struct proxy *px, const char *name)
        return cursrv;
 }
 
+/*
+ * This function returns the server with a matching address within selected
+ * proxy, or NULL if not found. The proxy lock is taken for reads during this
+ * operation since we don't want the address to change under us.
+ */
+struct server *server_find_by_addr(struct proxy *px, const char *addr)
+{
+       struct ebpt_node *node;
+       struct server *cursrv;
+
+       HA_RWLOCK_RDLOCK(PROXY_LOCK, &px->lock);
+       node = ebis_lookup(&px->used_server_addr, addr);
+       cursrv = node ? container_of(node, struct server, addr_node) : NULL;
+       HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &px->lock);
+       return cursrv;
+}
+
 /* Returns a pointer to the first server matching either name <name>, or id
  * if <name> starts with a '#'. NULL is returned if no match is found.
  * the lookup is performed in the backend <bk>