subscriber.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package sub_client
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  6. "google.golang.org/grpc"
  7. "sync"
  8. )
  9. type SubscriberConfiguration struct {
  10. ClientId string
  11. ConsumerGroup string
  12. ConsumerGroupInstanceId string
  13. GrpcDialOption grpc.DialOption
  14. MaxPartitionCount int32 // how many partitions to process concurrently
  15. SlidingWindowSize int32 // how many messages to process concurrently per partition
  16. }
  17. type ContentConfiguration struct {
  18. Topic topic.Topic
  19. Filter string
  20. PartitionOffsets []*schema_pb.PartitionOffset
  21. }
  22. type OnDataMessageFn func(m *mq_pb.SubscribeMessageResponse_Data)
  23. type OnEachMessageFunc func(key, value []byte) (err error)
  24. type OnCompletionFunc func()
  25. type TopicSubscriber struct {
  26. SubscriberConfig *SubscriberConfiguration
  27. ContentConfig *ContentConfiguration
  28. brokerPartitionAssignmentChan chan *mq_pb.SubscriberToSubCoordinatorResponse
  29. brokerPartitionAssignmentAckChan chan *mq_pb.SubscriberToSubCoordinatorRequest
  30. OnDataMessageFnnc OnDataMessageFn
  31. OnEachMessageFunc OnEachMessageFunc
  32. OnCompletionFunc OnCompletionFunc
  33. bootstrapBrokers []string
  34. waitForMoreMessage bool
  35. activeProcessors map[topic.Partition]*ProcessorState
  36. activeProcessorsLock sync.Mutex
  37. PartitionOffsetChan chan KeyedOffset
  38. }
  39. func NewTopicSubscriber(bootstrapBrokers []string, subscriber *SubscriberConfiguration, content *ContentConfiguration, partitionOffsetChan chan KeyedOffset) *TopicSubscriber {
  40. return &TopicSubscriber{
  41. SubscriberConfig: subscriber,
  42. ContentConfig: content,
  43. brokerPartitionAssignmentChan: make(chan *mq_pb.SubscriberToSubCoordinatorResponse, 1024),
  44. brokerPartitionAssignmentAckChan: make(chan *mq_pb.SubscriberToSubCoordinatorRequest, 1024),
  45. bootstrapBrokers: bootstrapBrokers,
  46. waitForMoreMessage: true,
  47. activeProcessors: make(map[topic.Partition]*ProcessorState),
  48. PartitionOffsetChan: partitionOffsetChan,
  49. }
  50. }
  51. func (sub *TopicSubscriber) SetEachMessageFunc(onEachMessageFn OnEachMessageFunc) {
  52. sub.OnEachMessageFunc = onEachMessageFn
  53. }
  54. func (sub *TopicSubscriber) SetOnDataMessageFn(fn OnDataMessageFn) {
  55. sub.OnDataMessageFnnc = fn
  56. }
  57. func (sub *TopicSubscriber) SetCompletionFunc(onCompletionFn OnCompletionFunc) {
  58. sub.OnCompletionFunc = onCompletionFn
  59. }