Skip to content

Commit

Permalink
add cache for network nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
yabinma committed Nov 12, 2024
1 parent b9b04dd commit 8340cb0
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions logic/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"encoding/json"
"errors"
"fmt"
"maps"
"net"
"slices"
"sort"
"sync"
"time"
Expand All @@ -24,8 +26,10 @@ import (
)

var (
nodeCacheMutex = &sync.RWMutex{}
nodesCacheMap = make(map[string]models.Node)
nodeCacheMutex = &sync.RWMutex{}
nodeNetworkCacheMutex = &sync.RWMutex{}
nodesCacheMap = make(map[string]models.Node)
nodesNetworkCacheMap = make(map[string]map[string]models.Node)
)

func getNodeFromCache(nodeID string) (node models.Node, ok bool) {
Expand All @@ -48,6 +52,20 @@ func deleteNodeFromCache(nodeID string) {
delete(nodesCacheMap, nodeID)
nodeCacheMutex.Unlock()
}
func deleteNodeFromNetworkCache(nodeID string, network string) {
nodeNetworkCacheMutex.Lock()
delete(nodesNetworkCacheMap[network], nodeID)
nodeNetworkCacheMutex.Unlock()
}

func storeNodeInNetworkCache(node models.Node, network string) {
nodeNetworkCacheMutex.Lock()
if nodesNetworkCacheMap[network] == nil {
nodesNetworkCacheMap[network] = make(map[string]models.Node)
}
nodesNetworkCacheMap[network][node.ID.String()] = node
nodeNetworkCacheMutex.Unlock()
}

func storeNodeInCache(node models.Node) {
nodeCacheMutex.Lock()
Expand Down Expand Up @@ -77,6 +95,11 @@ const (

// GetNetworkNodes - gets the nodes of a network
func GetNetworkNodes(network string) ([]models.Node, error) {
nodeNetworkCacheMutex.Lock()
defer nodeNetworkCacheMutex.Unlock()
if networkNodes, ok := nodesNetworkCacheMap[network]; ok {
return slices.Collect(maps.Values(networkNodes)), nil
}
allnodes, err := GetAllNodes()
if err != nil {
return []models.Node{}, err
Expand All @@ -99,6 +122,11 @@ func GetHostNodes(host *models.Host) []models.Node {

// GetNetworkNodesMemory - gets all nodes belonging to a network from list in memory
func GetNetworkNodesMemory(allNodes []models.Node, network string) []models.Node {
nodeNetworkCacheMutex.Lock()
defer nodeNetworkCacheMutex.Unlock()
if networkNodes, ok := nodesNetworkCacheMap[network]; ok {
return slices.Collect(maps.Values(networkNodes))
}
var nodes = []models.Node{}
for i := range allNodes {
node := allNodes[i]
Expand All @@ -123,6 +151,7 @@ func UpdateNodeCheckin(node *models.Node) error {
}
if servercfg.CacheEnabled() {
storeNodeInCache(*node)
storeNodeInNetworkCache(*node, node.Network)
}
return nil
}
Expand All @@ -140,6 +169,7 @@ func UpsertNode(newNode *models.Node) error {
}
if servercfg.CacheEnabled() {
storeNodeInCache(*newNode)
storeNodeInNetworkCache(*newNode, newNode.Network)
}
return nil
}
Expand Down Expand Up @@ -179,6 +209,7 @@ func UpdateNode(currentNode *models.Node, newNode *models.Node) error {
}
if servercfg.CacheEnabled() {
storeNodeInCache(*newNode)
storeNodeInNetworkCache(*newNode, newNode.Network)
}
return nil
}
Expand Down Expand Up @@ -288,6 +319,7 @@ func DeleteNodeByID(node *models.Node) error {
}
if servercfg.CacheEnabled() {
deleteNodeFromCache(node.ID.String())
deleteNodeFromNetworkCache(node.ID.String(), node.Network)
}
if servercfg.IsDNSMode() {
SetDNS()
Expand Down Expand Up @@ -459,6 +491,7 @@ func GetNodeByID(uuid string) (models.Node, error) {
}
if servercfg.CacheEnabled() {
storeNodeInCache(node)
storeNodeInNetworkCache(node, node.Network)
}
return node, nil
}
Expand Down Expand Up @@ -612,6 +645,7 @@ func createNode(node *models.Node) error {
}
if servercfg.CacheEnabled() {
storeNodeInCache(*node)
storeNodeInNetworkCache(*node, node.Network)
}
if _, ok := allocatedIpMap[node.Network]; ok {
if node.Address.IP != nil {
Expand Down

0 comments on commit 8340cb0

Please sign in to comment.