client.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package client
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "io"
  7. "log"
  8. "time"
  9. pb "git.mokkon.com/sainw/fho_forward_proto.git"
  10. grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
  11. "google.golang.org/grpc"
  12. "google.golang.org/grpc/codes"
  13. "google.golang.org/grpc/metadata"
  14. "google.golang.org/grpc/status"
  15. "google.golang.org/protobuf/types/known/timestamppb"
  16. )
  17. type FhoClient struct {
  18. id string
  19. key string
  20. dataService pb.DataServiceClient
  21. }
  22. func NewClient(address string, id string, key string) (*FhoClient, error) {
  23. opts := []grpc_retry.CallOption{
  24. grpc_retry.WithBackoff(grpc_retry.BackoffLinear(100 * time.Millisecond)),
  25. }
  26. conn, err := grpc.Dial(address, grpc.WithInsecure(),
  27. grpc.WithStreamInterceptor(grpc_retry.StreamClientInterceptor(opts...)))
  28. if err != nil {
  29. return nil, err
  30. }
  31. return &FhoClient{id: id, key: key, dataService: pb.NewDataServiceClient(conn)}, nil
  32. }
  33. func (f *FhoClient) query(object string, updatedAfter time.Time) ([]map[string]interface{}, error) {
  34. md := metadata.Pairs("id", f.id, "key", f.key)
  35. ctx := metadata.NewOutgoingContext(context.Background(), md)
  36. r, err := f.dataService.GetData(ctx, &pb.DataRequest{Object: object, UpdatedAfter: timestamppb.New(updatedAfter)})
  37. if err != nil {
  38. return nil, err
  39. }
  40. if r.ReplyStatus == pb.DataReply_ERROR {
  41. return nil, errors.New(r.ErrorMsg)
  42. }
  43. data := r.GetData()
  44. rows := make([]map[string]interface{}, 0)
  45. err = json.Unmarshal([]byte(data), &rows)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return rows, nil
  50. }
  51. type Snapshot struct {
  52. client *FhoClient
  53. object string
  54. updatedAfter time.Time
  55. stream pb.DataService_StreamDataClient
  56. }
  57. func (c *FhoClient) QuerySnapshot(object string, updatedAfter time.Time) (*Snapshot, error) {
  58. s := &Snapshot{object: object, updatedAfter: updatedAfter, client: c}
  59. err := s.assignStream()
  60. if err != nil {
  61. return nil, err
  62. }
  63. return s, nil
  64. }
  65. func (s *Snapshot) assignStream() error {
  66. md := metadata.Pairs("id", s.client.id, "key", s.client.key)
  67. ctx := metadata.NewOutgoingContext(context.Background(), md)
  68. stream, err := s.client.dataService.StreamData(ctx, &pb.DataRequest{Object: s.object, UpdatedAfter: timestamppb.New(s.updatedAfter)}, grpc_retry.WithMax(30))
  69. if err != nil {
  70. return err
  71. }
  72. s.stream = stream
  73. return nil
  74. }
  75. func (s *Snapshot) Next() (*[]map[string]interface{}, error) {
  76. start:
  77. r, err := s.stream.Recv()
  78. if err == io.EOF {
  79. return nil, err
  80. }
  81. if err != nil {
  82. log.Println("stream error:", err.Error())
  83. statusCode, ok := status.FromError(err)
  84. if ok {
  85. if statusCode.Code() == codes.Unauthenticated {
  86. return nil, err
  87. } else if statusCode.Code() == codes.Unavailable {
  88. log.Println("Retrying stream")
  89. for {
  90. time.Sleep(time.Second * 1)
  91. err := s.assignStream()
  92. if err == nil {
  93. goto start
  94. }
  95. }
  96. } else {
  97. return nil, err
  98. }
  99. }
  100. }
  101. if r.ReplyStatus == pb.DataReply_ERROR {
  102. return nil, errors.New(r.ErrorMsg)
  103. }
  104. data := r.GetData()
  105. rec := make([]map[string]interface{}, 0)
  106. err = json.Unmarshal([]byte(data), &rec)
  107. if err != nil {
  108. return nil, err
  109. }
  110. s.updatedAfter = r.LatestUpdatedTime.AsTime()
  111. return &rec, nil
  112. }