add comments

This commit is contained in:
iamqizhao 2016-05-12 18:52:24 -07:00
parent f93b6bbfb5
commit 32eec1acef
2 changed files with 13 additions and 12 deletions

View File

@ -7,8 +7,9 @@ import (
"google.golang.org/grpc/transport" "google.golang.org/grpc/transport"
) )
// Address represents a server the client connects to.
type Address struct { type Address struct {
// Addr is the peer address on which a connection will be established. // Addr is the server address on which a connection will be established.
Addr string Addr string
// Metadata is the information associated with Addr, which may be used // Metadata is the information associated with Addr, which may be used
// to make load balancing decision. This is from the metadata attached // to make load balancing decision. This is from the metadata attached
@ -32,18 +33,20 @@ type Balancer interface {
Close() error Close() error
} }
// RoundRobin returns a Balancer that selects addresses round-robin.
func RoundRobin() Balancer { func RoundRobin() Balancer {
return &roundRobin{} return &roundRobin{}
} }
type roundRobin struct { type roundRobin struct {
mu sync.Mutex mu sync.Mutex
addrs []Address addrs []Address
next int next int // index of the next address to return for Get()
waitCh chan struct{} waitCh chan struct{}
pending int
} }
// Up appends addr to the end of rr.addrs and sends notification if there
// are pending Get() calls.
func (rr *roundRobin) Up(addr Address) func(error) { func (rr *roundRobin) Up(addr Address) func(error) {
rr.mu.Lock() rr.mu.Lock()
defer rr.mu.Unlock() defer rr.mu.Unlock()
@ -64,6 +67,7 @@ func (rr *roundRobin) Up(addr Address) func(error) {
} }
} }
// down removes addr from rr.addrs and moves the remaining addrs forward.
func (rr *roundRobin) down(addr Address, err error) { func (rr *roundRobin) down(addr Address, err error) {
rr.mu.Lock() rr.mu.Lock()
defer rr.mu.Unlock() defer rr.mu.Unlock()
@ -76,6 +80,7 @@ func (rr *roundRobin) down(addr Address, err error) {
} }
} }
// Get returns the next addr in the rotation. It blocks if there is no address available.
func (rr *roundRobin) Get(ctx context.Context) (addr Address, put func(), err error) { func (rr *roundRobin) Get(ctx context.Context) (addr Address, put func(), err error) {
var ch chan struct{} var ch chan struct{}
rr.mu.Lock() rr.mu.Lock()
@ -85,13 +90,13 @@ func (rr *roundRobin) Get(ctx context.Context) (addr Address, put func(), err er
if len(rr.addrs) > 0 { if len(rr.addrs) > 0 {
addr = rr.addrs[rr.next] addr = rr.addrs[rr.next]
rr.next++ rr.next++
rr.pending++
rr.mu.Unlock() rr.mu.Unlock()
put = func() { put = func() {
rr.put(ctx, addr) rr.put(ctx, addr)
} }
return return
} }
// There is no address available. Wait on rr.waitCh.
if rr.waitCh == nil { if rr.waitCh == nil {
ch = make(chan struct{}) ch = make(chan struct{})
rr.waitCh = ch rr.waitCh = ch
@ -116,7 +121,6 @@ func (rr *roundRobin) Get(ctx context.Context) (addr Address, put func(), err er
} }
addr = rr.addrs[rr.next] addr = rr.addrs[rr.next]
rr.next++ rr.next++
rr.pending++
rr.mu.Unlock() rr.mu.Unlock()
put = func() { put = func() {
rr.put(ctx, addr) rr.put(ctx, addr)
@ -127,9 +131,6 @@ func (rr *roundRobin) Get(ctx context.Context) (addr Address, put func(), err er
} }
func (rr *roundRobin) put(ctx context.Context, addr Address) { func (rr *roundRobin) put(ctx context.Context, addr Address) {
rr.mu.Lock()
defer rr.mu.Unlock()
rr.pending--
} }
func (rr *roundRobin) Close() error { func (rr *roundRobin) Close() error {

View File

@ -618,7 +618,7 @@ func (ac *addrConn) resetTransport(closeTransport bool) error {
closeTransport = false closeTransport = false
select { select {
case <-time.After(sleepTime): case <-time.After(sleepTime):
case <-cc.shutdownChan: case <-ac.shutdownChan:
} }
retries++ retries++
grpclog.Printf("grpc: addrConn.resetTransport failed to create client transport: %v; Reconnecting to %q", err, ac.addr) grpclog.Printf("grpc: addrConn.resetTransport failed to create client transport: %v; Reconnecting to %q", err, ac.addr)