Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add mutex to tag operations #3134

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions logic/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading