Skip to content

Commit

Permalink
fix: can't connect rpc at first time
Browse files Browse the repository at this point in the history
  • Loading branch information
ixre committed Dec 16, 2020
1 parent 35c0cd0 commit 74a2f57
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions core/service/rpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package service

import (
"context"
"github.com/ixre/gof/log"
"go.etcd.io/etcd/clientv3"
"go2o/core/etcd"
Expand All @@ -18,12 +19,10 @@ import (
"time"
)

//var cfg clientv3.Config
var selector etcd.Selector

// 设置RPC地址
func ConfigureClient(c clientv3.Config) {
//cfg = c
log.Println("[ Go2o][ RPC]: connecting go2o rpc server...")
s, err := etcd.NewSelector(service, c, etcd.AlgRoundRobin)
if err != nil {
Expand All @@ -32,19 +31,15 @@ func ConfigureClient(c clientv3.Config) {
}
selector = s
tryConnect(10)
log.Println("[ Go2o][ API]: try times limit")
}

// 尝试连接服务,如果连接不成功,则退出
func tryConnect(retryTimes int) {
for i := 0; i < retryTimes; i++ {
log.Println("try ",i)
trans, _, err := StatusServiceClient()
if err == nil {
trans.Close()
break
}else{
log.Println(err.Error())
}
time.Sleep(time.Second)
if i >= retryTimes-1 {
Expand All @@ -60,8 +55,10 @@ func getConn(selector etcd.Selector) (*grpc.ClientConn, error) {
if err != nil {
return nil, err
}
log.Println("addr:",next.Addr)
return grpc.Dial(next.Addr, grpc.WithInsecure(), grpc.WithBlock())
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
conn, err := grpc.DialContext(ctx, next.Addr, grpc.WithInsecure(), grpc.WithBlock())
cancel()
return conn, err
}

// 状态客户端
Expand Down

0 comments on commit 74a2f57

Please sign in to comment.