From 23f214f4aaa1bea6908598af9db56705f43f80eb Mon Sep 17 00:00:00 2001 From: Joshua Rich Date: Thu, 15 Feb 2024 09:46:44 +1000 Subject: [PATCH] fix: :zap: only retry if the server is overloaded by default --- internal/agent/agent.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/agent/agent.go b/internal/agent/agent.go index 36d06e07f..e824be49a 100644 --- a/internal/agent/agent.go +++ b/internal/agent/agent.go @@ -7,6 +7,7 @@ package agent import ( "context" + "net/http" "os" "os/signal" "path/filepath" @@ -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 }