aboutsummaryrefslogtreecommitdiff
path: root/weed/mq/agent/agent_grpc_sub_session.go
blob: 17def9ed57a8d6ed406614188f27ece66d6cf35c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package agent

import (
	"context"
	"github.com/seaweedfs/seaweedfs/weed/mq/client/sub_client"
	"github.com/seaweedfs/seaweedfs/weed/mq/topic"
	"github.com/seaweedfs/seaweedfs/weed/pb/mq_agent_pb"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"math/rand/v2"
	"time"
)

func (a *MessageQueueAgent) StartSubscribeSession(ctx context.Context, req *mq_agent_pb.StartSubscribeSessionRequest) (*mq_agent_pb.StartSubscribeSessionResponse, error) {
	sessionId := rand.Int64()

	subscriberConfig := &sub_client.SubscriberConfiguration{
		ConsumerGroup:           req.ConsumerGroup,
		ConsumerGroupInstanceId: req.ConsumerGroupInstanceId,
		GrpcDialOption:          grpc.WithTransportCredentials(insecure.NewCredentials()),
		MaxPartitionCount:       req.MaxSubscribedPartitions,
		SlidingWindowSize:       req.SlidingWindowSize,
	}

	contentConfig := &sub_client.ContentConfiguration{
		Topic:            topic.FromPbTopic(req.Topic),
		Filter:           req.Filter,
		PartitionOffsets: req.PartitionOffsets,
	}

	topicSubscriber := sub_client.NewTopicSubscriber(
		a.brokersList(),
		subscriberConfig,
		contentConfig,
		make(chan sub_client.KeyedOffset, 1024),
	)

	a.subscribersLock.Lock()
	// remove inactive publishers to avoid memory leak
	for k, entry := range a.subscribers {
		if entry.lastActiveTsNs == 0 {
			// this is an active session
			continue
		}
		if time.Unix(0, entry.lastActiveTsNs).Add(10 * time.Hour).Before(time.Now()) {
			delete(a.subscribers, k)
		}
	}
	a.subscribers[SessionId(sessionId)] = &SessionEntry[*sub_client.TopicSubscriber]{
		entry: topicSubscriber,
	}
	a.subscribersLock.Unlock()

	return &mq_agent_pb.StartSubscribeSessionResponse{
		SessionId: sessionId,
	}, nil
}