setup.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright 2019 autocareai.com. All rights reserved.
  2. // Use of this source code is governed by autocareai.com.
  3. package pb
  4. import (
  5. "access-control-monitor/parser"
  6. fmt "fmt"
  7. "time"
  8. grpc "google.golang.org/grpc"
  9. "google.golang.org/grpc/keepalive"
  10. )
  11. // 客户端集合
  12. var Auth SmartAuthClient
  13. var Alarm SmartAlarmClient
  14. var Thirdparty SmartSiteThirdpartyClient
  15. func setupSmartAuthClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  16. // 根据是否为k8s来组装targets
  17. var serviceName string
  18. if parser.Conf.K8s {
  19. serviceName = parser.Conf.Rpc.SmartAuth.ServiceName
  20. } else {
  21. serviceName = parser.Conf.Rpc.SmartAuth.ServiceIp
  22. }
  23. // 发起一个连接并记录连接conn,后期释放
  24. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  25. parser.Conf.Rpc.SmartAuth.ServicePort),
  26. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  27. Auth = NewSmartAuthClient(conn)
  28. conns = append(conns, conn)
  29. } else {
  30. fmt.Println("[rpc] dial cabinet conn err", err)
  31. }
  32. return
  33. }
  34. func setupSmartAlarmClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  35. // 根据是否为k8s来组装targets
  36. var serviceName string
  37. if parser.Conf.K8s {
  38. serviceName = parser.Conf.Rpc.SmartAlarm.ServiceName
  39. } else {
  40. serviceName = parser.Conf.Rpc.SmartAlarm.ServiceIp
  41. }
  42. // 发起一个连接并记录连接conn,后期释放
  43. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  44. parser.Conf.Rpc.SmartAlarm.ServicePort),
  45. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  46. Alarm = NewSmartAlarmClient(conn)
  47. conns = append(conns, conn)
  48. } else {
  49. fmt.Println("[rpc] dial cabinet conn err", err)
  50. }
  51. return
  52. }
  53. func setupSmartThirdpartyClient(kacp keepalive.ClientParameters, conns []*grpc.ClientConn) {
  54. // 根据是否为k8s来组装targets
  55. var serviceName string
  56. if parser.Conf.K8s {
  57. serviceName = parser.Conf.Rpc.SmartThirdparty.ServiceName
  58. } else {
  59. serviceName = parser.Conf.Rpc.SmartThirdparty.ServiceIp
  60. }
  61. // 发起一个连接并记录连接conn,后期释放
  62. if conn, err := grpc.Dial(fmt.Sprintf("%s:%d", serviceName,
  63. parser.Conf.Rpc.SmartThirdparty.ServicePort),
  64. grpc.WithInsecure(), grpc.WithKeepaliveParams(kacp)); err == nil {
  65. Thirdparty = NewSmartSiteThirdpartyClient(conn)
  66. conns = append(conns, conn)
  67. } else {
  68. fmt.Println("[rpc] dial cabinet conn err", err)
  69. }
  70. return
  71. }
  72. // SetupClients 创建客户端
  73. func SetupClients() (conns []*grpc.ClientConn) {
  74. // 客户端配置参数
  75. var kacp = keepalive.ClientParameters{
  76. // send pings every n seconds if there is no activity
  77. Time: time.Duration(parser.Conf.Rpc.Keepalive.ClientTime) * time.Second,
  78. // wait n second for ping ack before considering the connection dead
  79. Timeout: time.Duration(parser.Conf.Rpc.Keepalive.ClientTimeout) * time.Second,
  80. // send pings even without active streams
  81. PermitWithoutStream: true,
  82. }
  83. setupSmartAuthClient(kacp, conns)
  84. setupSmartAlarmClient(kacp, conns)
  85. setupSmartThirdpartyClient(kacp, conns)
  86. return
  87. }