Skip to content

Commit

Permalink
bugfix: Race condition in stringvariables (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesread committed May 13, 2024
1 parent 1ab35fd commit 80083fe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
8 changes: 0 additions & 8 deletions internal/stringvariables/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@ func ReplaceEntityVars(prefix string, source string) string {
return source
}

func RemoveKeysThatStartWith(search string) {
for k, _ := range contents {
if strings.HasPrefix(k, search) {
delete(contents, k)
}
}
}

func GetEntities(entityTitle string) []string {
var ret []string

Expand Down
28 changes: 28 additions & 0 deletions internal/stringvariables/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ package stringvariables
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"strings"
"sync"
)

var (
Expand All @@ -21,15 +23,25 @@ var (
Name: "olivetin_sv_count",
Help: "The number entries in the sv map",
})

rwmutex = sync.RWMutex{}
)

func init() {
rwmutex.Lock()

contents = make(map[string]string)

rwmutex.Unlock()
}

func Get(key string) string {
rwmutex.RLock()

v, ok := contents[key]

rwmutex.RUnlock()

if !ok {
return ""
} else {
Expand All @@ -42,7 +54,23 @@ func GetAll() map[string]string {
}

func Set(key string, value string) {
rwmutex.Lock()

contents[key] = value

metricSvCount.Set(float64(len(contents)))

rwmutex.Unlock()
}

func RemoveKeysThatStartWith(search string) {
rwmutex.Lock()

for k, _ := range contents {
if strings.HasPrefix(k, search) {
delete(contents, k)
}
}

rwmutex.Unlock()
}

0 comments on commit 80083fe

Please sign in to comment.