mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-06 01:07:41 +08:00
add peer mutex
This commit is contained in:
@@ -575,12 +575,22 @@ func IsPeerAllowed(node, peer models.Node, checkDefaultPolicy bool) bool {
|
||||
if peer.IsStatic {
|
||||
peer = peer.StaticNode.ConvertToStaticNode()
|
||||
}
|
||||
var nodeTags, peerTags map[models.TagID]struct{}
|
||||
if node.Mutex != nil {
|
||||
node.Mutex.Lock()
|
||||
nodeTags := maps.Clone(node.Tags)
|
||||
nodeTags = maps.Clone(node.Tags)
|
||||
node.Mutex.Unlock()
|
||||
} else {
|
||||
nodeTags = node.Tags
|
||||
}
|
||||
if peer.Mutex != nil {
|
||||
peer.Mutex.Lock()
|
||||
peerTags := maps.Clone(peer.Tags)
|
||||
peerTags = maps.Clone(peer.Tags)
|
||||
peer.Mutex.Unlock()
|
||||
} else {
|
||||
peerTags = peer.Tags
|
||||
}
|
||||
|
||||
if checkDefaultPolicy {
|
||||
// check default policy if all allowed return true
|
||||
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
|
||||
@@ -662,12 +672,21 @@ func IsNodeAllowedToCommunicate(node, peer models.Node, checkDefaultPolicy bool)
|
||||
if peer.IsStatic {
|
||||
peer = peer.StaticNode.ConvertToStaticNode()
|
||||
}
|
||||
var nodeTags, peerTags map[models.TagID]struct{}
|
||||
if node.Mutex != nil {
|
||||
node.Mutex.Lock()
|
||||
nodeTags := maps.Clone(node.Tags)
|
||||
nodeTags = maps.Clone(node.Tags)
|
||||
node.Mutex.Unlock()
|
||||
} else {
|
||||
nodeTags = node.Tags
|
||||
}
|
||||
if peer.Mutex != nil {
|
||||
peer.Mutex.Lock()
|
||||
peerTags := maps.Clone(peer.Tags)
|
||||
peerTags = maps.Clone(peer.Tags)
|
||||
peer.Mutex.Unlock()
|
||||
} else {
|
||||
peerTags = peer.Tags
|
||||
}
|
||||
if checkDefaultPolicy {
|
||||
// check default policy if all allowed return true
|
||||
defaultPolicy, err := GetDefaultPolicy(models.NetworkID(node.Network), models.DevicePolicy)
|
||||
|
@@ -820,12 +820,17 @@ func GetTagMapWithNodes() (tagNodesMap map[models.TagID][]models.Node) {
|
||||
if nodeI.Tags == nil {
|
||||
continue
|
||||
}
|
||||
if nodeI.Mutex != nil {
|
||||
nodeI.Mutex.RLock()
|
||||
}
|
||||
for nodeTagID := range nodeI.Tags {
|
||||
tagNodesMap[nodeTagID] = append(tagNodesMap[nodeTagID], nodeI)
|
||||
}
|
||||
if nodeI.Mutex != nil {
|
||||
nodeI.Mutex.RUnlock()
|
||||
}
|
||||
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -836,12 +841,16 @@ func GetTagMapWithNodesByNetwork(netID models.NetworkID, withStaticNodes bool) (
|
||||
if nodeI.Tags == nil {
|
||||
continue
|
||||
}
|
||||
if nodeI.Mutex != nil {
|
||||
nodeI.Mutex.RLock()
|
||||
}
|
||||
for nodeTagID := range nodeI.Tags {
|
||||
tagNodesMap[nodeTagID] = append(tagNodesMap[nodeTagID], nodeI)
|
||||
}
|
||||
if nodeI.Mutex != nil {
|
||||
nodeI.Mutex.RUnlock()
|
||||
}
|
||||
}
|
||||
tagNodesMap["*"] = nodes
|
||||
if !withStaticNodes {
|
||||
return
|
||||
|
@@ -1,5 +1,7 @@
|
||||
package models
|
||||
|
||||
import "sync"
|
||||
|
||||
// ExtClient - struct for external clients
|
||||
type ExtClient struct {
|
||||
ClientID string `json:"clientid" bson:"clientid"`
|
||||
@@ -55,5 +57,6 @@ func (ext *ExtClient) ConvertToStaticNode() Node {
|
||||
Tags: ext.Tags,
|
||||
IsStatic: true,
|
||||
StaticNode: *ext,
|
||||
Mutex: &sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user