Retry on 4xx errors

This commit is contained in:
Tulir Asokan 2023-08-09 15:27:47 +03:00
parent aa28b6bd38
commit 2116071b73

View file

@ -8,6 +8,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"net/http"
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
@ -75,7 +76,6 @@ func (c *Client) doDittoPinger(log *zerolog.Logger, dittoPing chan struct{}, sto
func (c *Client) doLongPoll(loggedIn bool) { func (c *Client) doLongPoll(loggedIn bool) {
c.listenID++ c.listenID++
listenID := c.listenID listenID := c.listenID
errored := true
listenReqID := uuid.NewString() listenReqID := uuid.NewString()
log := c.Logger.With().Int("listen_id", listenID).Logger() log := c.Logger.With().Int("listen_id", listenID).Logger()
@ -90,6 +90,7 @@ func (c *Client) doLongPoll(loggedIn bool) {
defer close(stopDittoPinger) defer close(stopDittoPinger)
go c.doDittoPinger(&log, dittoPing, stopDittoPinger) go c.doDittoPinger(&log, dittoPing, stopDittoPinger)
errorCount := 1
for c.listenID == listenID { for c.listenID == listenID {
err := c.refreshAuthToken() err := c.refreshAuthToken()
if err != nil { if err != nil {
@ -115,28 +116,33 @@ func (c *Client) doLongPoll(loggedIn bool) {
if loggedIn { if loggedIn {
c.triggerEvent(&events.ListenTemporaryError{Error: err}) c.triggerEvent(&events.ListenTemporaryError{Error: err})
} }
errored = true errorCount++
log.Err(err).Msg("Error making listen request, retrying in 5 seconds") sleepSeconds := (errorCount + 1) * 5
time.Sleep(5 * time.Second) log.Err(err).Int("sleep_seconds", sleepSeconds).Msg("Error making listen request, retrying in a while")
time.Sleep(time.Duration(sleepSeconds) * time.Second)
continue continue
} }
if resp.StatusCode >= 400 && resp.StatusCode < 500 { if resp.StatusCode == http.StatusUnauthorized || resp.StatusCode == http.StatusForbidden {
log.Error().Int("status_code", resp.StatusCode).Msg("Error making listen request") log.Error().Int("status_code", resp.StatusCode).Msg("Error making listen request")
if loggedIn { if loggedIn {
c.triggerEvent(&events.ListenFatalError{Error: events.HTTPError{Action: "polling", Resp: resp}}) c.triggerEvent(&events.ListenFatalError{Error: events.HTTPError{Action: "polling", Resp: resp}})
} }
return return
} else if resp.StatusCode >= 500 { } else if resp.StatusCode >= 400 {
if loggedIn { if loggedIn {
c.triggerEvent(&events.ListenTemporaryError{Error: events.HTTPError{Action: "polling", Resp: resp}}) c.triggerEvent(&events.ListenTemporaryError{Error: events.HTTPError{Action: "polling", Resp: resp}})
} }
errored = true errorCount++
log.Debug().Int("statusCode", resp.StatusCode).Msg("5xx error in long polling, retrying in 5 seconds") sleepSeconds := (errorCount + 1) * 5
time.Sleep(5 * time.Second) log.Debug().
Int("statusCode", resp.StatusCode).
Int("sleep_seconds", sleepSeconds).
Msg("Error in long polling, retrying in a while")
time.Sleep(time.Duration(sleepSeconds) * time.Second)
continue continue
} }
if errored { if errorCount > 0 {
errored = false errorCount = 0
if loggedIn { if loggedIn {
c.triggerEvent(&events.ListenRecovered{}) c.triggerEvent(&events.ListenRecovered{})
} }