Skip to content

Commit

Permalink
Merge pull request #839 from blight19/master
Browse files Browse the repository at this point in the history
reslove WeightedICMPSelector
  • Loading branch information
smallnest authored Jan 11, 2024
2 parents 7ef7294 + 81531df commit 5bdb742
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 55 deletions.
28 changes: 17 additions & 11 deletions client/ping_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,32 @@ import (
ping "github.com/go-ping/ping"
)

// weightedICMPSelector selects servers with ping result.
type weightedICMPSelector struct {
servers []*Weighted
wrs *weightedRoundRobinSelector
}

func newWeightedICMPSelector(servers map[string]string) Selector {
ss := createICMPWeighted(servers)
return &weightedICMPSelector{servers: ss}
wicmps := weightedICMPSelector{
servers: ss,
wrs: &weightedRoundRobinSelector{servers: ss},
}
wicmps.wrs.servers = ss
wicmps.wrs.buildRing()
return &wicmps
}

func (s weightedICMPSelector) Select(ctx context.Context, servicePath, serviceMethod string, args interface{}) string {
ss := s.servers
if len(ss) == 0 {
return ""
}
w := nextWeighted(ss)
if w == nil {
return ""
}
return w.Server
func (s *weightedICMPSelector) Select(ctx context.Context, servicePath, serviceMethod string, args interface{}) string {
return s.wrs.Select(ctx, servicePath, serviceMethod, args)
}

func (s *weightedICMPSelector) UpdateServer(servers map[string]string) {
ss := createICMPWeighted(servers)
s.wrs.servers = ss
s.servers = ss
s.wrs.buildRing()
}

func createICMPWeighted(servers map[string]string) []*Weighted {
Expand Down
5 changes: 0 additions & 5 deletions client/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,3 @@ func (s *consistentHashSelector) UpdateServer(servers map[string]string) {
}
s.servers = ss
}

// weightedICMPSelector selects servers with ping result.
type weightedICMPSelector struct {
servers []*Weighted
}
44 changes: 44 additions & 0 deletions client/selector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,47 @@ func BenchmarkWeightedRoundRobinSelector_Select(b *testing.B) {
weightSelector.Select(ctx, "", "", nil)
}
}

//
//func TestWeightedICMPSelector(t *testing.T) {
// calc := make(map[string]int)
// servers := make(map[string]string)
// servers["@localhost:3333"] = ""
// servers["@www.baidu.com:3334"] = ""
// servers["@xxxx.xxxx:333"] = ""
// s := newWeightedICMPSelector(servers)
// ctx := context.Background()
// for i := 0; i < 10; i++ {
// host := s.Select(ctx, "", "", nil)
// if _, ok := calc[host]; ok {
// calc[host]++
// } else {
// calc[host] = 0
// }
// }
// if len(calc) != 2 {
// t.Errorf("expected %d but got %d", 2, len(servers))
// }
//}
//func TestWeightedICMPSelector_UpdateServer(t *testing.T) {
// calc := make(map[string]int)
// servers := make(map[string]string)
// servers["@localhost:3333"] = ""
// servers["@www.baidu.com:3334"] = ""
// servers["@xxxx.xxxx:333"] = ""
// s := newWeightedICMPSelector(servers)
// ctx := context.Background()
// servers["@www.sina.com:3333"] = ""
// s.UpdateServer(servers)
// for i := 0; i < 10; i++ {
// host := s.Select(ctx, "", "", nil)
// if _, ok := calc[host]; ok {
// calc[host]++
// } else {
// calc[host] = 0
// }
// }
// if len(calc) != 3 {
// t.Errorf("expected %d but got %d", 3, len(servers))
// }
//}
39 changes: 0 additions & 39 deletions client/smooth_weighted_round_robin.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,3 @@ type Weighted struct {
CurrentWeight int
EffectiveWeight int
}

// func (w *Weighted) fail() {
// w.EffectiveWeight -= w.Weight
// if w.EffectiveWeight < 0 {
// w.EffectiveWeight = 0
// }
// }

// https://github.com/phusion/nginx/commit/27e94984486058d73157038f7950a0a36ecc6e35
func nextWeighted(servers []*Weighted) (best *Weighted) {
total := 0

for i := 0; i < len(servers); i++ {
w := servers[i]

if w == nil {
continue
}
// if w is down, continue

w.CurrentWeight += w.EffectiveWeight
total += w.EffectiveWeight
if w.EffectiveWeight < w.Weight {
w.EffectiveWeight++
}

if best == nil || w.CurrentWeight > best.CurrentWeight {
best = w
}

}

if best == nil {
return nil
}

best.CurrentWeight -= total
return best
}

0 comments on commit 5bdb742

Please sign in to comment.