diff --git a/include/proto/backend.h b/include/proto/backend.h index d70c68faf..fc717179b 100644 --- a/include/proto/backend.h +++ b/include/proto/backend.h @@ -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); diff --git a/src/backend.c b/src/backend.c index 758b42967..0cb76f0be 100644 --- a/src/backend.c +++ b/src/backend.c @@ -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 + * . It returns -1 if there is any error, otherwise zero. If it + * returns -1, it may write an error message into ther buffer, for at + * most bytes, trailing zero included. The trailing '\n' will not be + * written. The function must be called with 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 diff --git a/src/cfgparse.c b/src/cfgparse.c index 8e390d563..b98089e9d 100644 --- a/src/cfgparse.c +++ b/src/cfgparse.c @@ -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 */