Skip to content

Commit

Permalink
refactor(agent): use sync.Once for initial config loading
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Jun 14, 2023
1 parent 9af1984 commit e163fb2
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,22 @@ func Run(appID string) {
// Try to load the app config. If it is not valid, start a new registration
// process. Keep trying until we successfully register with HA or the user
// quits.
var configWg sync.WaitGroup
configWg.Add(1)
var configOnce sync.Once
go func() {
defer configWg.Done()
agent.CheckConfig(agentCtx, agent.requestRegistrationInfoUI)
configOnce.Do(func() {
agent.CheckConfig(agentCtx, agent.requestRegistrationInfoUI)
})
}()

// Wait for the config to load, then start the sensor tracker and
// notifications worker
var workerWg sync.WaitGroup
defer workerWg.Done()
go func() {
configWg.Wait()
configOnce.Do(func() {
agent.CheckConfig(agentCtx, agent.requestRegistrationInfoUI)
})

appConfig := agent.LoadConfig()
agentWorkerCtx := config.StoreInContext(agentCtx, appConfig)
hassConfig = hass.NewHassConfig(agentWorkerCtx)
Expand Down

0 comments on commit e163fb2

Please sign in to comment.