Skip to content

Commit

Permalink
Merge pull request #7 from ZoeySimone/429_retry
Browse files Browse the repository at this point in the history
Add retry if status code is 429 Too Many Requests
  • Loading branch information
eamonnotoole authored Aug 12, 2021
2 parents 016be6a + ba4b959 commit 0851ef8
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions pkg/token/issuertoken/issuertoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ import (
"net/http"
"net/url"
"strings"
"time"

"github.com/hpe-hcss/errors/pkg/errors"
)

const (
retryLimit = 3
)

type TokenResponse struct {
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
Expand All @@ -29,12 +34,14 @@ func doRetries(call func() (*http.Response, error), retries int) (*http.Response
return nil, err
}

if resp.StatusCode != http.StatusInternalServerError || retries == 0 {
if !isStatusRetryable(resp.StatusCode) || retries == 0 {
break
}

time.Sleep(3 * time.Second)
retries--
}

return resp, nil
}

Expand All @@ -54,7 +61,7 @@ func GenerateIssuerToken(ctx context.Context, issuerURL, clientID, clientSecret

resp, err := doRetries(func() (*http.Response, error) {
return http.DefaultClient.Do(req)
}, 1)
}, retryLimit)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -103,3 +110,11 @@ func GenerateIssuerToken(ctx context.Context, issuerURL, clientID, clientSecret

return token.AccessToken, nil
}

func isStatusRetryable(statusCode int) bool {
if statusCode == http.StatusInternalServerError || statusCode == http.StatusTooManyRequests {
return true
}

return false
}

0 comments on commit 0851ef8

Please sign in to comment.