]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: proxy/stktable: add resolve_stick_rule helper function
authorAurelien DARRAGON <adarragon@haproxy.com>
Tue, 8 Aug 2023 09:37:59 +0000 (11:37 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 3 Nov 2023 16:30:30 +0000 (17:30 +0100)
Simplify stick and store sticktable proxy rules postparsing by adding
a sticking rule entry resolve (postparsing) function.

This will ease code maintenance.

include/haproxy/proxy.h
src/cfgparse.c
src/proxy.c

index 177e7178397e6821939471cc7cd46341aadf57dd..783154af65ff2098eef1218de8f6700ccbbaf119 100644 (file)
@@ -84,6 +84,7 @@ void proxy_capture_error(struct proxy *proxy, int is_back,
 void proxy_adjust_all_maxconn(void);
 struct proxy *cli_find_frontend(struct appctx *appctx, const char *arg);
 struct proxy *cli_find_frontend(struct appctx *appctx, const char *arg);
+int resolve_stick_rule(struct proxy *curproxy, struct sticking_rule *mrule);
 
 /*
  * This function returns a string containing the type of the proxy in a format
index fc04fc479be932bc703b9b6ecf627cb865c70d2f..9a28fae550ada8fdb7545cc2802a2cc5fd30325e 100644 (file)
@@ -3243,71 +3243,22 @@ init_proxies_list_stage1:
 
                /* find the target table for 'stick' rules */
                list_for_each_entry(mrule, &curproxy->sticking_rules, list) {
-                       struct stktable *target;
-
                        curproxy->be_req_ana |= AN_REQ_STICKING_RULES;
                        if (mrule->flags & STK_IS_STORE)
                                curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
 
-                       if (mrule->table.name)
-                               target = stktable_find_by_name(mrule->table.name);
-                       else
-                               target = curproxy->table;
-
-                       if (!target) {
-                               ha_alert("Proxy '%s': unable to find stick-table '%s'.\n",
-                                        curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
-                               cfgerr++;
-                       }
-                       else if (!stktable_compatible_sample(mrule->expr,  target->type)) {
-                               ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
-                                        curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
+                       if (!resolve_stick_rule(curproxy, mrule))
                                cfgerr++;
-                       }
-                       else {
-                               ha_free(&mrule->table.name);
-                               mrule->table.t = target;
-                               stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL, NULL);
-                               stktable_alloc_data_type(target, STKTABLE_DT_SERVER_KEY, NULL, NULL);
-                               if (!in_proxies_list(target->proxies_list, curproxy)) {
-                                       curproxy->next_stkt_ref = target->proxies_list;
-                                       target->proxies_list = curproxy;
-                               }
-                       }
+
                        err_code |= warnif_tcp_http_cond(curproxy, mrule->cond);
                }
 
                /* find the target table for 'store response' rules */
                list_for_each_entry(mrule, &curproxy->storersp_rules, list) {
-                       struct stktable *target;
-
                        curproxy->be_rsp_ana |= AN_RES_STORE_RULES;
 
-                       if (mrule->table.name)
-                               target = stktable_find_by_name(mrule->table.name);
-                       else
-                               target = curproxy->table;
-
-                       if (!target) {
-                               ha_alert("Proxy '%s': unable to find store table '%s'.\n",
-                                        curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
-                               cfgerr++;
-                       }
-                       else if (!stktable_compatible_sample(mrule->expr, target->type)) {
-                               ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
-                                        curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
+                       if (!resolve_stick_rule(curproxy, mrule))
                                cfgerr++;
-                       }
-                       else {
-                               ha_free(&mrule->table.name);
-                               mrule->table.t = target;
-                               stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL, NULL);
-                               stktable_alloc_data_type(target, STKTABLE_DT_SERVER_KEY, NULL, NULL);
-                               if (!in_proxies_list(target->proxies_list, curproxy)) {
-                                       curproxy->next_stkt_ref = target->proxies_list;
-                                       target->proxies_list = curproxy;
-                               }
-                       }
                }
 
                /* check validity for 'tcp-request' layer 4/5/6/7 rules */
index f8233fc78c382679c664a21da01d87c7985169bd..7ff087190324c6795753fe1c44fd5df7109721e4 100644 (file)
@@ -130,6 +130,41 @@ const struct cfg_opt cfg_opts2[] =
        { NULL, 0, 0, 0 }
 };
 
+/* Helper function to resolve a single sticking rule after config parsing.
+ * Returns 1 for success and 0 for failure
+ */
+int resolve_stick_rule(struct proxy *curproxy, struct sticking_rule *mrule)
+{
+       struct stktable *target;
+
+       if (mrule->table.name)
+               target = stktable_find_by_name(mrule->table.name);
+       else
+               target = curproxy->table;
+
+       if (!target) {
+               ha_alert("Proxy '%s': unable to find stick-table '%s'.\n",
+                        curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
+               return 0;
+       }
+       else if (!stktable_compatible_sample(mrule->expr, target->type)) {
+               ha_alert("Proxy '%s': type of fetch not usable with type of stick-table '%s'.\n",
+                        curproxy->id, mrule->table.name ? mrule->table.name : curproxy->id);
+               return 0;
+       }
+
+       /* success */
+       ha_free(&mrule->table.name);
+       mrule->table.t = target;
+       stktable_alloc_data_type(target, STKTABLE_DT_SERVER_ID, NULL, NULL);
+       stktable_alloc_data_type(target, STKTABLE_DT_SERVER_KEY, NULL, NULL);
+       if (!in_proxies_list(target->proxies_list, curproxy)) {
+               curproxy->next_stkt_ref = target->proxies_list;
+               target->proxies_list = curproxy;
+       }
+       return 1;
+}
+
 static void free_stick_rules(struct list *rules)
 {
        struct sticking_rule *rule, *ruleb;