seaweedfs/weed/mq/msgclient/subscriber.go

121 lines
3.1 KiB
Go
Raw Normal View History

2020-05-05 09:05:28 +00:00
package msgclient
2020-04-18 23:05:29 +00:00
import (
"context"
"io"
2020-05-18 00:33:53 +00:00
"sync"
2020-06-05 22:27:10 +00:00
"time"
2020-04-18 23:05:29 +00:00
2022-07-02 05:43:25 +00:00
"github.com/chrislusf/seaweedfs/weed/mq/broker"
"github.com/chrislusf/seaweedfs/weed/pb/mq_pb"
2020-05-10 10:50:30 +00:00
"google.golang.org/grpc"
2020-04-18 23:05:29 +00:00
)
type Subscriber struct {
2022-07-02 05:43:25 +00:00
subscriberClients []mq_pb.SeaweedMessaging_SubscribeClient
2020-05-17 18:10:45 +00:00
subscriberCancels []context.CancelFunc
subscriberId string
2020-04-18 23:05:29 +00:00
}
2020-05-17 01:53:54 +00:00
func (mc *MessagingClient) NewSubscriber(subscriberId, namespace, topic string, partitionId int, startTime time.Time) (*Subscriber, error) {
// read topic configuration
2022-07-02 05:43:25 +00:00
topicConfiguration := &mq_pb.TopicConfiguration{
PartitionCount: 4,
}
2022-07-02 05:43:25 +00:00
subscriberClients := make([]mq_pb.SeaweedMessaging_SubscribeClient, topicConfiguration.PartitionCount)
2020-05-17 18:10:45 +00:00
subscriberCancels := make([]context.CancelFunc, topicConfiguration.PartitionCount)
for i := 0; i < int(topicConfiguration.PartitionCount); i++ {
2020-06-05 22:27:10 +00:00
if partitionId >= 0 && i != partitionId {
2020-05-17 01:53:54 +00:00
continue
}
tp := broker.TopicPartition{
Namespace: namespace,
2020-06-05 22:27:10 +00:00
Topic: topic,
Partition: int32(i),
}
grpcClientConn, err := mc.findBroker(tp)
if err != nil {
return nil, err
}
2020-05-17 18:10:45 +00:00
ctx, cancel := context.WithCancel(context.Background())
client, err := setupSubscriberClient(ctx, grpcClientConn, tp, subscriberId, startTime)
if err != nil {
return nil, err
}
subscriberClients[i] = client
2020-05-17 18:10:45 +00:00
subscriberCancels[i] = cancel
}
return &Subscriber{
subscriberClients: subscriberClients,
2020-05-17 18:10:45 +00:00
subscriberCancels: subscriberCancels,
subscriberId: subscriberId,
}, nil
}
2022-07-02 05:43:25 +00:00
func setupSubscriberClient(ctx context.Context, grpcConnection *grpc.ClientConn, tp broker.TopicPartition, subscriberId string, startTime time.Time) (stream mq_pb.SeaweedMessaging_SubscribeClient, err error) {
stream, err = mq_pb.NewSeaweedMessagingClient(grpcConnection).Subscribe(ctx)
2020-05-05 09:05:28 +00:00
if err != nil {
return
2020-04-18 23:05:29 +00:00
}
// send init message
2022-07-02 05:43:25 +00:00
err = stream.Send(&mq_pb.SubscriberMessage{
Init: &mq_pb.SubscriberMessage_InitMessage{
Namespace: tp.Namespace,
Topic: tp.Topic,
Partition: tp.Partition,
2022-07-02 05:43:25 +00:00
StartPosition: mq_pb.SubscriberMessage_InitMessage_TIMESTAMP,
2020-04-30 09:19:51 +00:00
TimestampNs: startTime.UnixNano(),
2020-04-18 23:05:29 +00:00
SubscriberId: subscriberId,
},
})
if err != nil {
2020-05-05 09:05:28 +00:00
return
2020-04-18 23:05:29 +00:00
}
return stream, nil
}
2022-07-02 05:43:25 +00:00
func doSubscribe(subscriberClient mq_pb.SeaweedMessaging_SubscribeClient, processFn func(m *mq_pb.Message)) error {
2020-04-18 23:05:29 +00:00
for {
2020-05-17 01:53:54 +00:00
resp, listenErr := subscriberClient.Recv()
2020-04-18 23:05:29 +00:00
if listenErr == io.EOF {
return nil
}
if listenErr != nil {
println(listenErr.Error())
2020-04-18 23:05:29 +00:00
return listenErr
}
if resp.Data == nil {
// this could be heartbeat from broker
continue
}
2020-04-18 23:05:29 +00:00
processFn(resp.Data)
}
}
// Subscribe starts goroutines to process the messages
2022-07-02 05:43:25 +00:00
func (s *Subscriber) Subscribe(processFn func(m *mq_pb.Message)) {
2020-05-18 00:33:53 +00:00
var wg sync.WaitGroup
2020-04-29 20:26:02 +00:00
for i := 0; i < len(s.subscriberClients); i++ {
2020-05-17 01:53:54 +00:00
if s.subscriberClients[i] != nil {
2020-05-18 00:33:53 +00:00
wg.Add(1)
2022-07-02 05:43:25 +00:00
go func(subscriberClient mq_pb.SeaweedMessaging_SubscribeClient) {
2020-05-18 00:33:53 +00:00
defer wg.Done()
2020-05-18 00:38:21 +00:00
doSubscribe(subscriberClient, processFn)
}(s.subscriberClients[i])
2020-05-17 01:53:54 +00:00
}
}
2020-05-18 00:33:53 +00:00
wg.Wait()
}
2020-05-17 18:10:45 +00:00
func (s *Subscriber) Shutdown() {
for i := 0; i < len(s.subscriberClients); i++ {
if s.subscriberCancels[i] != nil {
s.subscriberCancels[i]()
}
}
}