forked from elodina/siesta
/
metadata.go
81 lines (68 loc) · 1.69 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package siesta
import (
"fmt"
"sync"
"time"
)
type Metadata struct {
connector Connector
metadataExpire time.Duration
cache map[string]*metadataEntry
refreshLock sync.Mutex
}
func NetMetadata(connector Connector, metadataExpire time.Duration) *Metadata {
return &Metadata{
connector: connector,
metadataExpire: metadataExpire,
cache: make(map[string]*metadataEntry),
}
}
func (tmc *Metadata) Get(topic string) ([]int32, error) {
cache := tmc.cache[topic]
if cache == nil {
err := tmc.Refresh([]string{topic})
if err != nil {
return nil, err
}
}
cache = tmc.cache[topic]
if cache != nil {
if cache.timestamp.Add(tmc.metadataExpire).Before(time.Now()) {
err := tmc.Refresh([]string{topic})
if err != nil {
return nil, err
}
}
cache = tmc.cache[topic]
if cache != nil {
return cache.partitions, nil
}
}
return nil, fmt.Errorf("Could not get topic metadata for topic %s", topic)
}
func (tmc *Metadata) Refresh(topics []string) error {
tmc.refreshLock.Lock()
defer tmc.refreshLock.Unlock()
topicMetadataResponse, err := tmc.connector.GetTopicMetadata(topics)
if err != nil {
return err
}
for _, topicMetadata := range topicMetadataResponse.TopicsMetadata {
partitions := make([]int32, 0)
for _, partitionMetadata := range topicMetadata.PartitionsMetadata {
partitions = append(partitions, partitionMetadata.PartitionID)
}
tmc.cache[topicMetadata.Topic] = newMetadataEntry(partitions)
}
return nil
}
type metadataEntry struct {
partitions []int32
timestamp time.Time
}
func newMetadataEntry(partitions []int32) *metadataEntry {
return &metadataEntry{
partitions: partitions,
timestamp: time.Now(),
}
}