Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the random load balancing method #376

Merged
merged 1 commit into from
Sep 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions internal/nginx/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,34 @@ func ParseLBMethod(method string) (string, error) {
return method, err
}

if method == "least_conn" || method == "ip_hash" {
if _, exists := nginxLBValidInput[method]; exists {
return method, nil
}
return "", fmt.Errorf("Invalid load balancing method: %q", method)
}

var nginxLBValidInput = map[string]bool{
"least_conn": true,
"ip_hash": true,
"random": true,
"random two": true,
"random two least_conn": true,
}

var nginxPlusLBValidInput = map[string]bool{
"least_time": true,
"last_byte": true,
"least_conn": true,
"ip_hash": true,
"least_time header": true,
"least_time last_byte": true,
"least_time header inflight": true,
"least_time last_byte inflight": true,
"least_time": true,
"last_byte": true,
"least_conn": true,
"ip_hash": true,
"random": true,
"random two": true,
"random two least_conn": true,
"random two least_time=header": true,
"random two least_time=last_byte": true,
"least_time header": true,
"least_time last_byte": true,
"least_time header inflight": true,
"least_time last_byte inflight": true,
}

// ParseLBMethodForPlus parses method and matches it to a corresponding load balancing method in NGINX Plus. An error is returned if method is not valid
Expand Down
18 changes: 16 additions & 2 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"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also test for some of invalid input:
random one, random two ip_hash, random two least_time

{"hash $request_id consistent", "hash $request_id consistent"},
}
Expand All @@ -20,6 +23,10 @@ func TestParseLBMethod(t *testing.T) {
"least_time header",
"hash123",
"hash $request_id conwrongspelling",
"random one",
"random two least_time=header",
"random two least_time=last_byte",
"random two ip_hash",
}

for _, test := range testsWithValidInput {
Expand Down Expand Up @@ -49,6 +56,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"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also test on some of invalid input:
random one
random two ip_hash"

{"hash $request_id", "hash $request_id"},
{"least_time header", "least_time header"},
{"least_time last_byte", "least_time last_byte"},
Expand All @@ -61,6 +73,9 @@ func TestParseLBMethodForPlus(t *testing.T) {
"blabla",
"hash123",
"least_time inflight header",
"random one",
"random two ip_hash",
"random two least_time",
}

for _, test := range testsWithValidInput {
Expand All @@ -82,7 +97,6 @@ func TestParseLBMethodForPlus(t *testing.T) {
}
}


func TestParseSlowStart(t *testing.T) {
var testsWithValidInput = []string{"1", "1m10s", "11 11", "5m 30s", "1s", "100m", "5w", "15m", "11M", "3h", "100y", "600"}
var invalidInput = []string{"ss", "rM", "m0m", "s1s", "-5s", "", "1L"}
Expand All @@ -101,4 +115,4 @@ func TestParseSlowStart(t *testing.T) {
t.Errorf("TestParseSlowStart(%q) didn't return error. Returned: %q", test, result)
}
}
}
}