Skip to content

Commit

Permalink
Implemented ignoring backends with negative weight/priority #204
Browse files Browse the repository at this point in the history
  • Loading branch information
illarion committed Jun 21, 2019
1 parent ec761f8 commit b0f8c7e
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/balance/weight.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@ package balance

import (
"errors"
"fmt"
"math/rand"

"github.com/yyyar/gobetween/core"
"github.com/yyyar/gobetween/logging"
)

/**
* Weight balancer
*/
type WeightBalancer struct{}

var log = logging.For("balance/weight")

/**
* Elect backend based on weight with priority strategy.
* See https://tools.ietf.org/html/rfc2782, Priority and Weight sections
Expand All @@ -29,11 +31,6 @@ func (b *WeightBalancer) Elect(context core.Context, backends []*core.Backend) (
return nil, errors.New("Can't elect backend, Backends empty")
}

// nothing to apply algorithm to - just one backend, simply return
if len(backends) == 1 {
return backends[0], nil
}

// according to RFC we should use backends with lowest priority
minPriority := backends[0].Priority
// group of backends with priority == minPriority
Expand All @@ -49,14 +46,16 @@ func (b *WeightBalancer) Elect(context core.Context, backends []*core.Backend) (
}

if backend.Priority < 0 {
return nil, fmt.Errorf("Invalid backend priority, shold not be less than 0: %v", backend.Priority)
log.Warn("Ignoring invalid backend priority %v, should not be less than 0", backend.Priority)
continue
}

if backend.Weight < 0 {
return nil, fmt.Errorf("Invalid backend weight, should not be less 0: %v", backend.Weight)
log.Warn("Ignoring invalid backend weight %v, should not be less than 0", backend.Weight)
continue
}

// got new lower priority, reset
// got new lower (accroding to RFC, lower values are preferred) priority, reset
if backend.Priority < minPriority {
minPriority = backend.Priority
group = make([]*core.Backend, 0, len(backends))
Expand Down

0 comments on commit b0f8c7e

Please sign in to comment.