agent/grpc: seed the rand for shuffling servers

This commit is contained in:
Daniel Nephin 2020-09-15 13:51:25 -04:00
parent 2294793357
commit e6ffd987a3
1 changed files with 3 additions and 2 deletions

View File

@ -5,6 +5,7 @@ import (
"math/rand" "math/rand"
"strings" "strings"
"sync" "sync"
"time"
"github.com/hashicorp/consul/agent/metadata" "github.com/hashicorp/consul/agent/metadata"
"google.golang.org/grpc/resolver" "google.golang.org/grpc/resolver"
@ -59,6 +60,7 @@ func NewServerResolverBuilder(cfg Config) *ServerResolverBuilder {
// Rebalance shuffles the server list for resolvers in all datacenters. // Rebalance shuffles the server list for resolvers in all datacenters.
func (s *ServerResolverBuilder) NewRebalancer(dc string) func() { func (s *ServerResolverBuilder) NewRebalancer(dc string) func() {
shuffler := rand.New(rand.NewSource(time.Now().UnixNano()))
return func() { return func() {
s.lock.RLock() s.lock.RLock()
defer s.lock.RUnlock() defer s.lock.RUnlock()
@ -70,8 +72,7 @@ func (s *ServerResolverBuilder) NewRebalancer(dc string) func() {
// Shuffle the list of addresses using the last list given to the resolver. // Shuffle the list of addresses using the last list given to the resolver.
resolver.addrLock.Lock() resolver.addrLock.Lock()
addrs := resolver.addrs addrs := resolver.addrs
// TODO: seed this rand, so it is a little more random-like shuffler.Shuffle(len(addrs), func(i, j int) {
rand.Shuffle(len(addrs), func(i, j int) {
addrs[i], addrs[j] = addrs[j], addrs[i] addrs[i], addrs[j] = addrs[j], addrs[i]
}) })
// Pass the shuffled list to the resolver. // Pass the shuffled list to the resolver.