Skip to content

Commit 8578368

Browse files
fix: Adds mutex to in-memory current store
1 parent 471cabc commit 8578368

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

currentStore.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"sync"
45
"time"
56

67
"github.com/google/uuid"
@@ -24,19 +25,27 @@ type current struct {
2425
// inMemoryCurrentStore stores point current values in a local in-memory cache.
2526
// These are not shared between instances.
2627
type inMemoryCurrentStore struct {
28+
mux *sync.Mutex
2729
cache map[uuid.UUID]current
2830
}
2931

3032
func newInMemoryCurrentStore() inMemoryCurrentStore {
31-
return inMemoryCurrentStore{cache: map[uuid.UUID]current{}}
33+
return inMemoryCurrentStore{
34+
mux: &sync.Mutex{},
35+
cache: map[uuid.UUID]current{},
36+
}
3237
}
3338

3439
func (s inMemoryCurrentStore) getCurrent(id uuid.UUID) current {
40+
s.mux.Lock()
41+
defer s.mux.Unlock()
3542
return s.cache[id]
3643
}
3744

3845
func (s inMemoryCurrentStore) setCurrent(id uuid.UUID, input currentInput) {
3946
timestamp := time.Now()
47+
s.mux.Lock()
48+
defer s.mux.Unlock()
4049
s.cache[id] = current{
4150
Ts: &timestamp,
4251
Value: input.Value,

0 commit comments

Comments
 (0)