-
Notifications
You must be signed in to change notification settings - Fork 18
Remove backoff dependency and deprecate RetryOptions #337
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d7ba79e
Remove backoff dependency and deprecate RetryOptions
vicentepinto98 2fe48a6
Update CHANGELOG, example
vicentepinto98 e2a21cc
Changes after review
vicentepinto98 26603d0
Remove consts
vicentepinto98 8face47
Remove Do routine, update changelogs
vicentepinto98 2c120a0
Update CHANGELOG
vicentepinto98 63c8752
Add release date
vicentepinto98 9b515f6
Remove const
vicentepinto98 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hcsa73 marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,22 @@ | ||
package clients | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
) | ||
|
||
const ( | ||
// API configuration options: | ||
Environment = "STACKIT_ENV" | ||
) | ||
|
||
const ( | ||
// Known error messages | ||
ClientTimeoutErr = "Client.Timeout exceeded while awaiting headers" | ||
ClientContextDeadlineErr = "context deadline exceeded" | ||
ClientConnectionRefusedErr = "connection refused" | ||
ClientEOFError = "unexpected EOF" | ||
) | ||
|
||
const ( | ||
DefaultClientTimeout = time.Minute | ||
DefaultRetryMaxRetries = 3 | ||
DefaultRetryWaitBetweenCalls = 30 * time.Second | ||
DefaultRetryTimeout = 2 * time.Minute | ||
DefaultClientTimeout = time.Minute | ||
) | ||
|
||
// Deprecated: retry options were removed to reduce complexity of the client. If this functionality is needed, you can provide your own custom HTTP client. | ||
type RetryConfig struct { | ||
MaxRetries int // Max retries | ||
WaitBetweenCalls time.Duration // Time to wait between requests | ||
RetryTimeout time.Duration // Max time to re-try | ||
ClientTimeout time.Duration // HTTP Client timeout | ||
} | ||
|
||
// Deprecated: retry options were removed to reduce complexity of the client. If this functionality is needed, you can provide your own custom HTTP client. | ||
func NewRetryConfig() *RetryConfig { | ||
return &RetryConfig{ | ||
MaxRetries: DefaultRetryMaxRetries, | ||
WaitBetweenCalls: DefaultRetryWaitBetweenCalls, | ||
RetryTimeout: DefaultRetryTimeout, | ||
ClientTimeout: DefaultClientTimeout, | ||
} | ||
} | ||
|
||
// Do performs the request, retrying according to the configurations provided | ||
func Do(client *http.Client, req *http.Request, cfg *RetryConfig) (resp *http.Response, err error) { | ||
if cfg == nil { | ||
cfg = NewRetryConfig() | ||
} | ||
if client == nil { | ||
client = &http.Client{} | ||
} | ||
client.Timeout = cfg.ClientTimeout | ||
|
||
maxRetries := cfg.MaxRetries | ||
ctx, cancel := context.WithTimeout(req.Context(), cfg.RetryTimeout) | ||
defer cancel() | ||
b := backoff.WithContext(backoff.NewConstantBackOff(cfg.WaitBetweenCalls), ctx) | ||
|
||
err = backoff.Retry(func() error { | ||
resp, err = client.Do(req) //nolint:bodyclose // body is closed by the caller functions | ||
if err != nil { | ||
if maxRetries > 0 { | ||
if errorIsOneOf(err, ClientTimeoutErr, ClientContextDeadlineErr, ClientConnectionRefusedErr) || | ||
(req.Method == http.MethodGet && strings.Contains(err.Error(), ClientEOFError)) { | ||
// reduce retries counter and retry | ||
maxRetries-- | ||
return err | ||
} | ||
} | ||
return backoff.Permanent(err) | ||
} | ||
if maxRetries > 0 && resp != nil { | ||
if resp.StatusCode == http.StatusBadGateway || | ||
resp.StatusCode == http.StatusGatewayTimeout { | ||
maxRetries-- | ||
return fmt.Errorf("request returned a gateway error") | ||
} | ||
} | ||
return nil | ||
}, b) | ||
if err != nil { | ||
return resp, fmt.Errorf("url: %s\nmethod: %s\n err: %w", req.URL.String(), req.Method, err) | ||
} | ||
|
||
return resp, err | ||
} | ||
|
||
// ErrorIsOneOf checks if a given error message | ||
// has one of the provided sub strings | ||
func errorIsOneOf(err error, msgs ...string) bool { | ||
if err == nil { | ||
return false | ||
} | ||
for _, m := range msgs { | ||
if strings.Contains(err.Error(), m) { | ||
return true | ||
} | ||
} | ||
return false | ||
return &RetryConfig{} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.