Skip to content

Commit

Permalink
Move CCC client setup to util
Browse files Browse the repository at this point in the history
- compute connection timeout from configured backoff function.
  • Loading branch information
benashz committed Dec 20, 2021
1 parent 725941b commit 21c490f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
28 changes: 28 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,3 +351,31 @@ func StatusCheckRetry(statusCodes ...int) retryablehttp.CheckRetry {
return false, nil
}
}

// SetupCCCRetryClient for handling Client Controlled Consistency related
// requests.
func SetupCCCRetryClient(client *api.Client, maxRetry int) {
client.SetReadYourWrites(true)
client.SetMaxRetries(maxRetry)
client.SetCheckRetry(StatusCheckRetry(http.StatusNotFound))

// ensure that the clone has the reasonable backoff min/max durations set.
if client.MinRetryWait() == 0 {
client.SetMinRetryWait(time.Millisecond * 1000)
}
if client.MaxRetryWait() == 0 {
client.SetMaxRetryWait(time.Millisecond * 1500)
}
if client.MaxRetryWait() < client.MinRetryWait() {
client.SetMaxRetryWait(client.MinRetryWait())
}

bo := retryablehttp.LinearJitterBackoff
client.SetBackoff(bo)

to := time.Duration(0)
for i := 0; i < client.MaxRetries(); i++ {
to += bo(client.MaxRetryWait(), client.MaxRetryWait(), i, nil)
}
client.SetClientTimeout(to + time.Second*30)
}
23 changes: 1 addition & 22 deletions vault/resource_identity_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ import (
"errors"
"fmt"
"log"
"net/http"
"time"

"github.com/hashicorp/go-retryablehttp"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/vault/api"

Expand Down Expand Up @@ -275,25 +272,7 @@ func readEntity(client *api.Client, path string, retry bool) (*api.Secret, error
if err != nil {
return nil, err
}
client.SetMaxRetries(maxHTTPRetriesCCC)
client.SetCheckRetry(util.StatusCheckRetry(http.StatusNotFound))

// ensure that the clone has the reasonable backoff min/max durations set.
if client.MinRetryWait() == 0 {
client.SetMinRetryWait(time.Millisecond * 1000)
}
if client.MaxRetryWait() == 0 {
client.SetMaxRetryWait(time.Millisecond * 1500)
}
if client.MaxRetryWait() < client.MinRetryWait() {
client.SetMaxRetryWait(client.MinRetryWait())
}

// ensure that retries are not failed due to context deadline being exceeded.
client.SetBackoff(retryablehttp.LinearJitterBackoff)
dt := time.Duration(client.MaxRetries())
d := ((client.MaxRetryWait() * dt) * dt) + time.Second + 30
client.SetClientTimeout(d)
util.SetupCCCRetryClient(client, maxHTTPRetriesCCC)
}

resp, err := client.Logical().Read(path)
Expand Down

0 comments on commit 21c490f

Please sign in to comment.