Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 03:30 UTC
reverseproxy: Don't panic in weighted_round_robin with fewer weights than upstreams (#8159)
Jay Hemnani committed
Oct 7, 2026 at 10:06 UTC
8ae69fae1e16ac8037762a6e017ed842048f6e51
2 files changed
+18
-1
modules/caddyhttp/reverseproxy/selectionpolicies.go
+1
-1
@@ -167,7 +167,7 @@ func (r *WeightedRoundRobinSelection) Select(pool UpstreamPool, _ *http.Request,
167
168
upstreams := make([]*Upstream, 0, len(weights))
169
for i, upstream := range pool {
170
- if !upstream.Available() || r.Weights[i] == 0 {
170
+ if i >= len(r.Weights) || !upstream.Available() || r.Weights[i] == 0 {
171
continue
172
}
173
upstreams = append(upstreams, upstream)
modules/caddyhttp/reverseproxy/selectionpolicies_test.go
+17
@@ -131,6 +131,23 @@ func TestWeightedRoundRobinPolicy(t *testing.T) {
131
}
132
}
133
134
+func TestWeightedRoundRobinPolicyFewerWeightsThanUpstreams(t *testing.T) {
135
+ pool := testPool()
136
+ wrrPolicy := WeightedRoundRobinSelection{
137
+ Weights: []int{1, 1},
138
+ totalWeight: 2,
139
+ }
140
+ req, _ := http.NewRequest("GET", "/", nil)
141
+
142
+ pool[0].setHealthy(false)
143
+ for i := 0; i < 4; i++ {
144
+ h := wrrPolicy.Select(pool, req, nil)
145
+ if h != pool[1] {
146
+ t.Fatalf("Selection %d: got %v, want the only available weighted host %v", i+1, h, pool[1])
147
+ }
148
+ }
149
+}
150
+
151
func TestWeightedRoundRobinSelection_Validate(t *testing.T) {
152
tests := []struct {
153
name string