Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
94 changes: 94 additions & 0 deletions client/backoffs.go
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 {

Copy link
Copy Markdown
Author

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

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) {
Comment thread
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
}
10 changes: 9 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"reflect"
"regexp"
"strings"

"github.com/hashicorp/go-retryablehttp"
)

var (
Expand All @@ -32,11 +34,17 @@ type Service struct {
Client *APIClient
}

func NewRetryAPIClient() *http.Client {
var retryClient = retryablehttp.NewClient()
retryClient.Backoff = thousandEyesBackoff

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The 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.

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()
}

c := new(APIClient)
Expand Down
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
module github.com/thousandeyes/thousandeyes-sdk-go/v3

go 1.18
go 1.23

toolchain go1.24.8

require github.com/stretchr/testify v1.7.2

require (
github.com/davecgh/go-spew v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
)
6 changes: 5 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
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/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-retryablehttp v0.7.8 h1:ylXZWnqa7Lhqpk0L1P1LzDtGcCR0rPVUrx/c8Unxc48=
github.com/hashicorp/go-retryablehttp v0.7.8/go.mod h1:rjiScheydd+CxvumBsIrFKlx3iS0jrZ7LvzFGFmuKbw=
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=
Expand All @@ -8,4 +12,4 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F
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=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading