diff --git a/modules/caddyhttp/reverseproxy/selectionpolicies.go b/modules/caddyhttp/reverseproxy/selectionpolicies.go index 20276bec9..751aa0306 100644 --- a/modules/caddyhttp/reverseproxy/selectionpolicies.go +++ b/modules/caddyhttp/reverseproxy/selectionpolicies.go @@ -142,46 +142,31 @@ func (r *WeightedRoundRobinSelection) Validate() error { // Select returns an available host, if any. func (r *WeightedRoundRobinSelection) Select(pool UpstreamPool, _ *http.Request, _ http.ResponseWriter) *Upstream { - if len(pool) == 0 { - return nil + type weightedUpstream struct { + upstream *Upstream + weight int } - if len(r.Weights) < 2 { - if !pool[0].Available() { - return nil - } - return pool[0] - } - var index, totalWeight int - weights := make([]int, 0, len(r.Weights)) - - for _, w := range r.Weights { - if w > 0 { - weights = append(weights, w) - } - } - currentWeight := int(r.index.Add(1)) % r.totalWeight - for i, weight := range weights { - totalWeight += weight - if currentWeight < totalWeight { - index = i - break - } - } - - upstreams := make([]*Upstream, 0, len(weights)) + upstreams := make([]weightedUpstream, 0, len(pool)) + var totalWeight int for i, upstream := range pool { - if i >= len(r.Weights) || !upstream.Available() || r.Weights[i] == 0 { + if i >= len(r.Weights) || r.Weights[i] <= 0 || !upstream.Available() { continue } - upstreams = append(upstreams, upstream) - if len(upstreams) == cap(upstreams) { - break - } + upstreams = append(upstreams, weightedUpstream{upstream, r.Weights[i]}) + totalWeight += r.Weights[i] } - if len(upstreams) == 0 { + if totalWeight == 0 { return nil } - return upstreams[index%len(upstreams)] + + currentWeight := uint64(r.index.Add(1)) % uint64(totalWeight) + for _, upstream := range upstreams { + if currentWeight < uint64(upstream.weight) { + return upstream.upstream + } + currentWeight -= uint64(upstream.weight) + } + return nil } // RandomChoiceSelection is a policy that selects diff --git a/modules/caddyhttp/reverseproxy/selectionpolicies_test.go b/modules/caddyhttp/reverseproxy/selectionpolicies_test.go index 6813ec036..77aba57c9 100644 --- a/modules/caddyhttp/reverseproxy/selectionpolicies_test.go +++ b/modules/caddyhttp/reverseproxy/selectionpolicies_test.go @@ -126,8 +126,8 @@ func TestWeightedRoundRobinPolicy(t *testing.T) { pool[1].countRequest(1) pool[1].MaxRequests = 1 h = wrrPolicy.Select(pool, req, nil) - if h != pool[2] { - t.Error("Expected to skip full host.") + if h != pool[0] { + t.Error("Expected to use the weights of the available hosts.") } } @@ -1008,3 +1008,16 @@ func TestCookieHashPolicyWithSecret(t *testing.T) { t.Error("Expected a new cookie to be set for a non-matching value") } } + +func TestWeightedRoundRobinPolicyAvailableWeights(t *testing.T) { + pool := testPool() + pool[1].setHealthy(false) + policy := WeightedRoundRobinSelection{Weights: []int{1, 8, 3}, totalWeight: 12} + counts := make(map[*Upstream]int) + for i := 0; i < 12; i++ { + counts[policy.Select(pool, nil, nil)]++ + } + if counts[pool[0]] != 3 || counts[pool[2]] != 9 || len(counts) != 2 { + t.Fatalf("Expected a 1:3 distribution among available hosts, got %v", counts) + } +}