Skip to content

Commit

Permalink
Configure Godo Retryable HTTP Client (#1016)
Browse files Browse the repository at this point in the history
* Draft: Configure Godo Retryable HTTP Client

* Update main.tf

* Update main.tf

* Update outputs.tf

* Update outputs.tf

* Update outputs.tf

* update outputs.tf

* make default retrymax=4

* export transport

* Client -> HTTPClient

* change hardcoded var

* Revert "change hardcoded var"

This reverts commit d61d02a.

* remove hardcoded var

* update client, add log

* doc update

* update log

* remove transport
  • Loading branch information
danaelhe committed Aug 22, 2023
1 parent 56ef0b6 commit 364fc6c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
30 changes: 15 additions & 15 deletions digitalocean/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import (
"net/http"
"net/url"
"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 +74,32 @@ 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))

client = retryableClient.StandardClient()
client.Transport = &oauth2.Transport{
Base: client.Transport,
Source: oauth2.ReuseTokenSource(nil, tokenSrc),
retryConfig := godo.RetryConfig{
RetryMax: c.HTTPRetryMax,
RetryWaitMin: godo.PtrTo(c.HTTPRetryWaitMin),
RetryWaitMax: godo.PtrTo(c.HTTPRetryWaitMax),
Logger: log.Default(),
}
} else {
client = oauth2.NewClient(context.Background(), tokenSrc)

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

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),
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.

0 comments on commit 364fc6c

Please sign in to comment.