github.com/turingchain2020/turingchain@v1.1.21/rpc/grpcclient/client.go (about)

     1  package grpcclient
     2  
     3  import (
     4  	"sync"
     5  	"time"
     6  
     7  	"github.com/turingchain2020/turingchain/types"
     8  	"google.golang.org/grpc"
     9  	"google.golang.org/grpc/keepalive"
    10  )
    11  
    12  // paraChainGrpcRecSize 平行链receive最大100M
    13  const paraChainGrpcRecSize = 100 * 1024 * 1024
    14  
    15  var mu sync.Mutex
    16  
    17  var defaultClient types.TuringchainClient
    18  
    19  //NewMainChainClient 创建一个平行链的 主链 grpc turingchain 客户端
    20  func NewMainChainClient(cfg *types.TuringchainConfig, grpcaddr string) (types.TuringchainClient, error) {
    21  	mu.Lock()
    22  	defer mu.Unlock()
    23  	if grpcaddr == "" && defaultClient != nil {
    24  		return defaultClient, nil
    25  	}
    26  	paraRemoteGrpcClient := types.Conf(cfg, "config.consensus.sub.para").GStr("ParaRemoteGrpcClient")
    27  	if grpcaddr != "" {
    28  		paraRemoteGrpcClient = grpcaddr
    29  	}
    30  	if paraRemoteGrpcClient == "" {
    31  		paraRemoteGrpcClient = "127.0.0.1:9672"
    32  	}
    33  	kp := keepalive.ClientParameters{
    34  		Time:                time.Second * 5,
    35  		Timeout:             time.Second * 20,
    36  		PermitWithoutStream: true,
    37  	}
    38  	conn, err := grpc.Dial(NewMultipleURL(paraRemoteGrpcClient), grpc.WithInsecure(),
    39  		grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(paraChainGrpcRecSize)),
    40  		grpc.WithKeepaliveParams(kp))
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  	grpcClient := types.NewTuringchainClient(conn)
    45  	if grpcaddr == "" {
    46  		defaultClient = grpcClient
    47  	}
    48  	return grpcClient, nil
    49  }