2023-09-01 15:36:51 +08:00
|
|
|
package sub_client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
"google.golang.org/grpc"
|
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
|
|
|
|
GroupMinimumPeers int32
|
2023-10-02 16:02:27 +08:00
|
|
|
GroupMaximumPeers int32
|
|
|
|
BootstrapServers []string
|
|
|
|
GrpcDialOption grpc.DialOption
|
2023-09-01 15:36:51 +08:00
|
|
|
}
|
|
|
|
|
2023-09-05 12:43:50 +08:00
|
|
|
type ContentConfiguration struct {
|
|
|
|
Namespace string
|
|
|
|
Topic string
|
|
|
|
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 {
|
|
|
|
ConcurrentPartitionLimit int // how many partitions to process concurrently
|
|
|
|
}
|
|
|
|
|
2024-01-01 05:25:33 +08:00
|
|
|
type OnEachMessageFunc func(key, value []byte) (shouldContinue bool, 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
|
2023-12-29 03:56:37 +08:00
|
|
|
ProcessorConfig *ProcessorConfiguration
|
2023-09-01 15:36:51 +08:00
|
|
|
brokerPartitionAssignments []*mq_pb.BrokerPartitionAssignment
|
2023-09-05 12:43:50 +08:00
|
|
|
OnEachMessageFunc OnEachMessageFunc
|
|
|
|
OnCompletionFunc OnCompletionFunc
|
2023-12-12 04:05:54 +08:00
|
|
|
bootstrapBrokers []string
|
|
|
|
waitForMoreMessage bool
|
|
|
|
alreadyProcessedTsNs int64
|
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{
|
2023-12-12 04:05:54 +08:00
|
|
|
SubscriberConfig: subscriber,
|
|
|
|
ContentConfig: content,
|
2023-12-29 03:56:37 +08:00
|
|
|
ProcessorConfig: &processor,
|
2023-12-12 04:05:54 +08:00
|
|
|
bootstrapBrokers: bootstrapBrokers,
|
|
|
|
waitForMoreMessage: true,
|
|
|
|
alreadyProcessedTsNs: content.StartTime.UnixNano(),
|
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
|
|
|
}
|