Skip to content

Commit ef3a3c3

Browse files
committed
refactor(store): Separate locks for flag data and metadata in store
Signed-off-by: Andrey <andreyturkov@google.com>
1 parent a99707b commit ef3a3c3

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

core/pkg/store/store.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ type IStore interface {
3131

3232
// store is the in-memory implementation of IStore. It should not be used directly by consumers.
3333
type store struct {
34-
mx sync.RWMutex
34+
flagMx sync.RWMutex
35+
metaMx sync.RWMutex
3536
db *memdb.MemDB
3637
logger *logger.Logger
3738
FlagSources []string
@@ -114,16 +115,15 @@ func (f *store) Get(_ context.Context, key string) (model.Flag, model.Metadata,
114115
}
115116

116117
func (f *store) SelectorForFlag(_ context.Context, flag model.Flag) string {
117-
f.mx.RLock()
118-
defer f.mx.RUnlock()
119-
118+
f.metaMx.RLock()
119+
defer f.metaMx.RUnlock()
120120
return f.SourceDetails[flag.Source].Selector
121121
}
122122

123123
func (f *store) String() (string, error) {
124124
f.logger.Debug("dumping flags to string")
125-
f.mx.RLock()
126-
defer f.mx.RUnlock()
125+
f.flagMx.RLock()
126+
defer f.flagMx.RUnlock()
127127

128128
state, _, err := f.GetAll(context.Background())
129129
if err != nil {
@@ -171,8 +171,11 @@ func (f *store) Update(
171171
defer txn.Abort()
172172
storedFlags, _, _ := f.GetAll(context.Background())
173173

174-
f.mx.Lock()
174+
f.metaMx.Lock()
175175
f.setSourceMetadata(source, metadata)
176+
f.metaMx.Unlock()
177+
178+
f.flagMx.Lock()
176179
for key, v := range storedFlags {
177180
if v.Source == source && v.Selector == selector {
178181
if _, ok := flags[key]; !ok {
@@ -198,7 +201,7 @@ func (f *store) Update(
198201
}
199202
}
200203
}
201-
f.mx.Unlock()
204+
f.flagMx.Unlock()
202205
for key, newFlag := range flags {
203206
newFlag.Source = source
204207
newFlag.Selector = selector
@@ -242,8 +245,8 @@ func (f *store) Update(
242245
}
243246

244247
func (f *store) GetMetadataForSource(source string) model.Metadata {
245-
f.mx.RLock()
246-
defer f.mx.RUnlock()
248+
f.metaMx.RLock()
249+
defer f.metaMx.RUnlock()
247250
perSource, ok := f.MetadataPerSource[source]
248251
if ok && perSource != nil {
249252
return maps.Clone(perSource)
@@ -252,8 +255,8 @@ func (f *store) GetMetadataForSource(source string) model.Metadata {
252255
}
253256

254257
func (f *store) SyncConfig(providers []syncpkg.SourceConfig) []string {
255-
f.mx.Lock()
256-
defer f.mx.Unlock()
258+
f.metaMx.Lock()
259+
defer f.metaMx.Unlock()
257260
f.FlagSources = nil
258261
f.SourceDetails = map[string]SourceDetails{}
259262
sources := make([]string, 0, len(providers))
@@ -270,8 +273,8 @@ func (f *store) SyncConfig(providers []syncpkg.SourceConfig) []string {
270273

271274
// TODO: this is a temporary solution to merge metadata in the case of error; properly handle it with https://github.com/open-feature/flagd/issues/1675
272275
func (f *store) getMetadata() model.Metadata {
273-
f.mx.RLock()
274-
defer f.mx.RUnlock()
276+
f.metaMx.RLock()
277+
defer f.metaMx.RUnlock()
275278
metadata := model.Metadata{}
276279
for _, perSource := range f.MetadataPerSource {
277280
for key, entry := range perSource {

0 commit comments

Comments
 (0)