subscriber.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package sub_client
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  5. "google.golang.org/grpc"
  6. "time"
  7. )
  8. type SubscriberConfiguration struct {
  9. ClientId string
  10. ConsumerGroup string
  11. ConsumerGroupInstanceId string
  12. GroupMinimumPeers int32
  13. GroupMaximumPeers int32
  14. BootstrapServers []string
  15. GrpcDialOption grpc.DialOption
  16. }
  17. type ContentConfiguration struct {
  18. Topic topic.Topic
  19. Filter string
  20. StartTime time.Time
  21. }
  22. type ProcessorConfiguration struct {
  23. ConcurrentPartitionLimit int // how many partitions to process concurrently
  24. }
  25. type OnEachMessageFunc func(key, value []byte) (shouldContinue bool, err error)
  26. type OnCompletionFunc func()
  27. type TopicSubscriber struct {
  28. SubscriberConfig *SubscriberConfiguration
  29. ContentConfig *ContentConfiguration
  30. ProcessorConfig *ProcessorConfiguration
  31. brokerPartitionAssignments []*mq_pb.BrokerPartitionAssignment
  32. OnEachMessageFunc OnEachMessageFunc
  33. OnCompletionFunc OnCompletionFunc
  34. bootstrapBrokers []string
  35. waitForMoreMessage bool
  36. alreadyProcessedTsNs int64
  37. }
  38. func NewTopicSubscriber(bootstrapBrokers []string, subscriber *SubscriberConfiguration, content *ContentConfiguration, processor ProcessorConfiguration) *TopicSubscriber {
  39. return &TopicSubscriber{
  40. SubscriberConfig: subscriber,
  41. ContentConfig: content,
  42. ProcessorConfig: &processor,
  43. bootstrapBrokers: bootstrapBrokers,
  44. waitForMoreMessage: true,
  45. alreadyProcessedTsNs: content.StartTime.UnixNano(),
  46. }
  47. }
  48. func (sub *TopicSubscriber) SetEachMessageFunc(onEachMessageFn OnEachMessageFunc) {
  49. sub.OnEachMessageFunc = onEachMessageFn
  50. }
  51. func (sub *TopicSubscriber) SetCompletionFunc(onCompletionFn OnCompletionFunc) {
  52. sub.OnCompletionFunc = onCompletionFn
  53. }