Skip to content

Commit

Permalink
Expose RetryServerErrors as a part of the config object.
Browse files Browse the repository at this point in the history
The client exposes a receiver method called RetryServerErrors which toggles a flag, c.retryServerErrors,
that enables/disables the retry logic in the http client. This means however, you could only enable
the retry logic once the client was created, meaning any failed metadata request in `NewClient()`
would not be retried. We've extended the interface of the client config object to allow
enabling `c.retryServerErrors` during client init `NewClient()`.
  • Loading branch information
sebasslash committed Jun 22, 2022
1 parent f759c44 commit fe750da
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions tfe.go
Expand Up @@ -66,17 +66,21 @@ type Config struct {

// RetryLogHook is invoked each time a request is retried.
RetryLogHook RetryLogHook

// RetryServerErrors enables the retry logic in the client.
RetryServerErrors bool
}

// DefaultConfig returns a default config structure.

func DefaultConfig() *Config {
config := &Config{
Address: os.Getenv("TFE_ADDRESS"),
BasePath: DefaultBasePath,
Token: os.Getenv("TFE_TOKEN"),
Headers: make(http.Header),
HTTPClient: cleanhttp.DefaultPooledClient(),
Address: os.Getenv("TFE_ADDRESS"),
BasePath: DefaultBasePath,
Token: os.Getenv("TFE_TOKEN"),
Headers: make(http.Header),
HTTPClient: cleanhttp.DefaultPooledClient(),
RetryServerErrors: false,
}

// Set the default address if none is given.
Expand Down Expand Up @@ -196,6 +200,7 @@ func NewClient(cfg *Config) (*Client, error) {
if cfg.RetryLogHook != nil {
config.RetryLogHook = cfg.RetryLogHook
}
config.RetryServerErrors = cfg.RetryServerErrors
}

// Parse the address to make sure its a valid URL.
Expand All @@ -216,10 +221,11 @@ func NewClient(cfg *Config) (*Client, error) {

// Create the client.
client := &Client{
baseURL: baseURL,
token: config.Token,
headers: config.Headers,
retryLogHook: config.RetryLogHook,
baseURL: baseURL,
token: config.Token,
headers: config.Headers,
retryLogHook: config.RetryLogHook,
retryServerErrors: config.RetryServerErrors,
}

client.http = &retryablehttp.Client{
Expand Down

0 comments on commit fe750da

Please sign in to comment.