Repository navigation
Expand file tree
/
Copy pathlist_offsets.go
More file actions
63 lines (50 loc) · 1.72 KB
/
Copy pathlist_offsets.go
File metadata and controls
63 lines (50 loc) · 1.72 KB
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
58
59
60
61
62
63
package minion
import (
"context"
"fmt"
"github.com/twmb/franz-go/pkg/kmsg"
"strconv"
"time"
)
func (s *Service) ListOffsetsCached(ctx context.Context, timestamp int64) (*kmsg.ListOffsetsResponse, error) {
reqId := ctx.Value("requestId").(string)
key := "partition-offsets-" + strconv.Itoa(int(timestamp)) + "-" + reqId
if cachedRes, exists := s.getCachedItem(key); exists {
return cachedRes.(*kmsg.ListOffsetsResponse), nil
}
res, err, _ := s.requestGroup.Do(key, func() (interface{}, error) {
offsets, err := s.ListOffsets(ctx, timestamp)
if err != nil {
return nil, err
}
s.setCachedItem(key, offsets, 120*time.Second)
return offsets, nil
})
if err != nil {
return nil, err
}
return res.(*kmsg.ListOffsetsResponse), nil
}
// ListOffsets fetches the low (timestamp: -2) or high water mark (timestamp: -1) for all topic partitions
func (s *Service) ListOffsets(ctx context.Context, timestamp int64) (*kmsg.ListOffsetsResponse, error) {
metadata, err := s.GetMetadataCached(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list consumer groups: %w", err)
}
topicReqs := make([]kmsg.ListOffsetsRequestTopic, len(metadata.Topics))
for i, topic := range metadata.Topics {
req := kmsg.NewListOffsetsRequestTopic()
req.Topic = topic.Topic
partitionReqs := make([]kmsg.ListOffsetsRequestTopicPartition, len(topic.Partitions))
for j, partition := range topic.Partitions {
partitionReqs[j] = kmsg.NewListOffsetsRequestTopicPartition()
partitionReqs[j].Partition = partition.Partition
partitionReqs[j].Timestamp = timestamp
}
req.Partitions = partitionReqs
topicReqs[i] = req
}
req := kmsg.NewListOffsetsRequest()
req.Topics = topicReqs
return req.RequestWith(ctx, s.kafkaSvc.Client)
}