Merge pull request 'Switch to sync.Map because go maps are unsound' (#504) from fixpanic into master
continuous-integration/drone/push Build is pending
Details
continuous-integration/drone/push Build is pending
Details
Reviewed-on: #504 Reviewed-by: Dan Ballard <dan@openprivacy.ca>
This commit is contained in:
commit
28ddbcc132
|
@ -6,15 +6,20 @@ import "sync"
|
|||
// examples of experiments include File Sharing, Profile Images and Groups.
|
||||
type Experiments struct {
|
||||
enabled bool
|
||||
experiments map[string]bool
|
||||
lock sync.Mutex
|
||||
experiments sync.Map
|
||||
}
|
||||
|
||||
// InitExperiments encapsulates a set of experiments separate from their storage in GlobalSettings.
|
||||
func InitExperiments(enabled bool, experiments map[string]bool) Experiments {
|
||||
|
||||
var syncExperiments sync.Map
|
||||
for experiment, set := range experiments {
|
||||
syncExperiments.Store(experiment, set)
|
||||
}
|
||||
|
||||
return Experiments{
|
||||
enabled: enabled,
|
||||
experiments: experiments,
|
||||
experiments: syncExperiments,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -28,12 +33,9 @@ func (e *Experiments) IsEnabled(experiment string) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// go will sometimes panic if we do not lock this read-only map...
|
||||
e.lock.Lock()
|
||||
defer e.lock.Unlock()
|
||||
enabled, exists := e.experiments[experiment]
|
||||
enabled, exists := e.experiments.Load(experiment)
|
||||
if !exists {
|
||||
return false
|
||||
}
|
||||
return enabled
|
||||
return enabled.(bool)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue