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.

64 lines
1.9 KiB

  1. package agent
  2. import (
  3. "context"
  4. "github.com/seaweedfs/seaweedfs/weed/mq/client/pub_client"
  5. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/mq_agent_pb"
  7. "log/slog"
  8. "math/rand/v2"
  9. "time"
  10. )
  11. func (a *MessageQueueAgent) StartPublishSession(ctx context.Context, req *mq_agent_pb.StartPublishSessionRequest) (*mq_agent_pb.StartPublishSessionResponse, error) {
  12. sessionId := rand.Int64()
  13. topicPublisher, err := pub_client.NewTopicPublisher(
  14. &pub_client.PublisherConfiguration{
  15. Topic: topic.NewTopic(req.Topic.Namespace, req.Topic.Name),
  16. PartitionCount: req.PartitionCount,
  17. Brokers: a.brokersList(),
  18. PublisherName: req.PublisherName,
  19. RecordType: req.RecordType,
  20. })
  21. if err != nil {
  22. return nil, err
  23. }
  24. a.publishersLock.Lock()
  25. // remove inactive publishers to avoid memory leak
  26. for k, entry := range a.publishers {
  27. if entry.lastActiveTsNs == 0 {
  28. // this is an active session
  29. continue
  30. }
  31. if time.Unix(0, entry.lastActiveTsNs).Add(10 * time.Hour).Before(time.Now()) {
  32. delete(a.publishers, k)
  33. }
  34. }
  35. a.publishers[SessionId(sessionId)] = &SessionEntry[*pub_client.TopicPublisher]{
  36. entry: topicPublisher,
  37. }
  38. a.publishersLock.Unlock()
  39. return &mq_agent_pb.StartPublishSessionResponse{
  40. SessionId: sessionId,
  41. }, nil
  42. }
  43. func (a *MessageQueueAgent) ClosePublishSession(ctx context.Context, req *mq_agent_pb.ClosePublishSessionRequest) (*mq_agent_pb.ClosePublishSessionResponse, error) {
  44. var finishErr string
  45. a.publishersLock.Lock()
  46. publisherEntry, found := a.publishers[SessionId(req.SessionId)]
  47. if found {
  48. if err := publisherEntry.entry.FinishPublish(); err != nil {
  49. finishErr = err.Error()
  50. slog.Warn("failed to finish publish", "error", err)
  51. }
  52. delete(a.publishers, SessionId(req.SessionId))
  53. }
  54. a.publishersLock.Unlock()
  55. return &mq_agent_pb.ClosePublishSessionResponse{
  56. Error: finishErr,
  57. }, nil
  58. }