Skip to content

Commit

Permalink
fix deadlock of roundrobin balancer
Browse files Browse the repository at this point in the history
  • Loading branch information
nghialv committed Jul 6, 2017
1 parent 41d9b6e commit 309e2d8
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,15 @@ type addrInfo struct {
}

type roundRobin struct {
r naming.Resolver
w naming.Watcher
addrs []*addrInfo // all the addresses the client should potentially connect
mu sync.Mutex
addrCh chan []Address // the channel to notify gRPC internals the list of addresses the client should connect to.
next int // index of the next address to return for Get()
waitCh chan struct{} // the channel to block when there is no connected address available
done bool // The Balancer is closed.
r naming.Resolver
w naming.Watcher
addrs []*addrInfo // all the addresses the client should potentially connect
mu sync.Mutex
addrChMu sync.Mutex // protects addrCh
addrCh chan []Address // the channel to notify gRPC internals the list of addresses the client should connect to.
next int // index of the next address to return for Get()
waitCh chan struct{} // the channel to block when there is no connected address available
done bool // The Balancer is closed.
}

func (rr *roundRobin) watchAddrUpdates() error {
Expand All @@ -160,8 +161,10 @@ func (rr *roundRobin) watchAddrUpdates() error {
grpclog.Printf("grpc: the naming watcher stops working due to %v.\n", err)
return err
}
rr.addrChMu.Lock()
defer rr.addrChMu.Unlock()
rr.mu.Lock()
defer rr.mu.Unlock()

for _, update := range updates {
addr := Address{
Addr: update.Addr,
Expand Down Expand Up @@ -199,9 +202,15 @@ func (rr *roundRobin) watchAddrUpdates() error {
open[i] = v.addr
}
if rr.done {
rr.mu.Unlock()
return ErrClientConnClosing
}
rr.addrCh <- open
done := rr.done
rr.mu.Unlock()

if !done {
rr.addrCh <- open
}
return nil
}

Expand Down Expand Up @@ -373,8 +382,11 @@ func (rr *roundRobin) Notify() <-chan []Address {
}

func (rr *roundRobin) Close() error {
rr.addrChMu.Lock()
defer rr.addrChMu.Unlock()
rr.mu.Lock()
defer rr.mu.Unlock()

if rr.done {
return errBalancerClosed
}
Expand Down

0 comments on commit 309e2d8

Please sign in to comment.