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