-
Notifications
You must be signed in to change notification settings - Fork 122
/
metadata.go
45 lines (35 loc) · 968 Bytes
/
metadata.go
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
package minion
import (
"context"
"fmt"
"github.com/twmb/franz-go/pkg/kmsg"
"time"
)
func (s *Service) GetMetadataCached(ctx context.Context) (*kmsg.MetadataResponse, error) {
reqId := ctx.Value("requestId").(string)
key := "metadata-" + reqId
if cachedRes, exists := s.getCachedItem(key); exists {
return cachedRes.(*kmsg.MetadataResponse), nil
}
res, err, _ := s.requestGroup.Do(key, func() (interface{}, error) {
metadata, err := s.GetMetadata(ctx)
if err != nil {
return nil, err
}
s.setCachedItem(key, metadata, 120*time.Second)
return metadata, nil
})
if err != nil {
return nil, err
}
return res.(*kmsg.MetadataResponse), nil
}
func (s *Service) GetMetadata(ctx context.Context) (*kmsg.MetadataResponse, error) {
req := kmsg.NewMetadataRequest()
req.Topics = nil
res, err := req.RequestWith(ctx, s.kafkaSvc.Client)
if err != nil {
return nil, fmt.Errorf("failed to request metadata: %w", err)
}
return res, nil
}