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 { if peer.IsStatic {
peer = peer.StaticNode.ConvertToStaticNode() peer = peer.StaticNode.ConvertToStaticNode()
} }
node.Mutex.Lock() var nodeTags, peerTags map[models.TagID]struct{}
nodeTags := maps.Clone(node.Tags) if node.Mutex != nil {
node.Mutex.Unlock() node.Mutex.Lock()
peer.Mutex.Lock() nodeTags = maps.Clone(node.Tags)
peerTags := maps.Clone(peer.Tags) node.Mutex.Unlock()
peer.Mutex.Unlock() } else {
nodeTags = node.Tags
}
if peer.Mutex != nil {
peer.Mutex.Lock()
peerTags = maps.Clone(peer.Tags)
peer.Mutex.Unlock()
} else {
peerTags = peer.Tags
}
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)
@@ -662,12 +672,21 @@ func IsNodeAllowedToCommunicate(node, peer models.Node, checkDefaultPolicy bool)
if peer.IsStatic { if peer.IsStatic {
peer = peer.StaticNode.ConvertToStaticNode() peer = peer.StaticNode.ConvertToStaticNode()
} }
node.Mutex.Lock() var nodeTags, peerTags map[models.TagID]struct{}
nodeTags := maps.Clone(node.Tags) if node.Mutex != nil {
node.Mutex.Unlock() node.Mutex.Lock()
peer.Mutex.Lock() nodeTags = maps.Clone(node.Tags)
peerTags := maps.Clone(peer.Tags) node.Mutex.Unlock()
peer.Mutex.Unlock() } else {
nodeTags = node.Tags
}
if peer.Mutex != nil {
peer.Mutex.Lock()
peerTags = maps.Clone(peer.Tags)
peer.Mutex.Unlock()
} else {
peerTags = peer.Tags
}
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)

View File

@@ -820,11 +820,16 @@ func GetTagMapWithNodes() (tagNodesMap map[models.TagID][]models.Node) {
if nodeI.Tags == nil { if nodeI.Tags == nil {
continue continue
} }
nodeI.Mutex.RLock() if nodeI.Mutex != nil {
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() if nodeI.Mutex != nil {
nodeI.Mutex.RUnlock()
}
} }
return return
} }
@@ -836,11 +841,15 @@ func GetTagMapWithNodesByNetwork(netID models.NetworkID, withStaticNodes bool) (
if nodeI.Tags == nil { if nodeI.Tags == nil {
continue continue
} }
nodeI.Mutex.RLock() if nodeI.Mutex != nil {
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() if nodeI.Mutex != nil {
nodeI.Mutex.RUnlock()
}
} }
tagNodesMap["*"] = nodes tagNodesMap["*"] = nodes
if !withStaticNodes { if !withStaticNodes {

View File

@@ -1,5 +1,7 @@
package models package models
import "sync"
// ExtClient - struct for external clients // ExtClient - struct for external clients
type ExtClient struct { type ExtClient struct {
ClientID string `json:"clientid" bson:"clientid"` ClientID string `json:"clientid" bson:"clientid"`
@@ -55,5 +57,6 @@ func (ext *ExtClient) ConvertToStaticNode() Node {
Tags: ext.Tags, Tags: ext.Tags,
IsStatic: true, IsStatic: true,
StaticNode: *ext, StaticNode: *ext,
Mutex: &sync.RWMutex{},
} }
} }