From: Aurelien DARRAGON Date: Wed, 18 Oct 2023 15:09:12 +0000 (+0200) Subject: MINOR: server: add helper function to detach server from proxy list X-Git-Tag: v2.9-dev9~31 X-Git-Url: http://git.haproxy.org/?a=commitdiff_plain;h=1822e8998b220c7f79148cdd06c132e8ea43aa45;p=haproxy-3.0.git MINOR: server: add helper function to detach server from proxy list Remove some code duplication by introducing a basic helper function to detach a server from its parent proxy. It is supported to call the function even if the server is not yet listed in the proxy list. If the server is not yet listed in the proxy, the function will do nothing. In delete_server(), we previously performed some BUG_ON() to ensure that the detach always succeeded given that we were certain that the server was in the proxy list because it was retrieved through get_backend_server(). However this test is superfluous, we can safely assume that the operation will always succeed if get_backend_server() returned != NULL (we're under full thread isolation), and if it's not the case, then we have a bigger API issue anyway.. --- diff --git a/src/server.c b/src/server.c index c73ffb3..2418503 100644 --- a/src/server.c +++ b/src/server.c @@ -2573,6 +2573,26 @@ struct server *srv_drop(struct server *srv) return next; } +/* Detach server from proxy list. It is supported to call this + * even if the server is not yet in the list + */ +static void _srv_detach(struct server *srv) +{ + struct proxy *be = srv->proxy; + + if (be->srv == srv) { + be->srv = srv->next; + } + else { + struct server *prev; + + for (prev = be->srv; prev && prev->next != srv; prev = prev->next) + ; + if (prev) + prev->next = srv->next; + } +} + /* Remove a server from a tracking list if is tracking another * server. No special care is taken if is tracked itself by another one : * this situation should be avoided by the caller. @@ -5181,17 +5201,7 @@ out: free_check(&srv->agent); /* remove the server from the proxy linked list */ - if (be->srv == srv) { - be->srv = srv->next; - } - else { - struct server *prev; - for (prev = be->srv; prev && prev->next != srv; prev = prev->next) - ; - if (prev) - prev->next = srv->next; - } - + _srv_detach(srv); } thread_release(); @@ -5288,23 +5298,7 @@ static int cli_parse_delete_server(char **args, char *payload, struct appctx *ap * The proxy servers list is currently not protected by a lock, so this * requires thread_isolate/release. */ - - /* be->srv cannot be empty since we have already found the server with - * get_backend_server */ - BUG_ON(!be->srv); - if (be->srv == srv) { - be->srv = srv->next; - } - else { - struct server *next; - for (next = be->srv; srv != next->next; next = next->next) { - /* srv cannot be not found since we have already found - * it with get_backend_server */ - BUG_ON(!next); - } - - next->next = srv->next; - } + _srv_detach(srv); /* Some deleted servers could still point to us using their 'next', * update them as needed