2020-05-05 17:05:28 +08:00
|
|
|
package msgclient
|
2020-04-19 03:44:55 +08:00
|
|
|
|
2020-04-19 07:05:29 +08:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
|
2020-05-10 18:50:30 +08:00
|
|
|
"google.golang.org/grpc"
|
2020-04-19 07:05:29 +08:00
|
|
|
)
|
2020-04-19 03:44:55 +08:00
|
|
|
|
|
|
|
type Subscriber struct {
|
2020-04-21 15:59:55 +08:00
|
|
|
subscriberClients []messaging_pb.SeaweedMessaging_SubscribeClient
|
|
|
|
subscriberId string
|
2020-04-19 07:05:29 +08:00
|
|
|
}
|
|
|
|
|
2020-05-08 17:47:22 +08:00
|
|
|
/*
|
2020-04-30 17:19:51 +08:00
|
|
|
func (mc *MessagingClient) NewSubscriber(subscriberId, namespace, topic string, startTime time.Time) (*Subscriber, error) {
|
2020-04-21 15:59:55 +08:00
|
|
|
// read topic configuration
|
|
|
|
topicConfiguration := &messaging_pb.TopicConfiguration{
|
|
|
|
PartitionCount: 4,
|
|
|
|
}
|
|
|
|
subscriberClients := make([]messaging_pb.SeaweedMessaging_SubscribeClient, topicConfiguration.PartitionCount)
|
|
|
|
|
|
|
|
for i := 0; i < int(topicConfiguration.PartitionCount); i++ {
|
2020-04-30 17:19:51 +08:00
|
|
|
client, err := mc.setupSubscriberClient(subscriberId, namespace, topic, int32(i), startTime)
|
2020-04-21 15:59:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
subscriberClients[i] = client
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Subscriber{
|
|
|
|
subscriberClients: subscriberClients,
|
|
|
|
subscriberId: subscriberId,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-04-30 17:19:51 +08:00
|
|
|
func (mc *MessagingClient) setupSubscriberClient(subscriberId, namespace, topic string, partition int32, startTime time.Time) (messaging_pb.SeaweedMessaging_SubscribeClient, error) {
|
2020-04-21 15:59:55 +08:00
|
|
|
|
2020-05-08 17:47:22 +08:00
|
|
|
stream, err := setupSubscriberClient(subscriberId, namespace, topic, partition, startTime)
|
2020-04-19 07:05:29 +08:00
|
|
|
if err != nil {
|
2020-05-08 17:47:22 +08:00
|
|
|
return stream, err
|
2020-05-05 17:05:28 +08:00
|
|
|
}
|
|
|
|
if newBroker != nil {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream, nil
|
|
|
|
|
|
|
|
}
|
2020-05-08 17:47:22 +08:00
|
|
|
*/
|
2020-05-05 17:05:28 +08:00
|
|
|
|
|
|
|
func setupSubscriberClient(grpcConnection *grpc.ClientConn, subscriberId string, namespace string, topic string, partition int32, startTime time.Time) (stream messaging_pb.SeaweedMessaging_SubscribeClient, err error) {
|
|
|
|
stream, err = messaging_pb.NewSeaweedMessagingClient(grpcConnection).Subscribe(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return
|
2020-04-19 07:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// send init message
|
|
|
|
err = stream.Send(&messaging_pb.SubscriberMessage{
|
|
|
|
Init: &messaging_pb.SubscriberMessage_InitMessage{
|
|
|
|
Namespace: namespace,
|
|
|
|
Topic: topic,
|
2020-04-21 15:59:55 +08:00
|
|
|
Partition: partition,
|
2020-04-19 07:05:29 +08:00
|
|
|
StartPosition: messaging_pb.SubscriberMessage_InitMessage_TIMESTAMP,
|
2020-04-30 17:19:51 +08:00
|
|
|
TimestampNs: startTime.UnixNano(),
|
2020-04-19 07:05:29 +08:00
|
|
|
SubscriberId: subscriberId,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
2020-05-05 17:05:28 +08:00
|
|
|
return
|
2020-04-19 07:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// process init response
|
2020-05-08 17:47:22 +08:00
|
|
|
_, err = stream.Recv()
|
2020-04-19 07:05:29 +08:00
|
|
|
if err != nil {
|
2020-05-05 17:05:28 +08:00
|
|
|
return
|
2020-04-19 07:05:29 +08:00
|
|
|
}
|
2020-04-21 15:59:55 +08:00
|
|
|
return stream, nil
|
2020-04-19 03:44:55 +08:00
|
|
|
}
|
|
|
|
|
2020-04-21 15:59:55 +08:00
|
|
|
func (s *Subscriber) doSubscribe(partition int, processFn func(m *messaging_pb.Message)) error {
|
2020-04-19 07:05:29 +08:00
|
|
|
for {
|
2020-04-21 15:59:55 +08:00
|
|
|
resp, listenErr := s.subscriberClients[partition].Recv()
|
2020-04-19 07:05:29 +08:00
|
|
|
if listenErr == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if listenErr != nil {
|
2020-04-21 15:59:55 +08:00
|
|
|
println(listenErr.Error())
|
2020-04-19 07:05:29 +08:00
|
|
|
return listenErr
|
|
|
|
}
|
|
|
|
processFn(resp.Data)
|
|
|
|
}
|
2020-04-19 03:44:55 +08:00
|
|
|
}
|
2020-04-21 15:59:55 +08:00
|
|
|
|
|
|
|
// Subscribe starts goroutines to process the messages
|
|
|
|
func (s *Subscriber) Subscribe(processFn func(m *messaging_pb.Message)) {
|
2020-04-30 04:26:02 +08:00
|
|
|
for i := 0; i < len(s.subscriberClients); i++ {
|
2020-04-21 15:59:55 +08:00
|
|
|
go s.doSubscribe(i, processFn)
|
|
|
|
}
|
|
|
|
}
|