Skip to content

Commit

Permalink
sleep if POST to hub returns 401
Browse files Browse the repository at this point in the history
  • Loading branch information
martinlindhe committed May 10, 2021
1 parent 57828d3 commit 40d0368
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
11 changes: 10 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type cleanupCommand struct {
var (
ErrHubTooManyRequests = errors.New("Hub replied with a 429 error code")
ErrHubServerError = errors.New("Hub replied with a 5xx error code")
ErrHubUnauthorized = errors.New("Hub replied with a 401 error code")
)

func (c *cleanupCommand) AddStep(f func() error) {
Expand Down Expand Up @@ -79,6 +80,14 @@ func (ca *Cagent) Run(outputFile *os.File, interrupt chan struct{}) {
// for error code 429, wait 10 seconds and try again
retryIn = 10 * time.Second
log.Infof("Run: HTTP 429, too many requests, retrying in %v", retryIn)
} else if err == ErrHubUnauthorized {
// increase sleep time by 30 seconds until it is 1 hour
if ca.Config.Sleep < 60*60 {
ca.Config.Sleep += 30
}
retries = 0
retryIn = time.Duration(ca.Config.Sleep) * time.Second
log.Infof("Run: failed to send measurements to hub. unable to authorize with provided Hub credentials (HTTP 401). waiting %v seconds until next attempt", ca.Config.Sleep)
} else if err == ErrHubServerError {
// for error codes 5xx, wait for configured amount of time and try again
retryIn = time.Duration(ca.Config.OnHTTP5xxRetryInterval) * time.Second
Expand Down Expand Up @@ -289,7 +298,7 @@ func (ca *Cagent) reportMeasurements(measurements common.MeasurementsMap, output

err := ca.PostResultToHub(ctx, result)
if err != nil {
if err == ErrHubTooManyRequests || err == ErrHubServerError {
if err == ErrHubTooManyRequests || err == ErrHubServerError || err == ErrHubUnauthorized {
return err
}
err = errors.Wrap(err, "failed to POST measurement result to Hub")
Expand Down
3 changes: 3 additions & 0 deletions hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ func (ca *Cagent) PostResultToHub(ctx context.Context, result *Result) error {
if resp.StatusCode == http.StatusTooManyRequests {
return ErrHubTooManyRequests
}
if resp.StatusCode == http.StatusUnauthorized {
return ErrHubUnauthorized
}
if resp.StatusCode >= 500 && resp.StatusCode <= 599 {
return ErrHubServerError
}
Expand Down

0 comments on commit 40d0368

Please sign in to comment.