From 267f6f48c0574bf6b35a6414114c8b2661ffdd01 Mon Sep 17 00:00:00 2001 From: Adrienne Cohea Date: Mon, 14 Dec 2020 21:12:32 -0800 Subject: [PATCH] Rename backoff to b --- resource_acl_bootstrap.go | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/resource_acl_bootstrap.go b/resource_acl_bootstrap.go index 87d19ad..423ae7f 100644 --- a/resource_acl_bootstrap.go +++ b/resource_acl_bootstrap.go @@ -3,6 +3,7 @@ package main import ( "log" "strings" + "time" backoff "github.com/cenkalti/backoff/v4" "github.com/hashicorp/nomad/api" @@ -64,17 +65,19 @@ func aclBootstrap() *schema.Resource { } func bootstrapACLs(d *schema.ResourceData, meta interface{}) error { + b := backoff.NewExponentialBackOff() + b.InitialInterval = 2 * time.Second + b.MaxInterval = 30 * time.Second + b.MaxElapsedTime = 10 * time.Minute + return backoff.Retry(func() error { client := meta.(*api.Client) resp, _, err := client.ACLTokens().Bootstrap(nil) if err != nil { - if strings.Contains(err.Error(), "i/o timeout") { - return err - } else { - return backoff.Permanent(err) - } + return maybeRetry(err) } + log.Printf("[DEBUG] Created ACL token %q", resp.AccessorID) d.SetId(resp.AccessorID) @@ -87,7 +90,7 @@ func bootstrapACLs(d *schema.ResourceData, meta interface{}) error { _ = d.Set("create_time", resp.CreateTime.UTC().String()) return nil - }, backoff.NewExponentialBackOff()) + }, b) } func forget(d *schema.ResourceData, m interface{}) error { @@ -98,3 +101,27 @@ func forget(d *schema.ResourceData, m interface{}) error { func doNothing(d *schema.ResourceData, m interface{}) error { return nil } + +func isNetworkError(err error) bool { + networkErrors := []string{ + "i/o timeout", + "connection refused", + "EOF", + } + + for _, e := range networkErrors { + if strings.Contains(err.Error(), e) { + return true + } + } + + return false +} + +func maybeRetry(err error) error { + if isNetworkError(err) { + return err + } + + return backoff.Permanent(err) +}