From a5e35b47119feb5331aa8e19aedcf7caf66ed161 Mon Sep 17 00:00:00 2001 From: gx578007 Date: Sun, 5 Mar 2023 22:20:14 +0800 Subject: [PATCH] FIX: add mutex in memory store --- pkg/service/memory.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/pkg/service/memory.go b/pkg/service/memory.go index 92ee9f6cd..978b3d2ca 100644 --- a/pkg/service/memory.go +++ b/pkg/service/memory.go @@ -3,6 +3,7 @@ package service import ( "reflect" "strings" + "sync" ) type MemoryService struct { @@ -26,14 +27,21 @@ func (s *MemoryService) NewStore(id string, subIDs ...string) Store { type MemoryStore struct { Key string memory *MemoryService + mu sync.Mutex } func (store *MemoryStore) Save(val interface{}) error { + store.mu.Lock() + defer store.mu.Unlock() + store.memory.Slots[store.Key] = val return nil } func (store *MemoryStore) Load(val interface{}) error { + store.mu.Lock() + defer store.mu.Unlock() + v := reflect.ValueOf(val) if data, ok := store.memory.Slots[store.Key]; ok { dataRV := reflect.ValueOf(data) @@ -46,6 +54,9 @@ func (store *MemoryStore) Load(val interface{}) error { } func (store *MemoryStore) Reset() error { + store.mu.Lock() + defer store.mu.Unlock() + delete(store.memory.Slots, store.Key) return nil -} +} \ No newline at end of file