2023-09-01 15:36:51 +08:00
|
|
|
package sub_client
|
|
|
|
|
|
|
|
import (
|
2024-02-06 15:14:25 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
2023-09-01 15:36:51 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
"google.golang.org/grpc"
|
2024-05-15 14:22:43 +08:00
|
|
|
"sync"
|
2023-12-12 04:05:54 +08:00
|
|
|
"time"
|
2023-09-01 15:36:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type SubscriberConfiguration struct {
|
2023-12-29 03:56:37 +08:00
|
|
|
ClientId string
|
|
|
|
ConsumerGroup string
|
|
|
|
ConsumerGroupInstanceId string
|
2024-02-06 15:14:25 +08:00
|
|
|
GrpcDialOption grpc.DialOption
|
2023-09-01 15:36:51 +08:00
|
|
|
}
|
|
|
|
|
2023-09-05 12:43:50 +08:00
|
|
|
type ContentConfiguration struct {
|
2024-02-06 15:14:25 +08:00
|
|
|
Topic topic.Topic
|
2023-09-05 12:43:50 +08:00
|
|
|
Filter string
|
2023-12-12 04:05:54 +08:00
|
|
|
StartTime time.Time
|
2023-09-05 12:43:50 +08:00
|
|
|
}
|
|
|
|
|
2023-12-29 03:56:37 +08:00
|
|
|
type ProcessorConfiguration struct {
|
2024-05-21 00:33:37 +08:00
|
|
|
MaxPartitionCount int32 // how many partitions to process concurrently
|
|
|
|
PerPartitionConcurrency int32 // how many messages to process concurrently per partition
|
2023-12-29 03:56:37 +08:00
|
|
|
}
|
|
|
|
|
2024-05-20 05:52:38 +08:00
|
|
|
type OnEachMessageFunc func(key, value []byte) (err error)
|
2023-09-05 12:43:50 +08:00
|
|
|
type OnCompletionFunc func()
|
|
|
|
|
2023-09-01 15:36:51 +08:00
|
|
|
type TopicSubscriber struct {
|
2023-09-05 12:43:50 +08:00
|
|
|
SubscriberConfig *SubscriberConfiguration
|
|
|
|
ContentConfig *ContentConfiguration
|
2024-05-15 14:22:43 +08:00
|
|
|
ProcessorConfig *ProcessorConfiguration
|
|
|
|
brokerPartitionAssignmentChan chan *mq_pb.BrokerPartitionAssignment
|
|
|
|
OnEachMessageFunc OnEachMessageFunc
|
2023-09-05 12:43:50 +08:00
|
|
|
OnCompletionFunc OnCompletionFunc
|
2023-12-12 04:05:54 +08:00
|
|
|
bootstrapBrokers []string
|
|
|
|
waitForMoreMessage bool
|
2024-05-15 14:22:43 +08:00
|
|
|
activeProcessors map[topic.Partition]*ProcessorState
|
|
|
|
activeProcessorsLock sync.Mutex
|
2023-09-01 15:36:51 +08:00
|
|
|
}
|
|
|
|
|
2023-12-29 03:56:37 +08:00
|
|
|
func NewTopicSubscriber(bootstrapBrokers []string, subscriber *SubscriberConfiguration, content *ContentConfiguration, processor ProcessorConfiguration) *TopicSubscriber {
|
2023-09-01 15:36:51 +08:00
|
|
|
return &TopicSubscriber{
|
2024-05-15 14:22:43 +08:00
|
|
|
SubscriberConfig: subscriber,
|
|
|
|
ContentConfig: content,
|
|
|
|
ProcessorConfig: &processor,
|
|
|
|
brokerPartitionAssignmentChan: make(chan *mq_pb.BrokerPartitionAssignment, 1024),
|
|
|
|
bootstrapBrokers: bootstrapBrokers,
|
|
|
|
waitForMoreMessage: true,
|
|
|
|
activeProcessors: make(map[topic.Partition]*ProcessorState),
|
2023-09-01 15:36:51 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-05 12:43:50 +08:00
|
|
|
func (sub *TopicSubscriber) SetEachMessageFunc(onEachMessageFn OnEachMessageFunc) {
|
|
|
|
sub.OnEachMessageFunc = onEachMessageFn
|
|
|
|
}
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
func (sub *TopicSubscriber) SetCompletionFunc(onCompletionFn OnCompletionFunc) {
|
|
|
|
sub.OnCompletionFunc = onCompletionFn
|
2023-09-05 12:43:50 +08:00
|
|
|
}
|