You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.2 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. package broker
  2. import (
  3. "fmt"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  8. "time"
  9. )
  10. func (broker *MessageQueueBroker) Subscribe(req *mq_pb.SubscribeRequest, stream mq_pb.SeaweedMessaging_SubscribeServer) error {
  11. localTopicPartition := broker.localTopicManager.GetTopicPartition(topic.FromPbTopic(req.Init.Topic),
  12. topic.FromPbPartition(req.Init.Partition))
  13. if localTopicPartition == nil {
  14. stream.Send(&mq_pb.SubscribeResponse{
  15. Message: &mq_pb.SubscribeResponse_Ctrl{
  16. Ctrl: &mq_pb.SubscribeResponse_CtrlMessage{
  17. Error: "not found",
  18. },
  19. },
  20. })
  21. return nil
  22. }
  23. localTopicPartition.Subscribe("client", time.Now(), func(logEntry *filer_pb.LogEntry) error {
  24. value := logEntry.GetData()
  25. if err := stream.Send(&mq_pb.SubscribeResponse{Message: &mq_pb.SubscribeResponse_Data{
  26. Data: &mq_pb.DataMessage{
  27. Key: []byte(fmt.Sprintf("key-%d", logEntry.PartitionKeyHash)),
  28. Value: value,
  29. },
  30. }}); err != nil {
  31. glog.Errorf("Error sending setup response: %v", err)
  32. return err
  33. }
  34. return nil
  35. })
  36. return nil
  37. }