Skip to content

Commit 3b7fad6

Browse files
author
Eduard Borges
committed
Add support for the new load balancing random algorithm
1 parent d9ac543 commit 3b7fad6

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

internal/nginx/extensions.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ func ParseLBMethod(method string) (string, error) {
1717
method, err := validateHashLBMethod(method)
1818
return method, err
1919
}
20+
if strings.HasPrefix(method, "random") {
21+
method, err := validateRandomLBMethod(method)
22+
return method, err
23+
}
2024

2125
if method == "least_conn" || method == "ip_hash" {
2226
return method, nil
@@ -45,6 +49,10 @@ func ParseLBMethodForPlus(method string) (string, error) {
4549
method, err := validateHashLBMethod(method)
4650
return method, err
4751
}
52+
if strings.HasPrefix(method, "random") {
53+
method, err := validateRandomLBMethodForPlus(method)
54+
return method, err
55+
}
4856

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

73+
func validateRandomLBMethod(method string) (string, error) {
74+
keyWords := strings.Split(method, " ")
75+
if keyWords[0] == "random" {
76+
if len(keyWords) == 1 || len(keyWords) == 2 && keyWords[1] == "two" ||
77+
len(keyWords) == 3 && keyWords[1] == "two" && keyWords[2] == "least_conn" {
78+
return method, nil
79+
}
80+
}
81+
return "", fmt.Errorf("Invalid load balancing method: %q", method)
82+
}
83+
84+
func validateRandomLBMethodForPlus(method string) (string, error) {
85+
keyWords := strings.Split(method, " ")
86+
if keyWords[0] == "random" {
87+
if len(keyWords) == 1 || len(keyWords) == 2 && keyWords[1] == "two" ||
88+
len(keyWords) == 3 && keyWords[1] == "two" &&
89+
(keyWords[2] == "least_conn" || keyWords[2] == "least_time=header" || keyWords[2] == "least_time=last_byte") {
90+
return method, nil
91+
}
92+
}
93+
return "", fmt.Errorf("Invalid load balancing method: %q", method)
94+
}
95+
6596
// http://nginx.org/en/docs/syntax.html
6697
var validTimeSuffixes = []string{
6798
"ms",

internal/nginx/extensions_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ func TestParseLBMethod(t *testing.T) {
1010
{"least_conn", "least_conn"},
1111
{"round_robin", ""},
1212
{"ip_hash", "ip_hash"},
13+
{"random", "random"},
14+
{"random two", "random two"},
15+
{"random two least_conn", "random two least_conn"},
1316
{"hash $request_id", "hash $request_id"},
1417
{"hash $request_id consistent", "hash $request_id consistent"},
1518
}
@@ -49,6 +52,11 @@ func TestParseLBMethodForPlus(t *testing.T) {
4952
{"least_conn", "least_conn"},
5053
{"round_robin", ""},
5154
{"ip_hash", "ip_hash"},
55+
{"random", "random"},
56+
{"random two", "random two"},
57+
{"random two least_conn", "random two least_conn"},
58+
{"random two least_time=header", "random two least_time=header"},
59+
{"random two least_time=last_byte", "random two least_time=last_byte"},
5260
{"hash $request_id", "hash $request_id"},
5361
{"least_time header", "least_time header"},
5462
{"least_time last_byte", "least_time last_byte"},

0 commit comments

Comments
 (0)