2020-04-18 19:44:55 +00:00
|
|
|
package client
|
|
|
|
|
2020-04-18 23:05:29 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/messaging_pb"
|
|
|
|
)
|
2020-04-18 19:44:55 +00:00
|
|
|
|
|
|
|
type Subscriber struct {
|
2020-04-21 07:59:55 +00:00
|
|
|
subscriberClients []messaging_pb.SeaweedMessaging_SubscribeClient
|
|
|
|
subscriberId string
|
2020-04-18 23:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MessagingClient) NewSubscriber(subscriberId, namespace, topic string) (*Subscriber, error) {
|
2020-04-21 07:59:55 +00: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++ {
|
|
|
|
client, err := mc.setupSubscriberClient(subscriberId, namespace, topic, int32(i))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
subscriberClients[i] = client
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Subscriber{
|
|
|
|
subscriberClients: subscriberClients,
|
|
|
|
subscriberId: subscriberId,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mc *MessagingClient) setupSubscriberClient(subscriberId, namespace, topic string, partition int32) (messaging_pb.SeaweedMessaging_SubscribeClient, error) {
|
|
|
|
|
2020-04-18 23:05:29 +00:00
|
|
|
stream, err := messaging_pb.NewSeaweedMessagingClient(mc.grpcConnection).Subscribe(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// send init message
|
|
|
|
err = stream.Send(&messaging_pb.SubscriberMessage{
|
|
|
|
Init: &messaging_pb.SubscriberMessage_InitMessage{
|
|
|
|
Namespace: namespace,
|
|
|
|
Topic: topic,
|
2020-04-21 07:59:55 +00:00
|
|
|
Partition: partition,
|
2020-04-18 23:05:29 +00:00
|
|
|
StartPosition: messaging_pb.SubscriberMessage_InitMessage_TIMESTAMP,
|
|
|
|
TimestampNs: time.Now().UnixNano(),
|
|
|
|
SubscriberId: subscriberId,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// process init response
|
|
|
|
initResponse, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if initResponse.Redirect != nil {
|
|
|
|
// TODO follow redirection
|
|
|
|
}
|
|
|
|
|
2020-04-21 07:59:55 +00:00
|
|
|
return stream, nil
|
|
|
|
|
2020-04-18 19:44:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 07:59:55 +00:00
|
|
|
func (s *Subscriber) doSubscribe(partition int, processFn func(m *messaging_pb.Message)) error {
|
2020-04-18 23:05:29 +00:00
|
|
|
for {
|
2020-04-21 07:59:55 +00:00
|
|
|
resp, listenErr := s.subscriberClients[partition].Recv()
|
2020-04-18 23:05:29 +00:00
|
|
|
if listenErr == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if listenErr != nil {
|
2020-04-21 07:59:55 +00:00
|
|
|
println(listenErr.Error())
|
2020-04-18 23:05:29 +00:00
|
|
|
return listenErr
|
|
|
|
}
|
|
|
|
processFn(resp.Data)
|
|
|
|
}
|
2020-04-18 19:44:55 +00:00
|
|
|
}
|
2020-04-21 07:59:55 +00:00
|
|
|
|
|
|
|
// Subscribe starts goroutines to process the messages
|
|
|
|
func (s *Subscriber) Subscribe(processFn func(m *messaging_pb.Message)) {
|
|
|
|
for i:=0;i<len(s.subscriberClients);i++{
|
|
|
|
go s.doSubscribe(i, processFn)
|
|
|
|
}
|
|
|
|
}
|