Skip to content

Commit

Permalink
namecheap: lower max retry attemts and time.Sleep period
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Apr 12, 2019
1 parent 6a10619 commit 74a1f51
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
32 changes: 21 additions & 11 deletions namecheap/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ import (
"github.com/hashicorp/terraform/terraform"
)

var (
ErrTooManyRetries = fmt.Errorf("exceeded max retry limit")
)

// These are the "Auto" TTL settings in Namecheap
const (
ncDefaultTTL int = 1799
ncDefaultMXPref int = 10
ncDefaultTimeout time.Duration = 30
)

// Provider returns a terraform.ResourceProvider.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Expand Down Expand Up @@ -72,22 +83,21 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return config.Client()
}

// retryApiCall attempts a specific calllback several times with greater pause between attempts.
// The callback should be responsible for modifying state and cleaning up any resources.
func retryApiCall(f func() error) error {
apiThrottleBackoffTime := 2
attempts, max := 0, 5
for {
attempts++
if attempts > max {
log.Printf("[ERROR] API Retry Limit Reached.")
return ErrTooManyRetries
}
if err := f(); err != nil {
log.Printf("[INFO] Err: %v", err.Error())
if strings.Contains(err.Error(), "expected element type <ApiResponse> but have <html>") {
log.Printf("[WARN] Bad Namecheap API response received, backing off for %d seconds...", apiThrottleBackoffTime)

time.Sleep(time.Duration(apiThrottleBackoffTime) * time.Second)

apiThrottleBackoffTime = apiThrottleBackoffTime * ncBackoffMultiplier

if apiThrottleBackoffTime > ncMaxThrottleRetry {
log.Printf("[ERROR] API Retry Limit Reached. Couldn't find namecheap record: %v", err)
break
}
log.Printf("[WARN] Bad Namecheap API response received, backing off for %d seconds...", attempts)
time.Sleep(time.Duration(attempts) * time.Second)
continue // retry
}
return fmt.Errorf("Failed to create namecheap Record: %s", err)
Expand Down
7 changes: 0 additions & 7 deletions namecheap/resource_namecheap_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@ import (
// We need a mutex here because of the underlying api
var mutex = &sync.Mutex{}

// This is the "Auto" TTL setting in Namecheap
const ncDefaultTTL int = 1799
const ncDefaultMXPref int = 10
const ncDefaultTimeout time.Duration = 30
const ncMaxThrottleRetry int = 60
const ncBackoffMultiplier int = 2

func resourceNameCheapRecord() *schema.Resource {
return &schema.Resource{
Create: resourceNameCheapRecordCreate,
Expand Down

0 comments on commit 74a1f51

Please sign in to comment.