reverseproxy: Don't panic in weighted_round_robin with fewer weights than upstreams (#8159)

(cherry picked from commit 8ae69fae1e)
This commit is contained in:
Jay Hemnani 2026-10-07 10:06:10 +05:30 • committed by Francis Lavoie
parent 80b97a436d
commit 053173babf
No known key found for this signature in database
2 changed files with 18 additions and 1 deletions

View file

@ -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)

View file

@ -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