1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- // Copyright 2019 getensh.com. All rights reserved.
- // Use of this source code is governed by getensh.com.
- package pb
- import (
- "cp-system-management/parser"
- fmt "fmt"
- "time"
- grpc "google.golang.org/grpc"
- "google.golang.org/grpc/keepalive"
- )
- // 客户端集合
- var Device DeviceClient
- var Organization OrganizationClient
- func setupDeviceClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.Device.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.Device.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.Device.ServicePort),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- Device = NewDeviceClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial device conn err", err)
- }
- return
- }
- func setupOrganizationClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
- // 根据是否为k8s来组装targets
- var serviceName string
- if parser.Conf.K8s {
- serviceName = parser.Conf.Rpc.Organization.ServiceName
- } else {
- serviceName = parser.Conf.Rpc.Organization.ServiceIp
- }
- // 发起一个连接并记录连接conn,后期释放
- if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
- parser.Conf.Rpc.Organization.ServicePort),
- grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
- Organization = NewOrganizationClient(conn)
- conns = append(conns, conn)
- } else {
- fmt.Println("[rpc] dial organization conn err", err)
- }
- return
- }
- // SetupClients 创建客户端
- func SetupClients() (conns []*grpc.ClientConn) {
- // 客户端配置参数
- var kacp = keepalive.ClientParameters{
- // send pings every n seconds if there is no activity
- Time: time.Duration(parser.Conf.Rpc.Keepalive.ClientTime) * time.Second,
- // wait n second for ping ack before considering the connection dead
- Timeout: time.Duration(parser.Conf.Rpc.Keepalive.ClientTimeout) * time.Second,
- // send pings even without active streams
- PermitWithoutStream: true,
- }
- setupDeviceClient(kacp, conns)
- setupOrganizationClient(kacp, conns)
- return
- }
|