优化RPC调用

This commit is contained in:
GoEdgeLab
2020-11-15 11:58:08 +08:00
parent 332632ab9f
commit 22ce8bcef5
2 changed files with 19 additions and 8 deletions

View File

@@ -53,6 +53,7 @@ func (this *IPListManager) Start() {
events.On(events.EventQuit, func() { events.On(events.EventQuit, func() {
ticker.Stop() ticker.Stop()
}) })
countErrors := 0
for { for {
select { select {
case <-ticker.C: case <-ticker.C:
@@ -60,15 +61,20 @@ func (this *IPListManager) Start() {
} }
err := this.loop() err := this.loop()
if err != nil { if err != nil {
countErrors++
logs.Println("IP_LIST_MANAGER", err.Error()) logs.Println("IP_LIST_MANAGER", err.Error())
// 方便立即重试 // 连续错误小于3次的我们立即重试
if countErrors <= 3 {
select { select {
case IPListUpdateNotify <- true: case IPListUpdateNotify <- true:
default: default:
} }
} }
} else {
countErrors = 0
}
} }
} }

View File

@@ -181,11 +181,16 @@ func (this *RPCClient) pickConn() *grpc.ClientConn {
// 检查连接状态 // 检查连接状态
if len(this.conns) > 0 { if len(this.conns) > 0 {
availableConns := []*grpc.ClientConn{} availableConns := []*grpc.ClientConn{}
for _, state := range []connectivity.State{connectivity.Ready, connectivity.Idle, connectivity.Connecting} {
for _, conn := range this.conns { for _, conn := range this.conns {
if conn.GetState() == connectivity.Ready { if conn.GetState() == state {
availableConns = append(availableConns, conn) availableConns = append(availableConns, conn)
} }
} }
if len(availableConns) > 0 {
break
}
}
if len(availableConns) > 0 { if len(availableConns) > 0 {
return availableConns[rands.Int(0, len(availableConns)-1)] return availableConns[rands.Int(0, len(availableConns)-1)]