Skip to content

Commit

Permalink
fix: ⚡ only retry if the server is overloaded by default
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuar committed Feb 16, 2024
1 parent 9b73cd8 commit 23f214f
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion internal/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package agent

import (
"context"
"net/http"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -181,7 +182,15 @@ func (agent *Agent) Stop() {
func setupContext(prefs *preferences.Preferences) (context.Context, context.CancelFunc) {
baseCtx, cancelFunc := context.WithCancel(context.Background())
agentCtx := hass.ContextSetURL(baseCtx, prefs.RestAPIURL)
r := resty.New().SetTimeout(1 * time.Second)
r := resty.New().
SetTimeout(1 * time.Second).
AddRetryCondition(
// RetryConditionFunc type is for retry condition function
// input: non-nil Response OR request execution error
func(r *resty.Response, err error) bool {
return r.StatusCode() == http.StatusTooManyRequests
},
)
agentCtx = hass.ContextSetClient(agentCtx, r)
return agentCtx, cancelFunc
}

0 comments on commit 23f214f

Please sign in to comment.