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.

73 lines
2.1 KiB

7 days ago
7 days ago
1 week ago
  1. package agent_client
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/mq_agent_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  8. "google.golang.org/grpc"
  9. "google.golang.org/grpc/credentials/insecure"
  10. )
  11. type SubscribeOption struct {
  12. ConsumerGroup string
  13. ConsumerGroupInstanceId string
  14. Topic topic.Topic
  15. Filter string
  16. MaxSubscribedPartitions int32
  17. SlidingWindowSize int32
  18. }
  19. type SubscribeSession struct {
  20. Option *SubscribeOption
  21. stream grpc.BidiStreamingClient[mq_agent_pb.SubscribeRecordRequest, mq_agent_pb.SubscribeRecordResponse]
  22. }
  23. func NewSubscribeSession(agentAddress string, option *SubscribeOption) (*SubscribeSession, error) {
  24. // call local agent grpc server to create a new session
  25. clientConn, err := grpc.NewClient(agentAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
  26. if err != nil {
  27. return nil, fmt.Errorf("dial agent server %s: %v", agentAddress, err)
  28. }
  29. agentClient := mq_agent_pb.NewSeaweedMessagingAgentClient(clientConn)
  30. resp, err := agentClient.StartSubscribeSession(context.Background(), &mq_agent_pb.StartSubscribeSessionRequest{
  31. ConsumerGroup: option.ConsumerGroup,
  32. ConsumerGroupInstanceId: option.ConsumerGroupInstanceId,
  33. Topic: &schema_pb.Topic{
  34. Namespace: option.Topic.Namespace,
  35. Name: option.Topic.Name,
  36. },
  37. MaxSubscribedPartitions: option.MaxSubscribedPartitions,
  38. Filter: option.Filter,
  39. SlidingWindowSize: option.SlidingWindowSize,
  40. })
  41. if err != nil {
  42. return nil, err
  43. }
  44. if resp.Error != "" {
  45. return nil, fmt.Errorf("start subscribe session: %v", resp.Error)
  46. }
  47. stream, err := agentClient.SubscribeRecord(context.Background())
  48. if err != nil {
  49. return nil, fmt.Errorf("subscribe record: %v", err)
  50. }
  51. if err = stream.Send(&mq_agent_pb.SubscribeRecordRequest{
  52. SessionId: resp.SessionId,
  53. }); err != nil {
  54. return nil, fmt.Errorf("send session id: %v", err)
  55. }
  56. return &SubscribeSession{
  57. Option: option,
  58. stream: stream,
  59. }, nil
  60. }
  61. func (s *SubscribeSession) CloseSession() error {
  62. err := s.stream.CloseSend()
  63. return err
  64. }