Skip to content

Commit

Permalink
settings needed mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
Todd Blose committed Mar 26, 2015
1 parent fddc6fb commit 1c28a13
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/github.com/getlantern/flashlight/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ type Settings struct {
Version string
BuildDate string
AutoReport bool
mutex sync.Mutex
}

func Configure(cfg *config.Config, version, buildDate string) {

cfgMutex.Lock()
defer cfgMutex.Unlock()

// base settings are always written
baseSettings = &Settings{
Expand All @@ -45,11 +47,8 @@ func Configure(cfg *config.Config, version, buildDate string) {
err := start(baseSettings)
if err != nil {
log.Errorf("Unable to register settings service: %q", err)
return
}
go read()
}
cfgMutex.Unlock()
}

func start(baseSettings *Settings) error {
Expand All @@ -61,12 +60,14 @@ func start(baseSettings *Settings) error {
}

service, err = ui.Register(messageType, nil, helloFn)
go read()
return err
}

func read() {
for msg := range service.In {
settings := (msg).(map[string]interface{})
baseSettings.mutex.Lock()
config.Update(func(updated *config.Config) error {
autoReport := settings["autoReport"].(bool)
if autoReport {
Expand All @@ -78,5 +79,6 @@ func read() {
*updated.AutoReport = autoReport
return nil
})
baseSettings.mutex.Unlock()
}
}

0 comments on commit 1c28a13

Please sign in to comment.