You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.2 KiB

1 year ago
  1. package sub_client
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  7. "sync"
  8. )
  9. func (sub *TopicSubscriber) doProcess() error {
  10. var wg sync.WaitGroup
  11. for _, brokerPartitionAssignment := range sub.brokerPartitionAssignments {
  12. brokerAddress := brokerPartitionAssignment.LeaderBroker
  13. grpcConnection, err := pb.GrpcDial(context.Background(), brokerAddress, true, sub.SubscriberConfig.GrpcDialOption)
  14. if err != nil {
  15. return fmt.Errorf("dial broker %s: %v", brokerAddress, err)
  16. }
  17. brokerClient := mq_pb.NewSeaweedMessagingClient(grpcConnection)
  18. subscribeClient, err := brokerClient.Subscribe(context.Background(), &mq_pb.SubscribeRequest{
  19. Message: &mq_pb.SubscribeRequest_Init{
  20. Init: &mq_pb.SubscribeRequest_InitMessage{
  21. ConsumerGroup: sub.SubscriberConfig.GroupId,
  22. ConsumerId: sub.SubscriberConfig.GroupInstanceId,
  23. Topic: &mq_pb.Topic{
  24. Namespace: sub.ContentConfig.Namespace,
  25. Name: sub.ContentConfig.Topic,
  26. },
  27. Partition: &mq_pb.Partition{
  28. RingSize: brokerPartitionAssignment.Partition.RingSize,
  29. RangeStart: brokerPartitionAssignment.Partition.RangeStart,
  30. RangeStop: brokerPartitionAssignment.Partition.RangeStop,
  31. },
  32. Filter: sub.ContentConfig.Filter,
  33. },
  34. },
  35. })
  36. if err != nil {
  37. return fmt.Errorf("create subscribe client: %v", err)
  38. }
  39. wg.Add(1)
  40. go func() {
  41. defer wg.Done()
  42. if sub.OnCompletionFunc != nil {
  43. defer sub.OnCompletionFunc()
  44. }
  45. defer func() {
  46. subscribeClient.SendMsg(&mq_pb.SubscribeRequest{
  47. Message: &mq_pb.SubscribeRequest_Ack{
  48. Ack: &mq_pb.SubscribeRequest_AckMessage{
  49. Sequence: 0,
  50. },
  51. },
  52. })
  53. subscribeClient.CloseSend()
  54. }()
  55. for {
  56. resp, err := subscribeClient.Recv()
  57. if err != nil {
  58. fmt.Printf("subscribe error: %v\n", err)
  59. return
  60. }
  61. if resp.Message == nil {
  62. continue
  63. }
  64. switch m := resp.Message.(type) {
  65. case *mq_pb.SubscribeResponse_Data:
  66. if !sub.OnEachMessageFunc(m.Data.Key, m.Data.Value) {
  67. return
  68. }
  69. case *mq_pb.SubscribeResponse_Ctrl:
  70. if m.Ctrl.IsEndOfStream || m.Ctrl.IsEndOfTopic {
  71. return
  72. }
  73. }
  74. }
  75. }()
  76. }
  77. wg.Wait()
  78. return nil
  79. }