From 6c107246221d2ae9320989bd815fad3e93113849 Mon Sep 17 00:00:00 2001 From: abhishek9686 Date: Sun, 22 Sep 2024 18:24:48 +0400 Subject: [PATCH] add mutex to tag operations --- logic/tags.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/logic/tags.go b/logic/tags.go index 7436aa266..769d0c036 100644 --- a/logic/tags.go +++ b/logic/tags.go @@ -28,6 +28,8 @@ func GetTag(tagID models.TagID) (models.Tag, error) { // InsertTag - creates new tag func InsertTag(tag models.Tag) error { + tagMutex.Lock() + defer tagMutex.Unlock() _, err := database.FetchRecord(database.TAG_TABLE_NAME, tag.ID.String()) if err == nil { return fmt.Errorf("tag `%s` exists already", tag.ID) @@ -41,6 +43,8 @@ func InsertTag(tag models.Tag) error { // DeleteTag - delete tag, will also untag hosts func DeleteTag(tagID models.TagID) error { + tagMutex.Lock() + defer tagMutex.Unlock() // cleanUp tags on hosts tag, err := GetTag(tagID) if err != nil { @@ -62,8 +66,6 @@ func DeleteTag(tagID models.TagID) error { // ListTagsWithHosts - lists all tags with tagged hosts func ListTagsWithNodes(netID models.NetworkID) ([]models.TagListResp, error) { - tagMutex.RLock() - defer tagMutex.RUnlock() tags, err := ListNetworkTags(netID) if err != nil { return []models.TagListResp{}, err @@ -83,7 +85,8 @@ func ListTagsWithNodes(netID models.NetworkID) ([]models.TagListResp, error) { // ListTags - lists all tags from DB func ListTags() ([]models.Tag, error) { - + tagMutex.RLock() + defer tagMutex.RUnlock() data, err := database.FetchRecords(database.TAG_TABLE_NAME) if err != nil && !database.IsEmptyRecord(err) { return []models.Tag{}, err @@ -102,7 +105,8 @@ func ListTags() ([]models.Tag, error) { // ListTags - lists all tags from DB func ListNetworkTags(netID models.NetworkID) ([]models.Tag, error) { - + tagMutex.RLock() + defer tagMutex.RUnlock() data, err := database.FetchRecords(database.TAG_TABLE_NAME) if err != nil && !database.IsEmptyRecord(err) { return []models.Tag{}, err