Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Yuri Shkuro <github@ysh.us>
  • Loading branch information
yurishkuro committed Jan 14, 2021
1 parent 5ddf595 commit b424d36
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
27 changes: 15 additions & 12 deletions pkg/discovery/grpcresolver/grpc_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,22 +123,25 @@ func (r *Resolver) Close() {
r.closing.Wait()
}

// rendezvousHash is the core of the algorithm. It takes input addresses,
// assigns each of them a hash, sorts them by those hash values, and
// returns top N of entries from the sorted list, up to minPeers parameter.
func (r *Resolver) rendezvousHash(addresses []string) []string {
hasher := fnv.New32()
hosts := hostScores{}
for _, address := range addresses {
hosts = append(hosts, hostScore{
hosts := make(hostScores, len(addresses))
for i, address := range addresses {
hosts[i] = hostScore{
address: address,
score: hashAddr(hasher, []byte(address), r.salt),
})
}
}
sort.Sort(hosts)
size := min(r.discoveryMinPeers, len(hosts))
topAddrs := make([]string, size)
for i := 0; i < size; i++ {
topAddrs[i] = hosts[i].address
n := min(r.discoveryMinPeers, len(hosts))
topN := make([]string, n)
for i := 0; i < n; i++ {
topN[i] = hosts[i].address
}
return topAddrs
return topN
}

func min(a, b int) int {
Expand All @@ -162,9 +165,9 @@ func (r *Resolver) updateAddresses(hostPorts []string) {
}

func generateAddresses(instances []string) []resolver.Address {
var addrs []resolver.Address
for _, instance := range instances {
addrs = append(addrs, resolver.Address{Addr: instance})
addrs := make([]resolver.Address, len(instances))
for i, instance := range instances {
addrs[i] = resolver.Address{Addr: instance}
}
return addrs
}
7 changes: 5 additions & 2 deletions pkg/discovery/grpcresolver/grpc_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ func TestGRPCResolverRoundRobin(t *testing.T) {
minPeers int
connections int // expected number of unique connections to servers
}{
{3, 3}, {5, 5}, {7, 5},
{minPeers: 3, connections: 3},
{minPeers: 5, connections: 3},
// note: test cannot succeed with minPeers < connections because resolver
// will never return more than minPeers addresses.
}
for _, test := range tests {
t.Run(fmt.Sprintf("minPeers=%d", test.minPeers), func(t *testing.T) {
t.Run(fmt.Sprintf("%+v", test), func(t *testing.T) {
res := New(notifier, discoverer, zap.NewNop(), test.minPeers)
defer resolver.UnregisterForTesting(res.Scheme())

Expand Down

0 comments on commit b424d36

Please sign in to comment.