From d49b146eaabf15dc12d3e7d497216a48c1c614a8 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Fri, 4 Sep 2020 20:08:31 +1000 Subject: [PATCH] Add mutex to store --- runtime/store.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/runtime/store.go b/runtime/store.go index 9620fec8..fd84cb50 100644 --- a/runtime/store.go +++ b/runtime/store.go @@ -8,6 +8,7 @@ import ( "fmt" "log" "reflect" + "sync" ) // Options defines the optional data that may be used @@ -48,6 +49,9 @@ type Store struct { runtime *Runtime notifySynchronously bool + // Lock + mux sync.Mutex + // Error handler errorHandler func(error) } @@ -95,6 +99,9 @@ func (s *Store) processUpdatedData(data string) error { return err } + // Lock mutex for writing + s.mux.Lock() + // Handle nulls if newData == nil { s.data = reflect.Zero(s.dataType) @@ -103,6 +110,9 @@ func (s *Store) processUpdatedData(data string) error { s.data = reflect.ValueOf(newData).Elem() } + // Unlock mutex + s.mux.Unlock() + return nil } @@ -153,7 +163,9 @@ func (s *Store) Set(data interface{}) error { } // Save data + s.mux.Lock() s.data = reflect.ValueOf(data) + s.mux.Unlock() // Stringify data newdata, err := json.Marshal(data)