package internal import ( "context" "crypto/tls" "time" pb "schain/proto" "google.golang.org/grpc" "google.golang.org/grpc/credentials" "google.golang.org/grpc/keepalive" ) const ( dialTimeout = 10 * time.Second maxRecvMessageSize = 100 * 1024 * 1024 // 100 MiB maxSendMessageSize = 100 * 1024 * 1024 // 100 MiB ) // 创建一个grpc连接并返回该连接 func NewClientConn(address string, tlsConf *tls.Config) (*grpc.ClientConn, error) { //Keepalive 时间、超时时间和允许无流的连接 kaOpts := keepalive.ClientParameters{ Time: 1 * time.Minute, Timeout: 20 * time.Second, PermitWithoutStream: true, } //gRPC 连接选项 dialOpts := []grpc.DialOption{ grpc.WithKeepaliveParams(kaOpts), grpc.WithBlock(), grpc.FailOnNonTempDialError(true), grpc.WithDefaultCallOptions( grpc.MaxCallRecvMsgSize(maxRecvMessageSize), grpc.MaxCallSendMsgSize(maxSendMessageSize), ), } if tlsConf != nil { creds := credentials.NewTLS(tlsConf) dialOpts = append(dialOpts, grpc.WithTransportCredentials(creds)) } else { dialOpts = append(dialOpts, grpc.WithInsecure()) } ctx, cancel := context.WithTimeout(context.Background(), dialTimeout) defer cancel() return grpc.DialContext(ctx, address, dialOpts...) } func NewRegisterClient(conn *grpc.ClientConn) (pb.ChaincodeSupport_RegisterClient, error) { return pb.NewChaincodeSupportClient(conn).Register(context.Background()) }