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.

63 lines
1.9 KiB

3 weeks 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"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/mq_agent_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  9. "google.golang.org/grpc"
  10. )
  11. type SubscribeOption struct {
  12. ConsumerGroup string
  13. ConsumerGroupInstanceId string
  14. Topic topic.Topic
  15. Filter string
  16. MaxSubscribedPartitions int32
  17. PerPartitionConcurrency int32
  18. }
  19. type SubscribeSession struct {
  20. Option *SubscribeOption
  21. stream grpc.BidiStreamingClient[mq_agent_pb.SubscribeRecordRequest, mq_agent_pb.SubscribeRecordResponse]
  22. sessionId int64
  23. }
  24. func NewSubscribeSession(agentAddress string, option *SubscribeOption) (*SubscribeSession, error) {
  25. // call local agent grpc server to create a new session
  26. clientConn, err := pb.GrpcDial(context.Background(), agentAddress, true, grpc.WithInsecure())
  27. if err != nil {
  28. return nil, fmt.Errorf("dial agent server %s: %v", agentAddress, err)
  29. }
  30. agentClient := mq_agent_pb.NewSeaweedMessagingAgentClient(clientConn)
  31. resp, err := agentClient.StartSubscribeSession(context.Background(), &mq_agent_pb.StartSubscribeSessionRequest{
  32. ConsumerGroup: option.ConsumerGroup,
  33. ConsumerGroupInstanceId: option.ConsumerGroupInstanceId,
  34. Topic: &schema_pb.Topic{
  35. Namespace: option.Topic.Namespace,
  36. Name: option.Topic.Name,
  37. },
  38. MaxSubscribedPartitions: option.MaxSubscribedPartitions,
  39. Filter: option.Filter,
  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. return &SubscribeSession{
  52. Option: option,
  53. stream: stream,
  54. sessionId: resp.SessionId,
  55. }, nil
  56. }