client.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package grpc
  2. import (
  3. "context"
  4. "sync"
  5. "time"
  6. "git.ikuban.com/server/kratos-etcd/registry"
  7. "git.ikuban.com/server/kratos-utils/v2/http/middleware/logging"
  8. "github.com/go-kratos/kratos/v2/log"
  9. "github.com/go-kratos/kratos/v2/middleware"
  10. "github.com/go-kratos/kratos/v2/transport/grpc"
  11. grpc2 "google.golang.org/grpc"
  12. )
  13. var (
  14. ConnMap = make(map[string]*grpc2.ClientConn)
  15. ConnLock sync.Mutex
  16. )
  17. type DialOption struct {
  18. Middlewares []middleware.Middleware
  19. Timeout time.Duration
  20. GrpcOptions []grpc2.DialOption
  21. }
  22. func GetDialInsecure(
  23. r *registry.Registry,
  24. endpointNameKey string, option *DialOption) (*grpc2.ClientConn, error) {
  25. option = checkOption(option)
  26. endpoint := grpc.WithEndpoint(endpointNameKey)
  27. if r == nil {
  28. return nil, nil
  29. }
  30. ConnLock.Lock()
  31. conn, ok := ConnMap[endpointNameKey]
  32. ConnLock.Unlock()
  33. if ok {
  34. return conn, nil
  35. }
  36. var err error
  37. conn, err = grpc.DialInsecure(context.Background(),
  38. endpoint,
  39. grpc.WithDiscovery(r),
  40. grpc.WithMiddleware(option.Middlewares...),
  41. grpc.WithTimeout(option.Timeout),
  42. grpc.WithOptions(option.GrpcOptions...),
  43. )
  44. if err != nil {
  45. return nil, err
  46. }
  47. ConnLock.Lock()
  48. ConnMap[endpointNameKey] = conn
  49. ConnLock.Unlock()
  50. return conn, nil
  51. }
  52. func GetDialSimple(endpointNameKey string, endpoint string, option *DialOption) (*grpc2.ClientConn, error) {
  53. option = checkOption(option)
  54. ConnLock.Lock()
  55. conn, ok := ConnMap[endpointNameKey]
  56. ConnLock.Unlock()
  57. if ok {
  58. return conn, nil
  59. }
  60. var err error
  61. conn, err = grpc.DialInsecure(context.Background(),
  62. grpc.WithEndpoint(endpoint),
  63. grpc.WithMiddleware(option.Middlewares...),
  64. grpc.WithTimeout(option.Timeout),
  65. grpc.WithOptions(option.GrpcOptions...),
  66. )
  67. if err != nil {
  68. return nil, err
  69. }
  70. ConnLock.Lock()
  71. ConnMap[endpointNameKey] = conn
  72. ConnLock.Unlock()
  73. return conn, nil
  74. }
  75. func NewGrpcClientOption(logger log.Logger) *DialOption {
  76. option := &DialOption{
  77. Middlewares: make([]middleware.Middleware, 0),
  78. Timeout: time.Second * 60,
  79. }
  80. if len(option.GrpcOptions) == 0 {
  81. option.GrpcOptions = append(option.GrpcOptions, grpc2.WithDefaultCallOptions(grpc2.MaxCallRecvMsgSize(ReceiveMsgSize)))
  82. }
  83. if logger != nil {
  84. option.Middlewares = append(option.Middlewares, logging.Client(logger))
  85. }
  86. return option
  87. }
  88. func checkOption(option *DialOption) *DialOption {
  89. if option == nil {
  90. option = &DialOption{
  91. Middlewares: make([]middleware.Middleware, 0),
  92. Timeout: time.Second * 60,
  93. }
  94. }
  95. if option.Timeout.Milliseconds() == 0 {
  96. option.Timeout = time.Second * 60
  97. }
  98. if len(option.GrpcOptions) == 0 {
  99. option.GrpcOptions = append(option.GrpcOptions, grpc2.WithDefaultCallOptions(grpc2.MaxCallRecvMsgSize(ReceiveMsgSize)))
  100. }
  101. return option
  102. }