reverseproxy: skip unavailable upstream with a single weighted_round_robin weight (#8320)

With fewer than two weights, WeightedRoundRobinSelection returned the
first upstream without checking that it is available.
pull/8344/head
D'Ali Ben Macha 2026-11-03 08:01:28 +01:00 committed by GitHub
parent cce035d7ad
commit aeea18be86
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 0 deletions

View File

@ -146,6 +146,9 @@ func (r *WeightedRoundRobinSelection) Select(pool UpstreamPool, _ *http.Request,
return nil
}
if len(r.Weights) < 2 {
if !pool[0].Available() {
return nil
}
return pool[0]
}
var index, totalWeight int

View File

@ -148,6 +148,24 @@ func TestWeightedRoundRobinPolicyFewerWeightsThanUpstreams(t *testing.T) {
}
}
func TestWeightedRoundRobinPolicySingleWeight(t *testing.T) {
pool := testPool()
wrrPolicy := WeightedRoundRobinSelection{
Weights: []int{1},
totalWeight: 1,
}
req, _ := http.NewRequest("GET", "/", nil)
if h := wrrPolicy.Select(pool, req, nil); h != pool[0] {
t.Errorf("got %v, want the only weighted host %v", h, pool[0])
}
pool[0].setHealthy(false)
if h := wrrPolicy.Select(pool, req, nil); h != nil {
t.Errorf("got %v, want nil when the only weighted host is unavailable", h)
}
}
func TestWeightedRoundRobinSelection_Validate(t *testing.T) {
tests := []struct {
name string