mirror of
https://github.com/caddyserver/caddy.git
synced 2026-11-19 22:39:38 +00:00
reverseproxy: preserve weights among available upstreams (#8380)
Some checks are pending
Tests / test (./cmd/caddy/caddy, ~1.27.0, macos-latest, 0, 1.27, mac) (push) Waiting to run
Tests / test (./cmd/caddy/caddy, ~1.27.0, ubuntu-latest, 0, 1.27, linux) (push) Waiting to run
Tests / test (./cmd/caddy/caddy.exe, ~1.27.0, windows-latest, True, 1.27, windows) (push) Waiting to run
Tests / test (s390x on IBM Z) (push) Waiting to run
Tests / goreleaser-check (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, aix) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, darwin) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, dragonfly) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, freebsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, illumos) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, linux) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, netbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, openbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, solaris) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, windows) (push) Waiting to run
Lint / lint (push) Waiting to run
Lint / lint-1 (push) Waiting to run
Lint / lint-2 (push) Waiting to run
Lint / govulncheck (push) Waiting to run
Lint / dependency-review (push) Waiting to run
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Some checks are pending
Tests / test (./cmd/caddy/caddy, ~1.27.0, macos-latest, 0, 1.27, mac) (push) Waiting to run
Tests / test (./cmd/caddy/caddy, ~1.27.0, ubuntu-latest, 0, 1.27, linux) (push) Waiting to run
Tests / test (./cmd/caddy/caddy.exe, ~1.27.0, windows-latest, True, 1.27, windows) (push) Waiting to run
Tests / test (s390x on IBM Z) (push) Waiting to run
Tests / goreleaser-check (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, aix) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, darwin) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, dragonfly) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, freebsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, illumos) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, linux) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, netbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, openbsd) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, solaris) (push) Waiting to run
Cross-Build / build (~1.26.0, 1.26, windows) (push) Waiting to run
Lint / lint (push) Waiting to run
Lint / lint-1 (push) Waiting to run
Lint / lint-2 (push) Waiting to run
Lint / govulncheck (push) Waiting to run
Lint / dependency-review (push) Waiting to run
OpenSSF Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
This commit is contained in:
parent
daa74db1d1
commit
e3271947ee
2 changed files with 33 additions and 35 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue