Switch to sync.Map because go maps are unsound
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Sarah Jamie Lewis 2023-04-05 19:30:00 -07:00
parent 2e59cc43ab
commit cccb97d5f0
1 changed files with 10 additions and 8 deletions

View File

@ -6,15 +6,20 @@ import "sync"
// examples of experiments include File Sharing, Profile Images and Groups. // examples of experiments include File Sharing, Profile Images and Groups.
type Experiments struct { type Experiments struct {
enabled bool enabled bool
experiments map[string]bool experiments sync.Map
lock sync.Mutex
} }
// InitExperiments encapsulates a set of experiments separate from their storage in GlobalSettings. // InitExperiments encapsulates a set of experiments separate from their storage in GlobalSettings.
func InitExperiments(enabled bool, experiments map[string]bool) Experiments { func InitExperiments(enabled bool, experiments map[string]bool) Experiments {
var syncExperiments sync.Map
for experiment, set := range experiments {
syncExperiments.Store(experiment, set)
}
return Experiments{ return Experiments{
enabled: enabled, enabled: enabled,
experiments: experiments, experiments: syncExperiments,
} }
} }
@ -28,12 +33,9 @@ func (e *Experiments) IsEnabled(experiment string) bool {
return false return false
} }
// go will sometimes panic if we do not lock this read-only map... enabled, exists := e.experiments.Load(experiment)
e.lock.Lock()
defer e.lock.Unlock()
enabled, exists := e.experiments[experiment]
if !exists { if !exists {
return false return false
} }
return enabled return enabled.(bool)
} }