Skip to content

Commit

Permalink
Add retry on 502 return code
Browse files Browse the repository at this point in the history
In this PR we add the 502 (Bad Gateway) to the list of http return codes
that will result in a retry.  This is to alleviate errors seen in some
runtime environments.

Signed-off-by: Eamonn O'Toole <[email protected]>
  • Loading branch information
eamonnotoole committed Oct 9, 2024
1 parent b8c4e76 commit a832630
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 3 additions & 2 deletions pkg/token/token-util/token-util.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// (C) Copyright 2021-2023 Hewlett Packard Enterprise Development LP
// (C) Copyright 2021-2024 Hewlett Packard Enterprise Development LP

package tokenutil

Expand Down Expand Up @@ -134,7 +134,8 @@ func ManageHTTPErrorCodes(resp *http.Response, clientID string) error {
}

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

Expand Down
15 changes: 12 additions & 3 deletions pkg/token/token-util/token-util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,23 @@ func TestDoRetries(t *testing.T) {
responseStatus: http.StatusTooManyRequests,
},
{
name: "status 502 no retry",
name: "status 502",
call: func() (*http.Response, error) {
totalRetries++

return &http.Response{StatusCode: http.StatusBadGateway}, nil
},
responseStatus: http.StatusBadGateway,
},
{
name: "status 403 no retry",
call: func() (*http.Response, error) {
totalRetries++

return &http.Response{StatusCode: http.StatusForbidden}, nil
},
responseStatus: http.StatusForbidden,
},
{
name: "no url",
call: func() (*http.Response, error) {
Expand All @@ -184,8 +193,8 @@ func TestDoRetries(t *testing.T) {
} else {
assert.Equal(t, tc.responseStatus, resp.StatusCode)

// only 429 and 500 status codes should retry
if tc.responseStatus == http.StatusBadGateway {
// only 429, 500 and 502 status codes should retry
if tc.responseStatus == http.StatusForbidden {
assert.Equal(t, 1, totalRetries)
} else {
assert.Equal(t, 2, totalRetries)
Expand Down

0 comments on commit a832630

Please sign in to comment.