Dimitri Masson
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
58 additions and
2 deletions
-
modules/caddyhttp/reverseproxy/selectionpolicies.go
-
modules/caddyhttp/reverseproxy/selectionpolicies_test.go
|
@ -397,13 +397,18 @@ func leastRequests(upstreams []*Upstream) *Upstream { |
|
|
return nil |
|
|
return nil |
|
|
} |
|
|
} |
|
|
var best []*Upstream |
|
|
var best []*Upstream |
|
|
var bestReqs int |
|
|
var bestReqs int = -1 |
|
|
for _, upstream := range upstreams { |
|
|
for _, upstream := range upstreams { |
|
|
|
|
|
if upstream == nil { |
|
|
|
|
|
continue |
|
|
|
|
|
} |
|
|
reqs := upstream.NumRequests() |
|
|
reqs := upstream.NumRequests() |
|
|
if reqs == 0 { |
|
|
if reqs == 0 { |
|
|
return upstream |
|
|
return upstream |
|
|
} |
|
|
} |
|
|
if reqs <= bestReqs { |
|
|
// If bestReqs was just initialized to -1
|
|
|
|
|
|
// we need to append upstream also
|
|
|
|
|
|
if reqs <= bestReqs || bestReqs == -1 { |
|
|
bestReqs = reqs |
|
|
bestReqs = reqs |
|
|
best = append(best, upstream) |
|
|
best = append(best, upstream) |
|
|
} |
|
|
} |
|
|
|
@ -271,3 +271,54 @@ func TestURIHashPolicy(t *testing.T) { |
|
|
t.Error("Expected uri policy policy host to be nil.") |
|
|
t.Error("Expected uri policy policy host to be nil.") |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestLeastRequests(t *testing.T) { |
|
|
|
|
|
pool := testPool() |
|
|
|
|
|
pool[0].Dial = "localhost:8080" |
|
|
|
|
|
pool[1].Dial = "localhost:8081" |
|
|
|
|
|
pool[2].Dial = "localhost:8082" |
|
|
|
|
|
pool[0].SetHealthy(true) |
|
|
|
|
|
pool[1].SetHealthy(true) |
|
|
|
|
|
pool[2].SetHealthy(true) |
|
|
|
|
|
pool[0].CountRequest(10) |
|
|
|
|
|
pool[1].CountRequest(20) |
|
|
|
|
|
pool[2].CountRequest(30) |
|
|
|
|
|
|
|
|
|
|
|
result := leastRequests(pool) |
|
|
|
|
|
|
|
|
|
|
|
if result == nil { |
|
|
|
|
|
t.Error("Least request should not return nil") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if result != pool[0] { |
|
|
|
|
|
t.Error("Least request should return pool[0]") |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func TestRandomChoicePolicy(t *testing.T) { |
|
|
|
|
|
pool := testPool() |
|
|
|
|
|
pool[0].Dial = "localhost:8080" |
|
|
|
|
|
pool[1].Dial = "localhost:8081" |
|
|
|
|
|
pool[2].Dial = "localhost:8082" |
|
|
|
|
|
pool[0].SetHealthy(false) |
|
|
|
|
|
pool[1].SetHealthy(true) |
|
|
|
|
|
pool[2].SetHealthy(true) |
|
|
|
|
|
pool[0].CountRequest(10) |
|
|
|
|
|
pool[1].CountRequest(20) |
|
|
|
|
|
pool[2].CountRequest(30) |
|
|
|
|
|
|
|
|
|
|
|
request := httptest.NewRequest(http.MethodGet, "/test", nil) |
|
|
|
|
|
randomChoicePolicy := new(RandomChoiceSelection) |
|
|
|
|
|
randomChoicePolicy.Choose = 2 |
|
|
|
|
|
|
|
|
|
|
|
h := randomChoicePolicy.Select(pool, request) |
|
|
|
|
|
|
|
|
|
|
|
if h == nil { |
|
|
|
|
|
t.Error("RandomChoicePolicy should not return nil") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if h == pool[0] { |
|
|
|
|
|
t.Error("RandomChoicePolicy should not choose pool[0]") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|