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

Introduce RetriesDefaultWaitTime #219

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 17 additions & 12 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,22 +364,27 @@ func FormatJSON(raw []byte) (string, error) {
return string(pretty), nil
}

func RetryBackoffFunc(logger Logger) gophercloud.RetryBackoffFunc {
func RetryBackoffFunc(logger Logger, RetriesDefaultWaitTime int) gophercloud.RetryBackoffFunc {
return func(ctx context.Context, respErr *gophercloud.ErrUnexpectedResponseCode, e error, retries uint) error {
retryAfter := respErr.ResponseHeader.Get("Retry-After")
if retryAfter == "" {
return e
}

var sleep time.Duration

// Parse delay seconds or HTTP date
if v, err := strconv.ParseUint(retryAfter, 10, 32); err == nil {
sleep = time.Duration(v) * time.Second
} else if v, err := time.Parse(http.TimeFormat, retryAfter); err == nil {
sleep = time.Until(v)
} else {
retryAfter := respErr.ResponseHeader.Get("Retry-After")
// No Retry-After Header set by service and no default wait time set
if retryAfter == "" && RetriesDefaultWaitTime == 0 {
return e
// No Retry-After Header set by service and default wait time set
} else if retryAfter == "" && RetriesDefaultWaitTime > 0 {
sleep = time.Duration(RetriesDefaultWaitTime) * time.Second
// Retry-After Header is set
} else {
// Parse delay seconds or HTTP date
if v, err := strconv.ParseUint(retryAfter, 10, 32); err == nil {
sleep = time.Duration(v) * time.Second
} else if v, err := time.Parse(http.TimeFormat, retryAfter); err == nil {
sleep = time.Until(v)
} else {
return e
}
}

l := logger
Expand Down
7 changes: 6 additions & 1 deletion terraform/auth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type Config struct {
ApplicationCredentialSecret string
UseOctavia bool
MaxRetries int
RetriesDefaultWaitTime int
DisableNoCacheHeader bool
Context context.Context

Expand Down Expand Up @@ -97,6 +98,10 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {
return fmt.Errorf("max_retries should be a positive value")
}

if c.RetriesDefaultWaitTime < 0 {
return fmt.Errorf("retries_default_wait_time should be a positive value")
}

clientOpts := new(clientconfig.ClientOpts)

// If a cloud entry was given, base AuthOptions on a clouds.yaml file.
Expand Down Expand Up @@ -205,7 +210,7 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {

if c.MaxRetries > 0 {
client.MaxBackoffRetries = uint(c.MaxRetries)
client.RetryBackoffFunc = osClient.RetryBackoffFunc(logger)
client.RetryBackoffFunc = osClient.RetryBackoffFunc(logger, c.RetriesDefaultWaitTime)
}

if !c.DelayedAuth && !c.Swauth {
Expand Down
Loading