-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from Dean-Coakley/lb-method
Change the default load balancing method to least_conn
- Loading branch information
Showing
7 changed files
with
176 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package nginx | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// ParseLBMethod parses method and matches it to a corresponding load balancing method in NGINX. An error is returned if method is not valid | ||
func ParseLBMethod(method string) (string, error) { | ||
method = strings.TrimSpace(method) | ||
if method == "round_robin" { | ||
return "", nil | ||
} | ||
if strings.HasPrefix(method, "hash") { | ||
method, err := validateHashLBMethod(method) | ||
return method, err | ||
} | ||
|
||
if method == "least_conn" || method == "ip_hash" { | ||
return method, nil | ||
} | ||
return "", fmt.Errorf("Invalid load balancing method: %q", method) | ||
} | ||
|
||
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, | ||
} | ||
|
||
// ParseLBMethodForPlus parses method and matches it to a corresponding load balancing method in NGINX Plus. An error is returned if method is not valid | ||
func ParseLBMethodForPlus(method string) (string, error) { | ||
method = strings.TrimSpace(method) | ||
if method == "round_robin" { | ||
return "", nil | ||
} | ||
if strings.HasPrefix(method, "hash") { | ||
method, err := validateHashLBMethod(method) | ||
return method, err | ||
} | ||
|
||
if _, exists := nginxPlusLBValidInput[method]; exists { | ||
return method, nil | ||
} | ||
return "", fmt.Errorf("Invalid load balancing method: %q", method) | ||
} | ||
|
||
func validateHashLBMethod(method string) (string, error) { | ||
keyWords := strings.Split(method, " ") | ||
if keyWords[0] == "hash" { | ||
if len(keyWords) == 2 || len(keyWords) == 3 && keyWords[2] == "consistent" { | ||
return method, nil | ||
} | ||
} | ||
return "", fmt.Errorf("Invalid load balancing method: %q", method) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package nginx | ||
|
||
import "testing" | ||
|
||
func TestParseLBMethod(t *testing.T) { | ||
var testsWithValidInput = []struct { | ||
input string | ||
expected string | ||
}{ | ||
{"least_conn", "least_conn"}, | ||
{"round_robin", ""}, | ||
{"ip_hash", "ip_hash"}, | ||
{"hash $request_id", "hash $request_id"}, | ||
{"hash $request_id consistent", "hash $request_id consistent"}, | ||
} | ||
|
||
var invalidInput = []string{ | ||
"", | ||
"blabla", | ||
"least_time header", | ||
"hash123", | ||
"hash $request_id conwrongspelling", | ||
} | ||
|
||
for _, test := range testsWithValidInput { | ||
result, err := ParseLBMethod(test.input) | ||
if err != nil { | ||
t.Errorf("TestParseLBMethod(%q) returned an error for valid input", test.input) | ||
} | ||
|
||
if result != test.expected { | ||
t.Errorf("TestParseLBMethod(%q) returned %q expected %q", test.input, result, test.expected) | ||
} | ||
} | ||
|
||
for _, input := range invalidInput { | ||
_, err := ParseLBMethod(input) | ||
if err == nil { | ||
t.Errorf("TestParseLBMethod(%q) does not return an error for invalid input", input) | ||
} | ||
} | ||
} | ||
|
||
func TestParseLBMethodForPlus(t *testing.T) { | ||
var testsWithValidInput = []struct { | ||
input string | ||
expected string | ||
}{ | ||
{"least_conn", "least_conn"}, | ||
{"round_robin", ""}, | ||
{"ip_hash", "ip_hash"}, | ||
{"hash $request_id", "hash $request_id"}, | ||
{"least_time header", "least_time header"}, | ||
{"least_time last_byte", "least_time last_byte"}, | ||
{"least_time header inflight", "least_time header inflight"}, | ||
{"least_time last_byte inflight", "least_time last_byte inflight"}, | ||
} | ||
|
||
var invalidInput = []string{ | ||
"", | ||
"blabla", | ||
"hash123", | ||
"least_time inflight header", | ||
} | ||
|
||
for _, test := range testsWithValidInput { | ||
result, err := ParseLBMethodForPlus(test.input) | ||
if err != nil { | ||
t.Errorf("TestParseLBMethod(%q) returned an error for valid input", test.input) | ||
} | ||
|
||
if result != test.expected { | ||
t.Errorf("TestParseLBMethod(%q) returned %q expected %q", test.input, result, test.expected) | ||
} | ||
} | ||
|
||
for _, input := range invalidInput { | ||
_, err := ParseLBMethodForPlus(input) | ||
if err == nil { | ||
t.Errorf("TestParseLBMethod(%q) does not return an error for invalid input", input) | ||
} | ||
} | ||
} |