Skip to content
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

Configure Godo Retryable HTTP Client #1016

Merged
merged 20 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 17 additions & 11 deletions digitalocean/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
"log"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/digitalocean/godo"
"github.com/hashicorp/go-retryablehttp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/logging"
"golang.org/x/oauth2"
)
Expand Down Expand Up @@ -76,30 +75,37 @@ func (c *Config) Client() (*CombinedConfig, error) {

userAgent := fmt.Sprintf("Terraform/%s", c.TerraformVersion)
var client *http.Client
var godoOpts []godo.ClientOpt

client = oauth2.NewClient(context.Background(), tokenSrc)

if c.HTTPRetryMax > 0 {
retryableClient := retryablehttp.NewClient()
retryableClient.RetryMax = c.HTTPRetryMax
retryableClient.RetryWaitMin = time.Duration(c.HTTPRetryWaitMin * float64(time.Second))
retryableClient.RetryWaitMax = time.Duration(c.HTTPRetryWaitMax * float64(time.Second))
retryConfig := godo.RetryConfig{
RetryMax: c.HTTPRetryMax,
RetryWaitMin: godo.PtrTo(c.HTTPRetryWaitMin),
RetryWaitMax: godo.PtrTo(c.HTTPRetryWaitMax),
Logger: log.New(os.Stderr, "", log.LstdFlags),
danaelhe marked this conversation as resolved.
Show resolved Hide resolved
}

godoOpts = []godo.ClientOpt{godo.WithRetryAndBackoffs(retryConfig)}

client = retryableClient.StandardClient()
client.Transport = &oauth2.Transport{
Base: client.Transport,
Source: oauth2.ReuseTokenSource(nil, tokenSrc),
andrewsomething marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
client = oauth2.NewClient(context.Background(), tokenSrc)
}

client.Transport = logging.NewTransport("DigitalOcean", client.Transport)
godoOpts = append(godoOpts, godo.SetUserAgent(userAgent))

godoOpts := []godo.ClientOpt{godo.SetUserAgent(userAgent)}
if c.RequestsPerSecond > 0.0 {
godoOpts = append(godoOpts, godo.SetStaticRateLimit(c.RequestsPerSecond))
}

godoClient, err := godo.New(client, godoOpts...)
clientTransport := logging.NewTransport("DigitalOcean", godoClient.HTTPClient.Transport)

godoClient.HTTPClient.Transport = clientTransport

if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion digitalocean/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func Provider() *schema.Provider {
"http_retry_max": {
Type: schema.TypeInt,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("DIGITALOCEAN_HTTP_RETRY_MAX", 0),
DefaultFunc: schema.EnvDefaultFunc("DIGITALOCEAN_HTTP_RETRY_MAX", 4),
danaelhe marked this conversation as resolved.
Show resolved Hide resolved
Description: "The maximum number of retries on a failed API request.",
},
"http_retry_wait_min": {
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ The following arguments are supported:
of retries on a failed API request (client errors, 422, 500, 502...), the exponential
backoff can be configured by the `http_retry_wait_min` and `http_retry_wait_max` arguments
(Defaults to the value of the `DIGITALOCEAN_HTTP_RETRY_MAX` environment variable or
`0`, which means no retries, if unset).
`4` if unset).
* `http_retry_wait_min` - (Optional) This can be used to configure the minimum
waiting time (**in seconds**) between failed requests for the backoff strategy
(Defaults to the value of the `DIGITALOCEAN_HTTP_RETRY_WAIT_MIN` environment
Expand Down
2 changes: 1 addition & 1 deletion vendor/github.com/digitalocean/godo/godo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.