[MINOR] externalize the "balance" option parser to backend.c
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.
This commit is contained in:
parent
1a20a5d1b2
commit
a0cbda61a7
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 */
|
||||
|
|
Loading…
Reference in New Issue