Skip to content

Commit

Permalink
Support the random load balancing method
Browse files Browse the repository at this point in the history
Brings support for the random load balancing method -
http://nginx.org/en/docs/http/ngx_http_upstream_module.html#random
- added in NGINX OSS 1.15.1 and NGINX Plus R16.
  • Loading branch information
Eduard Borges authored and pleshakov committed Sep 17, 2018
1 parent 46b8077 commit 0ea4885
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
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"},
{"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"},
{"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)
}
}
}
}

0 comments on commit 0ea4885

Please sign in to comment.