Skip to content

Commit

Permalink
Add mutex to store
Browse files Browse the repository at this point in the history
  • Loading branch information
leaanthony committed Sep 4, 2020
1 parent 0474f15 commit d49b146
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions runtime/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"log"
"reflect"
"sync"
)

// Options defines the optional data that may be used
Expand Down Expand Up @@ -48,6 +49,9 @@ type Store struct {
runtime *Runtime
notifySynchronously bool

// Lock
mux sync.Mutex

// Error handler
errorHandler func(error)
}
Expand Down Expand Up @@ -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)
Expand All @@ -103,6 +110,9 @@ func (s *Store) processUpdatedData(data string) error {
s.data = reflect.ValueOf(newData).Elem()
}

// Unlock mutex
s.mux.Unlock()

return nil
}

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d49b146

Please sign in to comment.