Skip to content

Commit

Permalink
Add support for the new load balancing random algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduard Borges committed Sep 14, 2018
1 parent d9ac543 commit 3b7fad6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
31 changes: 31 additions & 0 deletions internal/nginx/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ func ParseLBMethod(method string) (string, error) {
method, err := validateHashLBMethod(method)
return method, err
}
if strings.HasPrefix(method, "random") {
method, err := validateRandomLBMethod(method)
return method, err
}

if method == "least_conn" || method == "ip_hash" {
return method, nil
Expand Down Expand Up @@ -45,6 +49,10 @@ func ParseLBMethodForPlus(method string) (string, error) {
method, err := validateHashLBMethod(method)
return method, err
}
if strings.HasPrefix(method, "random") {
method, err := validateRandomLBMethodForPlus(method)
return method, err
}

if _, exists := nginxPlusLBValidInput[method]; exists {
return method, nil
Expand All @@ -62,6 +70,29 @@ func validateHashLBMethod(method string) (string, error) {
return "", fmt.Errorf("Invalid load balancing method: %q", method)
}

func validateRandomLBMethod(method string) (string, error) {
keyWords := strings.Split(method, " ")
if keyWords[0] == "random" {
if len(keyWords) == 1 || len(keyWords) == 2 && keyWords[1] == "two" ||
len(keyWords) == 3 && keyWords[1] == "two" && keyWords[2] == "least_conn" {
return method, nil
}
}
return "", fmt.Errorf("Invalid load balancing method: %q", method)
}

func validateRandomLBMethodForPlus(method string) (string, error) {
keyWords := strings.Split(method, " ")
if keyWords[0] == "random" {
if len(keyWords) == 1 || len(keyWords) == 2 && keyWords[1] == "two" ||
len(keyWords) == 3 && keyWords[1] == "two" &&
(keyWords[2] == "least_conn" || keyWords[2] == "least_time=header" || keyWords[2] == "least_time=last_byte") {
return method, nil
}
}
return "", fmt.Errorf("Invalid load balancing method: %q", method)
}

// http://nginx.org/en/docs/syntax.html
var validTimeSuffixes = []string{
"ms",
Expand Down
8 changes: 8 additions & 0 deletions internal/nginx/extensions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ func TestParseLBMethod(t *testing.T) {
{"least_conn", "least_conn"},
{"round_robin", ""},
{"ip_hash", "ip_hash"},
{"random", "random"},
{"random two", "random two"},
{"random two least_conn", "random two least_conn"},
{"hash $request_id", "hash $request_id"},
{"hash $request_id consistent", "hash $request_id consistent"},
}
Expand Down Expand Up @@ -49,6 +52,11 @@ func TestParseLBMethodForPlus(t *testing.T) {
{"least_conn", "least_conn"},
{"round_robin", ""},
{"ip_hash", "ip_hash"},
{"random", "random"},
{"random two", "random two"},
{"random two least_conn", "random two least_conn"},
{"random two least_time=header", "random two least_time=header"},
{"random two least_time=last_byte", "random two least_time=last_byte"},
{"hash $request_id", "hash $request_id"},
{"least_time header", "least_time header"},
{"least_time last_byte", "least_time last_byte"},
Expand Down

0 comments on commit 3b7fad6

Please sign in to comment.