pick a random address if the current in use is deleted by resolver (#1135)

This commit is contained in:
Menghan Li
2017-03-28 11:13:46 -07:00
committed by GitHub
parent ccdf270424
commit b3cc2b5eca

View File

@ -39,6 +39,7 @@ package grpclb
import ( import (
"errors" "errors"
"fmt" "fmt"
"math/rand"
"sync" "sync"
"time" "time"
@ -105,6 +106,7 @@ type balancer struct {
waitCh chan struct{} waitCh chan struct{}
done bool done bool
expTimer *time.Timer expTimer *time.Timer
rand *rand.Rand
} }
func (b *balancer) watchAddrUpdates(w naming.Watcher, ch chan remoteBalancerInfo) error { func (b *balancer) watchAddrUpdates(w naming.Watcher, ch chan remoteBalancerInfo) error {
@ -176,6 +178,11 @@ func (b *balancer) watchAddrUpdates(w naming.Watcher, ch chan remoteBalancerInfo
case <-ch: case <-ch:
default: default:
} }
// Pick a random one from the list, instead of always using the first one.
if l := len(b.rbs); l > 1 {
tmpIdx := b.rand.Intn(l - 1)
b.rbs[0], b.rbs[tmpIdx] = b.rbs[tmpIdx], b.rbs[0]
}
ch <- b.rbs[0] ch <- b.rbs[0]
} }
} }
@ -310,6 +317,7 @@ func (b *balancer) callRemoteBalancer(lbc lbpb.LoadBalancerClient, seq int) (ret
} }
func (b *balancer) Start(target string, config grpc.BalancerConfig) error { func (b *balancer) Start(target string, config grpc.BalancerConfig) error {
b.rand = rand.New(rand.NewSource(time.Now().Unix()))
// TODO: Fall back to the basic direct connection if there is no name resolver. // TODO: Fall back to the basic direct connection if there is no name resolver.
if b.r == nil { if b.r == nil {
return errors.New("there is no name resolver installed") return errors.New("there is no name resolver installed")