Skip to content

Commit

Permalink
Make Retrys by Default for NewFromToken() (#632)
Browse files Browse the repository at this point in the history
* Make Retrys by Default for NewFromToken()

* add doc

* more go doc

* Use New()
  • Loading branch information
danaelhe committed Aug 28, 2023
1 parent 4052b67 commit 0161e07
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion godo.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const (
headerRateReset = "RateLimit-Reset"
headerRequestID = "x-request-id"
internalHeaderRetryAttempts = "X-Godo-Retry-Attempts"

defaultRetryMax = 4
defaultRetryWaitMax = 30
defaultRetryWaitMin = 1
)

// Client manages communication with DigitalOcean V2 API.
Expand Down Expand Up @@ -229,7 +233,20 @@ func NewFromToken(token string) *Client {
cleanToken := strings.Trim(strings.TrimSpace(token), "'")
ctx := context.Background()
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: cleanToken})
return NewClient(oauth2.NewClient(ctx, ts))

oauthClient := oauth2.NewClient(ctx, ts)
client, err := New(oauthClient, WithRetryAndBackoffs(
RetryConfig{
RetryMax: defaultRetryMax,
RetryWaitMin: PtrTo(float64(defaultRetryWaitMin)),
RetryWaitMax: PtrTo(float64(defaultRetryWaitMax)),
},
))
if err != nil {
panic(err)
}

return client
}

// NewClient returns a new DigitalOcean API client, using the given
Expand Down

0 comments on commit 0161e07

Please sign in to comment.