Skip to content

Commit

Permalink
Merge pull request #105 from moul/fix/gfanton/store-concurrency
Browse files Browse the repository at this point in the history
fix(store): Fix store concurrency
  • Loading branch information
moul authored Sep 12, 2018
2 parents 7d8c6c4 + 5b8733f commit ac2e25e
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"regexp"
"strings"
"sync"
"text/template"

"github.com/Masterminds/sprig"
Expand All @@ -22,13 +23,6 @@ var (
registry *ggdescriptor.Registry // some helpers need access to registry
)

// Utility to store some vars across multiple scope
var store = make(map[string]interface{})

func SetRegistry(reg *ggdescriptor.Registry) {
registry = reg
}

var ProtoHelpersFuncMap = template.FuncMap{
"string": func(i interface {
String() string
Expand Down Expand Up @@ -166,19 +160,50 @@ var ProtoHelpersFuncMap = template.FuncMap{

var pathMap map[interface{}]*descriptor.SourceCodeInfo_Location

func setStore(key string, i interface{}) string {
store[key] = i
return ""
var store = newStore()

// Utility to store some vars across multiple scope
type globalStore struct {
store map[string]interface{}
mu sync.Mutex
}

func newStore() *globalStore {
return &globalStore{
store: make(map[string]interface{}),
}
}

func getStore(s string) interface{} {
if v, ok := store[s]; ok {
func (s *globalStore) getData(key string) interface{} {
s.mu.Lock()
defer s.mu.Unlock()

if v, ok := s.store[key]; ok {
return v
}

return false
}

func (s *globalStore) setData(key string, o interface{}) {
s.mu.Lock()
s.store[key] = o
s.mu.Unlock()
}

func setStore(key string, o interface{}) string {
store.setData(key, o)
return ""
}

func getStore(key string) interface{} {
return store.getData(key)
}

func SetRegistry(reg *ggdescriptor.Registry) {
registry = reg
}

func InitPathMap(file *descriptor.FileDescriptorProto) {
pathMap = make(map[interface{}]*descriptor.SourceCodeInfo_Location)
addToPathMap(file.GetSourceCodeInfo(), file, []int32{})
Expand Down

0 comments on commit ac2e25e

Please sign in to comment.