/
client.go
61 lines (54 loc) · 1.73 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package grpcclient
import (
"fmt"
"sync"
"time"
"github.com/33cn/chain33/types"
"google.golang.org/grpc"
"google.golang.org/grpc/keepalive"
)
// paraChainGrpcRecSize 平行链receive最大100M
const paraChainGrpcRecSize = 100 * 1024 * 1024
var mu sync.Mutex
var defaultClient types.Chain33Client
//NewMainChainClient 创建一个平行链的 主链 grpc chain33 客户端
func NewMainChainClient(cfg *types.Chain33Config, grpcaddr string) (types.Chain33Client, error) {
mu.Lock()
defer mu.Unlock()
if grpcaddr == "" && defaultClient != nil {
return defaultClient, nil
}
paraRemoteGrpcClient := types.Conf(cfg, "config.consensus.sub.para").GStr("ParaRemoteGrpcClient")
if grpcaddr != "" {
paraRemoteGrpcClient = grpcaddr
}
if paraRemoteGrpcClient == "" {
paraRemoteGrpcClient = "127.0.0.1:8802"
}
kp := keepalive.ClientParameters{
Time: time.Second * 5,
Timeout: time.Second * 20,
PermitWithoutStream: true,
}
var conn *grpc.ClientConn
var err error
useLBSync := types.Conf(cfg, "config.consensus.sub.para").IsEnable("useGrpcLBSync")
if useLBSync {
conn, err = grpc.Dial(NewSyncURL(paraRemoteGrpcClient), grpc.WithInsecure(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(paraChainGrpcRecSize)),
grpc.WithDefaultServiceConfig(fmt.Sprintf(`{"LoadBalancingPolicy": "%s"}`, SyncLbName)),
grpc.WithKeepaliveParams(kp))
} else {
conn, err = grpc.Dial(NewMultipleURL(paraRemoteGrpcClient), grpc.WithInsecure(),
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(paraChainGrpcRecSize)),
grpc.WithKeepaliveParams(kp))
}
if err != nil {
return nil, err
}
grpcClient := types.NewChain33Client(conn)
if grpcaddr == "" {
defaultClient = grpcClient
}
return grpcClient, nil
}