Skip to content

Commit

Permalink
Merge pull request #50 from mimiro-io/fix/panic-concurrent-map-read-w…
Browse files Browse the repository at this point in the history
…rite

Add RWMutex to avoid concurrent read write to map
  • Loading branch information
4hlberg authored Sep 1, 2023
2 parents 1c0c153 + f109fe0 commit 4a222bb
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions internal/store/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/mimiro-io/objectstorage-datalayer/internal/conf"
"go.uber.org/zap"
"strings"
"sync"
)

type StorageEngine struct {
Expand All @@ -14,6 +15,7 @@ type StorageEngine struct {
storages map[string]storageState
mngr *conf.ConfigurationManager
env *conf.Env
lock *sync.RWMutex
}

type storageState struct {
Expand All @@ -28,6 +30,7 @@ func NewStorageEngine(logger *zap.SugaredLogger, config *conf.ConfigurationManag
logger: logger.Named("storage"),
env: env,
storages: make(map[string]storageState),
lock: &sync.RWMutex{},
}
}

Expand All @@ -39,16 +42,17 @@ func (engine *StorageEngine) Storage(datasetName string) (StorageInterface, erro
}

var state storageState

engine.lock.Lock()
defer engine.lock.Unlock()
if s, ok := engine.storages[datasetName]; ok {
storage, err := engine.initBackend(engine.mngr.Datalayer.StorageMapping[datasetName])
engine.logger.Debug(storage)
if err != nil {
return nil, err
}

engine.storages[datasetName] = s
state = s

} else {
storage, err := engine.initBackend(engine.mngr.Datalayer.StorageMapping[datasetName])
if err != nil {
Expand All @@ -61,12 +65,13 @@ func (engine *StorageEngine) Storage(datasetName string) (StorageInterface, erro
engine.storages[datasetName] = s
state = s
}

return state.storage, nil
}

// Close handles cleanup of storage engines, if needed
func (engine *StorageEngine) Close(datasetName string) {
engine.lock.Lock()
defer engine.lock.Unlock()
if s, ok := engine.storages[datasetName]; ok {
s.isRunning = false
}
Expand Down

0 comments on commit 4a222bb

Please sign in to comment.