mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-21 07:19:40 +00:00
reverseproxy: Don't panic in weighted_round_robin with fewer weights than upstreams (#8159)
(cherry picked from commit 8ae69fae1e)
This commit is contained in:
parent
80b97a436d
commit
053173babf
2 changed files with 18 additions and 1 deletions
|
|
@ -167,7 +167,7 @@ func (r *WeightedRoundRobinSelection) Select(pool UpstreamPool, _ *http.Request,
|
|||
|
||||
upstreams := make([]*Upstream, 0, len(weights))
|
||||
for i, upstream := range pool {
|
||||
if !upstream.Available() || r.Weights[i] == 0 {
|
||||
if i >= len(r.Weights) || !upstream.Available() || r.Weights[i] == 0 {
|
||||
continue
|
||||
}
|
||||
upstreams = append(upstreams, upstream)
|
||||
|
|
|
|||
|
|
@ -131,6 +131,23 @@ func TestWeightedRoundRobinPolicy(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestWeightedRoundRobinPolicyFewerWeightsThanUpstreams(t *testing.T) {
|
||||
pool := testPool()
|
||||
wrrPolicy := WeightedRoundRobinSelection{
|
||||
Weights: []int{1, 1},
|
||||
totalWeight: 2,
|
||||
}
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
|
||||
pool[0].setHealthy(false)
|
||||
for i := 0; i < 4; i++ {
|
||||
h := wrrPolicy.Select(pool, req, nil)
|
||||
if h != pool[1] {
|
||||
t.Fatalf("Selection %d: got %v, want the only available weighted host %v", i+1, h, pool[1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestWeightedRoundRobinSelection_Validate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue