Skip to content

Commit

Permalink
fix(agent,hass): fix logic around retrying websocket connection
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Oct 3, 2023
1 parent 14282ae commit 6603e06
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
7 changes: 3 additions & 4 deletions internal/agent/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ func (agent *Agent) runNotificationsWorker(ctx context.Context, options AgentOpt
wg.Add(1)
go func() {
defer wg.Done()
restartCh := make(chan struct{})
api.StartWebsocket(ctx, agent, notifyCh, restartCh)
for range restartCh {
log.Debug().Msg("Restarting websocket connection.")
for {
restartCh := make(chan struct{})
api.StartWebsocket(ctx, agent, notifyCh, restartCh)
<-restartCh
}
}()

Expand Down
10 changes: 5 additions & 5 deletions internal/hass/api/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ func StartWebsocket(ctx context.Context, settings Agent, notifyCh chan [2]string

retryFunc := func() error {
var resp *http.Response
socket, resp, err = gws.NewClient(newWebsocket(ctx, settings, notifyCh, doneCh), &gws.ClientOption{
Addr: websocketURL,
})
defer resp.Body.Close()
socket, resp, err = gws.NewClient(
newWebsocket(ctx, settings, notifyCh, doneCh),
&gws.ClientOption{Addr: websocketURL})
if err != nil {
log.Error().Err(err).
Msgf("Could not connect to websocket (%s).", resp.Status)
Msg("Could not connect to websocket.")
return err
}
defer resp.Body.Close()
return nil
}
err = backoff.Retry(retryFunc, backoff.WithContext(backoff.NewExponentialBackOff(), ctx))
Expand Down

0 comments on commit 6603e06

Please sign in to comment.