]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] externalize the "balance" option parser to backend.c
authorWilly Tarreau <w@1wt.eu>
Thu, 1 Nov 2007 20:39:54 +0000 (21:39 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 1 Nov 2007 22:04:55 +0000 (23:04 +0100)
A new function "backend_parse_balance" has been created in backend.c,
which is dedicated to the parsing of the "balance" keyword. It will
provide easier methods for adding new algorithms.

include/proto/backend.h
src/backend.c
src/cfgparse.c

index d70c68faf030c61f46d911b513766ffc22f33b79..fc717179b132d082d6c927d0aea195b45ea80c7f 100644 (file)
@@ -36,6 +36,8 @@ int connect_server(struct session *s);
 int srv_count_retry_down(struct session *t, int conn_err);
 int srv_retryable_connect(struct session *t);
 int srv_redispatch_connect(struct session *t);
+int backend_parse_balance(const char **args, char *err,
+                         int errlen, struct proxy *curproxy);
 
 void recount_servers(struct proxy *px);
 void recalc_server_map(struct proxy *px);
index 758b42967c5e51c581cfaca7f6d4c8c686166eb7..0cb76f0beeccec75589a346430757dce30a37288 100644 (file)
@@ -728,13 +728,47 @@ int srv_redispatch_connect(struct session *t)
 }
 
 int be_downtime(struct proxy *px) {
-
        if ((px->srv_act || px->srv_bck) && px->last_change < now.tv_sec)               // ignore negative time
                return px->down_time;
 
        return now.tv_sec - px->last_change + px->down_time;
 }
 
+/* This function parses a "balance" statement in a backend section describing
+ * <curproxy>. It returns -1 if there is any error, otherwise zero. If it
+ * returns -1, it may write an error message into ther <err> buffer, for at
+ * most <errlen> bytes, trailing zero included. The trailing '\n' will not be
+ * written. The function must be called with <args> pointing to the first word
+ * after "balance".
+ */
+int backend_parse_balance(const char **args, char *err, int errlen, struct proxy *curproxy)
+{
+       if (!*(args[0])) {
+               /* if no option is set, use round-robin by default */
+               curproxy->options &= ~PR_O_BALANCE;
+               curproxy->options |= PR_O_BALANCE_RR;
+               return 0;
+       }
+
+       if (!strcmp(args[0], "roundrobin")) {
+               curproxy->options &= ~PR_O_BALANCE;
+               curproxy->options |= PR_O_BALANCE_RR;
+       }
+       else if (!strcmp(args[0], "source")) {
+               curproxy->options &= ~PR_O_BALANCE;
+               curproxy->options |= PR_O_BALANCE_SH;
+       }
+       else if (!strcmp(args[0], "uri")) {
+               curproxy->options &= ~PR_O_BALANCE;
+               curproxy->options |= PR_O_BALANCE_UH;
+       }
+       else {
+               snprintf(err, errlen, "'balance' only supports 'roundrobin', 'source' and 'uri' options.");
+               return -1;
+       }
+       return 0;
+}
+
 /*
  * Local variables:
  *  c-indent-level: 8
index 8e390d56381199e32c90ff2bb79133870e702836..b98089e9d3a7d7d380f8e811c351c15d9536a7cf 100644 (file)
@@ -1326,27 +1326,10 @@ int cfg_parse_listen(const char *file, int linenum, char **args)
                if (warnifnotcap(curproxy, PR_CAP_BE, file, linenum, args[0], NULL))
                        return 0;
 
-               if (*(args[1])) {
-                       if (!strcmp(args[1], "roundrobin")) {
-                               curproxy->options &= ~PR_O_BALANCE;
-                               curproxy->options |= PR_O_BALANCE_RR;
-                       }
-                       else if (!strcmp(args[1], "source")) {
-                               curproxy->options &= ~PR_O_BALANCE;
-                               curproxy->options |= PR_O_BALANCE_SH;
-                       }
-                       else if (!strcmp(args[1], "uri")) {
-                               curproxy->options &= ~PR_O_BALANCE;
-                               curproxy->options |= PR_O_BALANCE_UH;
-                       }
-                       else {
-                               Alert("parsing [%s:%d] : '%s' only supports 'roundrobin', 'source' and 'uri' options.\n", file, linenum, args[0]);
-                               return -1;
-                       }
-               }
-               else {/* if no option is set, use round-robin by default */
-                       curproxy->options &= ~PR_O_BALANCE;
-                       curproxy->options |= PR_O_BALANCE_RR;
+               memcpy(trash, "error near 'balance'", 19);
+               if (backend_parse_balance((const char **)args + 1, trash, sizeof(trash), curproxy) < 0) {
+                       Alert("parsing [%s:%d] : %s\n", file, linenum, trash);
+                       return -1;
                }
        }
        else if (!strcmp(args[0], "server")) {  /* server address */