From: Aurelien DARRAGON Date: Wed, 22 Feb 2023 08:55:05 +0000 (+0100) Subject: MINOR: proxy: add findserver_unique_id() and findserver_unique_name() X-Git-Tag: v2.8-dev7~28 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4e5e2664;p=thirdparty%2Fhaproxy.git MINOR: proxy: add findserver_unique_id() and findserver_unique_name() Adding alternative findserver() functions to be able to perform an unique match based on name or puid and by leveraging revision id (rid) to make sure the function won't match with a new server reusing the same name or puid of the "potentially deleted" server we were initially looking for. For example, if you were in the position of finding a server based on a given name provided to you by a different context: Since dynamic servers were implemented, between the time the name was picked and the time you will perform the findserver() call some dynamic server deletion/additions could've been performed in the mean time. In such cases, findserver() could return a new server that re-uses the name of a previously deleted server. Depending on your needs, it could be perfectly fine, but there are some cases where you want to lookup the original server that was provided to you (if it still exists). --- diff --git a/include/haproxy/proxy.h b/include/haproxy/proxy.h index 27a7ae861e..039077242c 100644 --- a/include/haproxy/proxy.h +++ b/include/haproxy/proxy.h @@ -58,6 +58,8 @@ struct proxy *proxy_find_by_id(int id, int cap, int table); struct proxy *proxy_find_by_name(const char *name, int cap, int table); struct proxy *proxy_find_best_match(int cap, const char *name, int id, int *diff); struct server *findserver(const struct proxy *px, const char *name); +struct server *findserver_unique_id(const struct proxy *px, int puid, uint32_t rid); +struct server *findserver_unique_name(const struct proxy *px, const char *name, uint32_t rid); int proxy_cfg_ensure_no_http(struct proxy *curproxy); void init_new_proxy(struct proxy *p); void proxy_preset_defaults(struct proxy *defproxy); diff --git a/src/proxy.c b/src/proxy.c index 89c9cbe8f1..8d4de56e3a 100644 --- a/src/proxy.c +++ b/src/proxy.c @@ -1285,6 +1285,50 @@ struct server *findserver(const struct proxy *px, const char *name) { return target; } +/* + * This function finds a server with matching " x " within + * selected proxy . + * Using the combination of proxy-uid + revision id ensures that the function + * will either return the server we're expecting or NULL if it has been removed + * from the proxy. + */ +struct server *findserver_unique_id(const struct proxy *px, int puid, uint32_t rid) { + + struct server *cursrv; + + if (!px) + return NULL; + + for (cursrv = px->srv; cursrv; cursrv = cursrv->next) { + if (cursrv->puid == puid && cursrv->rid == rid) + return cursrv; + } + + return NULL; +} + +/* + * This function finds a server with matching " x " within + * selected proxy . + * Using the combination of name + revision id ensures that the function will + * either return the server we're expecting or NULL if it has been removed + * from the proxy. + */ +struct server *findserver_unique_name(const struct proxy *px, const char *name, uint32_t rid) { + + struct server *cursrv; + + if (!px) + return NULL; + + for (cursrv = px->srv; cursrv; cursrv = cursrv->next) { + if (!strcmp(cursrv->id, name) && cursrv->rid == rid) + return cursrv; + } + + return NULL; +} + /* This function checks that the designated proxy has no http directives * enabled. It will output a warning if there are, and will fix some of them. * It returns the number of fatal errors encountered. This should be called