| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package nats |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | ) |
| 8 | |
| 9 | func newCache() *cache { |
| 10 | return &cache{ |
| 11 | accounts: make(accCache), |
| 12 | routes: make(routeCache), |
| 13 | inGateways: make(gwCache), |
| 14 | outGateways: make(gwCache), |
| 15 | leafs: make(leafCache), |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | type cache struct { |
| 20 | accounts accCache |
| 21 | routes routeCache |
| 22 | inGateways gwCache |
| 23 | outGateways gwCache |
| 24 | leafs leafCache |
| 25 | } |
| 26 | |
| 27 | func (c *cache) resetUpdated() { |
| 28 | for _, gw := range c.inGateways { |
| 29 | gw.updated = false |
| 30 | for _, conn := range gw.conns { |
| 31 | conn.updated = false |
| 32 | } |
| 33 | } |
| 34 | for _, gw := range c.outGateways { |
| 35 | gw.updated = false |
| 36 | for _, conn := range gw.conns { |
| 37 | conn.updated = false |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | type ( |
| 43 | accCache map[string]*accCacheEntry |
| 44 | accCacheEntry struct { |
| 45 | hasCharts bool |
| 46 | updated bool |
| 47 | |
| 48 | accName string |
| 49 | } |
| 50 | ) |
| 51 | |
| 52 | func (c *accCache) put(ai accountInfo) { |
| 53 | acc, ok := (*c)[ai.Account] |
| 54 | if !ok { |
| 55 | acc = &accCacheEntry{accName: ai.Account} |
| 56 | (*c)[ai.Account] = acc |
| 57 | } |
| 58 | acc.updated = true |
| 59 | } |
| 60 | |
| 61 | type ( |
| 62 | routeCache map[uint64]*routeCacheEntry |
| 63 | routeCacheEntry struct { |
| 64 | hasCharts bool |
| 65 | updated bool |
| 66 | |
| 67 | rid uint64 |
| 68 | remoteId string |
| 69 | } |
| 70 | ) |
| 71 | |
| 72 | func (c *routeCache) put(ri routeInfo) { |
| 73 | route, ok := (*c)[ri.Rid] |
| 74 | if !ok { |
| 75 | route = &routeCacheEntry{rid: ri.Rid, remoteId: ri.RemoteID} |
| 76 | (*c)[ri.Rid] = route |
| 77 | } |
| 78 | route.updated = true |
| 79 | } |
| 80 | |
| 81 | type ( |
| 82 | gwCache map[string]*gwCacheEntry |
| 83 | gwCacheEntry struct { |
| 84 | hasCharts bool |
| 85 | updated bool |
| 86 | |
| 87 | gwName string |
| 88 | rgwName string |
| 89 | conns map[uint64]*gwConnCacheEntry |
| 90 | } |
| 91 | gwConnCacheEntry struct { |
| 92 | hasCharts bool |
| 93 | updated bool |
| 94 | |
| 95 | gwName string |
| 96 | rgwName string |
| 97 | cid uint64 |
| 98 | } |
| 99 | ) |
| 100 | |
| 101 | func (c *gwCache) put(gwName, rgwName string, rgi *remoteGatewayInfo) { |
| 102 | gw, ok := (*c)[gwName] |
| 103 | if !ok { |
| 104 | gw = &gwCacheEntry{gwName: gwName, rgwName: rgwName, conns: make(map[uint64]*gwConnCacheEntry)} |
| 105 | (*c)[gwName] = gw |
| 106 | } |
| 107 | gw.updated = true |
| 108 | |
| 109 | conn, ok := gw.conns[rgi.Connection.Cid] |
| 110 | if !ok { |
| 111 | conn = &gwConnCacheEntry{gwName: gwName, rgwName: rgwName, cid: rgi.Connection.Cid} |
| 112 | gw.conns[rgi.Connection.Cid] = conn |
| 113 | } |
| 114 | conn.updated = true |
| 115 | } |
| 116 | |
| 117 | type ( |
| 118 | leafCache map[string]*leafCacheEntry |
| 119 | leafCacheEntry struct { |
| 120 | hasCharts bool |
| 121 | updated bool |
| 122 | |
| 123 | leafName string |
| 124 | account string |
| 125 | ip string |
| 126 | port int |
| 127 | } |
| 128 | ) |
| 129 | |
| 130 | func (c *leafCache) put(li leafInfo) { |
| 131 | key := fmt.Sprintf("%s_%s_%s_%d", li.Name, li.Account, li.IP, li.Port) |
| 132 | leaf, ok := (*c)[key] |
| 133 | if !ok { |
| 134 | leaf = &leafCacheEntry{leafName: li.Name, account: li.Account, ip: li.IP, port: li.Port} |
| 135 | (*c)[key] = leaf |
| 136 | } |
| 137 | leaf.updated = true |
| 138 | } |