Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add logs #39

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions httputils/httphelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,14 @@ func HttpPostWithContext(ctx context.Context, httpClient IHttpClient, fullURL st
if err != nil {
return err
}
defer resp.Body.Close()

// If the status code is not 200, we will retry
defer func() {
if resp != nil && resp.Body != nil {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
}()

if resp.StatusCode != http.StatusOK {
if shouldRetry(resp) {
return fmt.Errorf("received status code: %d", resp.StatusCode)
Expand All @@ -107,17 +112,17 @@ func HttpPostWithContext(ctx context.Context, httpClient IHttpClient, fullURL st
return nil
}

// Create a new exponential backoff policy
expBackOff := backoff.NewExponentialBackOff()
expBackOff.MaxElapsedTime = maxElapsedTime // Set the maximum elapsed time
expBackOff.MaxElapsedTime = maxElapsedTime

// Run the operation with the exponential backoff policy
if err = backoff.Retry(operation, expBackOff); err != nil {
return resp, err
}

return resp, nil
}



func defaultShouldRetry(resp *http.Response) bool {
// If received codes 401/403/404/500 should return false
return resp.StatusCode != http.StatusUnauthorized &&
Expand Down
Loading