add peer mutex

This commit is contained in:
abhishek9686
2025-02-19 23:04:50 +04:00
parent 92698363cd
commit ce50b965d4
3 changed files with 47 additions and 16 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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{},
}
}