master
go 98 lines 1.93 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package rabbitmq
4
5 func newCache() *cache {
6 return &cache{
7 nodes: make(map[string]*nodeCacheItem),
8 vhosts: make(map[string]*vhostCacheItem),
9 queues: make(map[string]*queueCacheItem),
10 }
11 }
12
13 type (
14 cache struct {
15 overview struct{ hasCharts bool }
16 nodes map[string]*nodeCacheItem
17 vhosts map[string]*vhostCacheItem
18 queues map[string]*queueCacheItem
19 }
20 nodeCacheItem struct {
21 name string
22 seen bool
23 hasCharts bool
24 peers map[string]*peerCacheItem
25 }
26 peerCacheItem struct {
27 name string
28 node string
29 seen bool
30 hasCharts bool
31 }
32 vhostCacheItem struct {
33 name string
34 seen bool
35 hasCharts bool
36 }
37 queueCacheItem struct {
38 name string
39 node string
40 vhost string
41 typ string
42 seen bool
43 hasCharts bool
44 }
45 )
46
47 func (c *cache) resetSeen() {
48 for _, v := range c.nodes {
49 v.seen = false
50 for _, v := range v.peers {
51 v.seen = false
52 }
53 }
54 for _, v := range c.vhosts {
55 v.seen = false
56 }
57 for _, v := range c.queues {
58 v.seen = false
59 }
60 }
61
62 func (c *cache) getNode(node apiNodeResp) *nodeCacheItem {
63 v, ok := c.nodes[node.Name]
64 if !ok {
65 v = &nodeCacheItem{name: node.Name, peers: make(map[string]*peerCacheItem)}
66 c.nodes[node.Name] = v
67 }
68 return v
69 }
70
71 func (c *cache) getNodeClusterPeer(node apiNodeResp, peer apiClusterPeer) *peerCacheItem {
72 n := c.getNode(node)
73 v, ok := n.peers[peer.Name]
74 if !ok {
75 v = &peerCacheItem{node: node.Name, name: peer.Name}
76 n.peers[peer.Name] = v
77 }
78 return v
79 }
80
81 func (c *cache) getQueue(q apiQueueResp) *queueCacheItem {
82 key := q.Node + "_" + q.Vhost + "_" + q.Name
83 v, ok := c.queues[key]
84 if !ok {
85 v = &queueCacheItem{node: q.Node, name: q.Name, vhost: q.Vhost, typ: q.Type}
86 c.queues[key] = v
87 }
88 return v
89 }
90
91 func (c *cache) getVhost(vhost string) *vhostCacheItem {
92 v, ok := c.vhosts[vhost]
93 if !ok {
94 v = &vhostCacheItem{name: vhost}
95 c.vhosts[vhost] = v
96 }
97 return v
98 }