-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathhelper.go
80 lines (68 loc) · 1.68 KB
/
helper.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
package healer
type MetaInfo struct {
brokers []*BrokerInfo
topicMetadatas []TopicMetadata
//groups []string
}
type Helper struct {
clientID string
brokers *Brokers
metaInfo *MetaInfo
}
func NewHelperFromBrokers(brokers *Brokers, clientID string) *Helper {
h := &Helper{
clientID: clientID,
brokers: brokers,
metaInfo: &MetaInfo{},
}
return h
}
func NewHelper(brokerList, clientID string, config *BrokerConfig) (*Helper, error) {
//func NewBrokers(brokerList string, clientID string, connecTimeout int, timeout int) (*Brokers, error) {
var (
err error
)
h := &Helper{
clientID: clientID,
}
h.brokers, err = NewBrokersWithConfig(brokerList, config)
h.metaInfo = &MetaInfo{}
if err != nil {
return nil, err
}
return h, nil
}
func (h *Helper) UpdateMeta() error {
metadataResponse, err := h.brokers.RequestMetaData(h.clientID, []string{})
if err != nil {
return err
}
h.metaInfo = &MetaInfo{}
h.metaInfo.brokers = metadataResponse.Brokers
h.metaInfo.topicMetadatas = metadataResponse.TopicMetadatas
return nil
}
func (h *Helper) GetGroups() []string {
err := h.UpdateMeta()
if err != nil {
logger.Error(err, "update metadata error")
return nil
}
groups := []string{}
for _, brokerinfo := range h.metaInfo.brokers {
broker, err := h.brokers.GetBroker(brokerinfo.NodeID)
if err != nil {
logger.Error(err, "get broker failed", "NodeID", brokerinfo.NodeID)
return nil
}
response, err := broker.RequestListGroups(h.clientID)
if err != nil {
logger.Error(err, "get group list failed", "broker", broker.GetAddress())
return nil
}
for _, g := range response.Groups {
groups = append(groups, g.GroupID)
}
}
return groups
}