Skip to content

Commit

Permalink
add optional retrywaitmix and max
Browse files Browse the repository at this point in the history
  • Loading branch information
danaelhe committed Aug 2, 2023
1 parent f363fea commit 32d5d94
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 11 additions & 5 deletions godo.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,14 @@ type Client struct {

// RetryConfig sets the values used for enabling retries and backoffs for
// requests that fail with 429 or 500-level response codes using the go-retryablehttp client.
// RetryConfig.RetryMax must be configured to enable this behavior.
// RetryConfig.RetryMax must be configured to enable this behavior. RetryConfig.RetryWaitMin and
// RetryConfig.RetryWaitMax are optional, with the default values being 1.0 and 30.0, respectively.
// Note: Opting to use the go-retryablehttp client will overwrite any custom HTTP client passed into New().
// Only the custom HTTP client's custom transport and timeout will be maintained.
type RetryConfig struct {
RetryMax int
RetryWaitMin float64 // Minimum time to wait
RetryWaitMax float64 // Maximum time to wait
RetryWaitMin *float64 // Minimum time to wait
RetryWaitMax *float64 // Maximum time to wait
}

// RequestCompletionCallback defines the type of the request callback function
Expand Down Expand Up @@ -295,8 +296,13 @@ func New(httpClient *http.Client, opts ...ClientOpt) (*Client, error) {
if c.RetryConfig.RetryMax > 0 {
retryableClient := retryablehttp.NewClient()
retryableClient.RetryMax = c.RetryConfig.RetryMax
retryableClient.RetryWaitMin = time.Duration(c.RetryConfig.RetryWaitMin * float64(time.Second))
retryableClient.RetryWaitMax = time.Duration(c.RetryConfig.RetryWaitMin * float64(time.Second))

if c.RetryConfig.RetryWaitMin != nil {
retryableClient.RetryWaitMin = time.Duration(*c.RetryConfig.RetryWaitMin * float64(time.Second))
}
if c.RetryConfig.RetryWaitMax != nil {
retryableClient.RetryWaitMax = time.Duration(*c.RetryConfig.RetryWaitMax * float64(time.Second))
}

// if timeout is set, it is maintained before overwriting client with StandardClient()
retryableClient.HTTPClient.Timeout = c.client.Timeout
Expand Down
10 changes: 8 additions & 2 deletions godo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,16 @@ func TestWithRetryAndBackoffs(t *testing.T) {
})

oauth_client := oauth2.NewClient(oauth2.NoContext, tokenSrc)
var waitMax *float64 = new(float64)
var waitMin *float64 = new(float64)

*waitMax = 6.0
*waitMin = 3.0

retryConfig := RetryConfig{
RetryMax: 3,
RetryWaitMin: 3.0,
RetryWaitMax: 6.0,
RetryWaitMin: waitMin,
RetryWaitMax: waitMax,
}

// Create the client. Use short retry windows so we fail faster.
Expand Down

0 comments on commit 32d5d94

Please sign in to comment.