mirror of
https://github.com/caddyserver/caddy.git
synced 2025-04-23 13:14:08 +08:00
reverseproxy: allow 0 as weighted round robin value
This commit is contained in:
parent
7a77bcf144
commit
1edb1b68c1
@ -136,8 +136,15 @@ func (r *WeightedRoundRobinSelection) Select(pool UpstreamPool, _ *http.Request,
|
||||
return pool[0]
|
||||
}
|
||||
var index, totalWeight int
|
||||
var weights []int
|
||||
|
||||
for _, w := range r.Weights {
|
||||
if w > 0 {
|
||||
weights = append(weights, w)
|
||||
}
|
||||
}
|
||||
currentWeight := int(atomic.AddUint32(&r.index, 1)) % r.totalWeight
|
||||
for i, weight := range r.Weights {
|
||||
for i, weight := range weights {
|
||||
totalWeight += weight
|
||||
if currentWeight < totalWeight {
|
||||
index = i
|
||||
@ -145,9 +152,9 @@ func (r *WeightedRoundRobinSelection) Select(pool UpstreamPool, _ *http.Request,
|
||||
}
|
||||
}
|
||||
|
||||
upstreams := make([]*Upstream, 0, len(r.Weights))
|
||||
for _, upstream := range pool {
|
||||
if !upstream.Available() {
|
||||
upstreams := make([]*Upstream, 0, len(weights))
|
||||
for i, upstream := range pool {
|
||||
if !upstream.Available() || r.Weights[i] == 0 {
|
||||
continue
|
||||
}
|
||||
upstreams = append(upstreams, upstream)
|
||||
|
@ -131,6 +131,50 @@ func TestWeightedRoundRobinPolicy(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestWeightedRoundRobinPolicyWithZeroWeight(t *testing.T) {
|
||||
pool := testPool()
|
||||
wrrPolicy := WeightedRoundRobinSelection{
|
||||
Weights: []int{0, 2, 1},
|
||||
totalWeight: 3,
|
||||
}
|
||||
req, _ := http.NewRequest("GET", "/", nil)
|
||||
|
||||
h := wrrPolicy.Select(pool, req, nil)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected first weighted round robin host to be second host in the pool.")
|
||||
}
|
||||
|
||||
h = wrrPolicy.Select(pool, req, nil)
|
||||
if h != pool[2] {
|
||||
t.Error("Expected second weighted round robin host to be third host in the pool.")
|
||||
}
|
||||
|
||||
h = wrrPolicy.Select(pool, req, nil)
|
||||
if h != pool[1] {
|
||||
t.Error("Expected third weighted round robin host to be second host in the pool.")
|
||||
}
|
||||
|
||||
// mark second host as down
|
||||
pool[1].setHealthy(false)
|
||||
h = wrrPolicy.Select(pool, req, nil)
|
||||
if h != pool[2] {
|
||||
t.Error("Expect select next available host.")
|
||||
}
|
||||
|
||||
h = wrrPolicy.Select(pool, req, nil)
|
||||
if h != pool[2] {
|
||||
t.Error("Expect select only available host.")
|
||||
}
|
||||
|
||||
pool[1].setHealthy(true)
|
||||
|
||||
h = wrrPolicy.Select(pool, req, nil)
|
||||
if h != pool[1] {
|
||||
t.Error("Expect select first host on availability.")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestLeastConnPolicy(t *testing.T) {
|
||||
pool := testPool()
|
||||
lcPolicy := LeastConnSelection{}
|
||||
|
Loading…
x
Reference in New Issue
Block a user