-
Notifications
You must be signed in to change notification settings - Fork 24
feat: Use retry http and add backoff #151
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "regexp" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/hashicorp/go-retryablehttp" | ||
| ) | ||
|
|
||
| var ( | ||
| rateLimitResetHeaders = []string{ | ||
| "x-organization-rate-limit-reset", | ||
| "x-instant-test-rate-limit-reset", | ||
| } | ||
| resetHeaderPattern = regexp.MustCompile(`^\s*[0-9]+\s*$`) | ||
| timeNow = time.Now | ||
| ) | ||
|
|
||
| // Custom backoff function | ||
| func thousandEyesBackoff(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration { | ||
| if resp == nil { | ||
| return 0 | ||
| } | ||
|
|
||
| if resp.StatusCode == http.StatusTooManyRequests || resp.StatusCode == http.StatusServiceUnavailable { | ||
| if sleep, ok := parseRetryAfterHeader(resp.Header["Retry-After"]); ok { | ||
| return sleep | ||
| } | ||
| } | ||
|
|
||
| for _, header := range rateLimitResetHeaders { | ||
| if resetTimeStr := resp.Header.Get(header); resetTimeStr != "" { | ||
| if sleep, ok := parseResetHeader(&resetTimeStr); ok { | ||
| return sleep | ||
| } | ||
|
|
||
| // Default backoff if no valid reset time is found | ||
| return retryablehttp.DefaultBackoff(min, max, attemptNum, resp) | ||
| } | ||
| } | ||
|
|
||
| // Default case for no Retry-After and rateLimitResetHeaders | ||
| return retryablehttp.DefaultBackoff(min, max, attemptNum, resp) | ||
| } | ||
|
|
||
| func parseRetryAfterHeader(headers []string) (time.Duration, bool) { | ||
|
tshe-te marked this conversation as resolved.
|
||
| if len(headers) == 0 || headers[0] == "" { | ||
| return 0, false | ||
| } | ||
| header := headers[0] | ||
| // Retry-After: 120 | ||
| if sleep, err := strconv.ParseInt(header, 10, 64); err == nil { | ||
| if sleep < 0 { // a negative sleep doesn't make sense | ||
| return 0, false | ||
| } | ||
| return time.Second * time.Duration(sleep), true | ||
| } | ||
|
|
||
| // Retry-After: Fri, 31 Dec 1999 23:59:59 GMT | ||
| retryTime, err := time.Parse(time.RFC1123, header) | ||
| if err != nil { | ||
| return 0, false | ||
| } | ||
| if until := retryTime.Sub(timeNow()); until > 0 { | ||
| return until, true | ||
| } | ||
| // date is in the past | ||
| return 0, true | ||
| } | ||
|
|
||
| // Function to parse the reset header | ||
| func parseResetHeader(value *string) (time.Duration, bool) { | ||
| if value == nil || !resetHeaderPattern.MatchString(*value) { | ||
| return 0, false | ||
| } | ||
|
|
||
| // Parse the header value to a Unix timestamp | ||
| resetTimeUnix, err := strconv.ParseInt(*value, 10, 64) | ||
| if err != nil { | ||
| return 0, false | ||
| } | ||
|
|
||
| // Calculate the duration until the reset time | ||
| resetTime := time.Unix(resetTimeUnix, 0) | ||
| waitDuration := time.Until(resetTime) | ||
|
|
||
| // Return 0 if the reset time has already passed | ||
| if waitDuration < 0 { | ||
| return 0, true | ||
| } | ||
| return waitDuration, true | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -14,6 +14,8 @@ import ( | |
| "reflect" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "github.com/hashicorp/go-retryablehttp" | ||
| ) | ||
|
|
||
| var ( | ||
|
|
@@ -32,12 +34,23 @@ type Service struct { | |
| Client *APIClient | ||
| } | ||
|
|
||
| func NewRetryAPIClient(httpClient *http.Client) *http.Client { | ||
| retryClient := retryablehttp.NewClient() | ||
| retryClient.CheckRetry = retryablehttp.DefaultRetryPolicy | ||
| retryClient.Backoff = thousandEyesBackoff | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't set up RetryMax so the client will attempt 5 times as the default. |
||
|
|
||
| if httpClient != nil { | ||
| retryClient.HTTPClient = httpClient | ||
| } | ||
|
|
||
| // Return the retry-enabled client | ||
| return retryClient.StandardClient() | ||
| } | ||
|
|
||
| // NewAPIClient creates a new API client. | ||
| // optionally a custom http.Client to allow for advanced features such as caching. | ||
| func NewAPIClient(cfg *Configuration) *APIClient { | ||
| if cfg.HTTPClient == nil { | ||
| cfg.HTTPClient = http.DefaultClient | ||
| } | ||
| cfg.HTTPClient = NewRetryAPIClient(cfg.HTTPClient) | ||
|
|
||
| c := new(APIClient) | ||
| c.cfg = cfg | ||
|
|
||
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,11 +1,9 @@ | ||
| module github.com/thousandeyes/thousandeyes-sdk-go/v3 | ||
|
|
||
| go 1.18 | ||
| go 1.23 | ||
|
|
||
| require github.com/stretchr/testify v1.7.2 | ||
| toolchain go1.24.8 | ||
|
|
||
| require ( | ||
| github.com/davecgh/go-spew v1.1.0 // indirect | ||
| github.com/pmezard/go-difflib v1.0.0 // indirect | ||
| gopkg.in/yaml.v3 v3.0.1 // indirect | ||
| ) | ||
| require github.com/hashicorp/go-retryablehttp v0.7.8 | ||
|
|
||
| require github.com/hashicorp/go-cleanhttp v0.5.2 // indirect |
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,11 +1,14 @@ | ||
| github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
| github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
| github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
| github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
| github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
| github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= | ||
| github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
| gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
| gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
| gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= | ||
| github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= | ||
| github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= | ||
| github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ= | ||
| github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48= | ||
| github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k= | ||
| github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M= | ||
| github.com/hashicorp/go-retryablehttp v0.7.8 h1:ylXZWnqa7Lhqpk0L1P1LzDtGcCR0rPVUrx/c8Unxc48= | ||
| github.com/hashicorp/go-retryablehttp v0.7.8/go.mod h1:rjiScheydd+CxvumBsIrFKlx3iS0jrZ7LvzFGFmuKbw= | ||
| github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= | ||
| github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= | ||
| github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= | ||
| github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= | ||
| golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= | ||
| golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This function has the same logic as the python SDK which respects Retry-After first then checks thousandeyes headers. https://github.com/thousandeyes/thousandeyes-sdk-python/pull/7/files#diff-8587054108c7902eac148f3b9ea7a213f04ddebde0be29f8b68affe61b98b39fR46