mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-08 14:15:25 +08:00
add node mutex to model
This commit is contained in:
parent
7dd4c048c3
commit
92698363cd
3 changed files with 25 additions and 7 deletions
|
@ -17,7 +17,6 @@ import (
|
|||
var (
|
||||
aclCacheMutex = &sync.RWMutex{}
|
||||
aclCacheMap = make(map[string]models.Acl)
|
||||
aclTagsMutex = &sync.RWMutex{}
|
||||
)
|
||||
|
||||
func MigrateAclPolicies() {
|
||||
|
@ -576,10 +575,12 @@ func IsPeerAllowed(node, peer models.Node, checkDefaultPolicy bool) bool {
|
|||
if peer.IsStatic {
|
||||
peer = peer.StaticNode.ConvertToStaticNode()
|
||||
}
|
||||
aclTagsMutex.RLock()
|
||||
peerTags := maps.Clone(peer.Tags)
|
||||
node.Mutex.Lock()
|
||||
nodeTags := maps.Clone(node.Tags)
|
||||
aclTagsMutex.RUnlock()
|
||||
node.Mutex.Unlock()
|
||||
peer.Mutex.Lock()
|
||||
peerTags := maps.Clone(peer.Tags)
|
||||
peer.Mutex.Unlock()
|
||||
if checkDefaultPolicy {
|
||||
// check default policy if all allowed return true
|
||||
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
|
||||
|
@ -661,10 +662,12 @@ func IsNodeAllowedToCommunicate(node, peer models.Node, checkDefaultPolicy bool)
|
|||
if peer.IsStatic {
|
||||
peer = peer.StaticNode.ConvertToStaticNode()
|
||||
}
|
||||
aclTagsMutex.RLock()
|
||||
peerTags := maps.Clone(peer.Tags)
|
||||
node.Mutex.Lock()
|
||||
nodeTags := maps.Clone(node.Tags)
|
||||
aclTagsMutex.RUnlock()
|
||||
node.Mutex.Unlock()
|
||||
peer.Mutex.Lock()
|
||||
peerTags := maps.Clone(peer.Tags)
|
||||
peer.Mutex.Unlock()
|
||||
if checkDefaultPolicy {
|
||||
// check default policy if all allowed return true
|
||||
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
|
||||
|
|
|
@ -35,12 +35,18 @@ var (
|
|||
func getNodeFromCache(nodeID string) (node models.Node, ok bool) {
|
||||
nodeCacheMutex.RLock()
|
||||
node, ok = nodesCacheMap[nodeID]
|
||||
if node.Mutex == nil {
|
||||
node.Mutex = &sync.RWMutex{}
|
||||
}
|
||||
nodeCacheMutex.RUnlock()
|
||||
return
|
||||
}
|
||||
func getNodesFromCache() (nodes []models.Node) {
|
||||
nodeCacheMutex.RLock()
|
||||
for _, node := range nodesCacheMap {
|
||||
if node.Mutex == nil {
|
||||
node.Mutex = &sync.RWMutex{}
|
||||
}
|
||||
nodes = append(nodes, node)
|
||||
}
|
||||
nodeCacheMutex.RUnlock()
|
||||
|
@ -425,6 +431,9 @@ func GetAllNodes() ([]models.Node, error) {
|
|||
}
|
||||
// add node to our array
|
||||
nodes = append(nodes, node)
|
||||
if node.Mutex == nil {
|
||||
node.Mutex = &sync.RWMutex{}
|
||||
}
|
||||
nodesMap[node.ID.String()] = node
|
||||
}
|
||||
|
||||
|
@ -811,9 +820,11 @@ func GetTagMapWithNodes() (tagNodesMap map[models.TagID][]models.Node) {
|
|||
if nodeI.Tags == nil {
|
||||
continue
|
||||
}
|
||||
nodeI.Mutex.RLock()
|
||||
for nodeTagID := range nodeI.Tags {
|
||||
tagNodesMap[nodeTagID] = append(tagNodesMap[nodeTagID], nodeI)
|
||||
}
|
||||
nodeI.Mutex.RUnlock()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
@ -825,9 +836,11 @@ func GetTagMapWithNodesByNetwork(netID models.NetworkID, withStaticNodes bool) (
|
|||
if nodeI.Tags == nil {
|
||||
continue
|
||||
}
|
||||
nodeI.Mutex.RLock()
|
||||
for nodeTagID := range nodeI.Tags {
|
||||
tagNodesMap[nodeTagID] = append(tagNodesMap[nodeTagID], nodeI)
|
||||
}
|
||||
nodeI.Mutex.RUnlock()
|
||||
}
|
||||
tagNodesMap["*"] = nodes
|
||||
if !withStaticNodes {
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"math/rand"
|
||||
"net"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
@ -117,6 +118,7 @@ type Node struct {
|
|||
IsUserNode bool `json:"is_user_node"`
|
||||
StaticNode ExtClient `json:"static_node"`
|
||||
Status NodeStatus `json:"node_status"`
|
||||
Mutex *sync.RWMutex `json:"-"`
|
||||
}
|
||||
|
||||
// LegacyNode - legacy struct for node model
|
||||
|
|
Loading…
Add table
Reference in a new issue