Skip to content
Draft
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
15 changes: 12 additions & 3 deletions kaleido/kaleidobase/providerdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import (
"context"
"crypto/tls"
"fmt"
"math/rand/v2"
"net"
"net/http"
"os"
"strconv"
"time"

"github.com/go-resty/resty/v2"
Expand Down Expand Up @@ -121,9 +123,16 @@ func NewProviderData(logCtx context.Context, conf *ProviderModel) *ProviderData
}
return false
}).
SetRetryCount(3).
SetRetryWaitTime(1 * time.Second).
SetRetryMaxWaitTime(10 * time.Second).
SetRetryCount(5).
SetRetryAfter(func(c *resty.Client, r *resty.Response) (time.Duration, error) {
tflog.Debug(logCtx, fmt.Sprintf("retryAfter: %s", r.Header().Get("Retry-After")))
retryAfter, err := strconv.ParseFloat(r.Header().Get("Retry-After"), 64) // decimal seconds
if err != nil {
retryAfter = 1.0
}
jitter := time.Duration(rand.NormFloat64() * float64(5*time.Second)) // up to 5 second random jitter to account for 5 concurrent connections all retrying up to 5 times
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong - should rand.Float64() otherwise we could have negative jitter

return time.Duration(retryAfter*float64(time.Second)) + jitter, nil
}).
Comment on lines +126 to +135
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also consider adding throttling, so no matter what the client doesn't go faster than 1 req every 250ms ?

SetBaseURL(platformAPI)
if platformUsername != "" && platformPassword != "" {
platform = platform.SetBasicAuth(platformUsername, platformPassword)
Expand Down
4 changes: 2 additions & 2 deletions kaleido/kaleidobase/retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ type CustomRetry struct {
}

var Retry = &CustomRetry{
InitialDelay: 500 * time.Millisecond,
MaximumDelay: 5 * time.Second,
InitialDelay: 5 * time.Second,
MaximumDelay: 30 * time.Second,
Factor: 2.0,
}

Expand Down
5 changes: 4 additions & 1 deletion kaleido/platform/cms_action_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,11 @@ func (r *cms_action_baseResource) waitForActionStatus(ctx context.Context, data
path := r.apiPath(data)
cancelInfo := APICancelInfo()
_ = kaleidobase.Retry.Do(ctx, fmt.Sprintf("build-check %s", path), func(attempt int) (retry bool, err error) {
ok, _ := r.apiRequest(ctx, http.MethodGet, path, nil, &api, diagnostics, cancelInfo)
ok, statusCode := r.apiRequest(ctx, http.MethodGet, path, nil, &api, diagnostics, cancelInfo)
if !ok {
if statusCode == 429 {
return true, fmt.Errorf("rate limit exceeded")
}
return false, fmt.Errorf("action-check failed") // already set in diag
}
cancelInfo.CancelInfo = fmt.Sprintf("waiting for completion - status: %s", api.OutputBase().Status)
Expand Down
5 changes: 4 additions & 1 deletion kaleido/platform/cms_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,11 @@ func (r *cms_buildResource) waitForBuildStatus(ctx context.Context, data *CMSBui
path := r.apiPath(data)
cancelInfo := APICancelInfo()
_ = kaleidobase.Retry.Do(ctx, fmt.Sprintf("build-check %s", path), func(attempt int) (retry bool, err error) {
ok, _ := r.apiRequest(ctx, http.MethodGet, path, nil, &api, diagnostics, cancelInfo)
ok, statusCode := r.apiRequest(ctx, http.MethodGet, path, nil, &api, diagnostics, cancelInfo)
if !ok {
if statusCode == 429 {
return true, fmt.Errorf("rate limit exceeded")
}
return false, fmt.Errorf("build-check failed") // already set in diag
}
cancelInfo.CancelInfo = fmt.Sprintf("(waiting for completion - status: %s)", api.Status)
Expand Down
5 changes: 4 additions & 1 deletion kaleido/platform/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,11 @@ func (r *commonResource) waitForReadyStatus(ctx context.Context, path string, di
cancelInfo := APICancelInfo()
_ = kaleidobase.Retry.Do(ctx, fmt.Sprintf("ready-check %s", path), func(attempt int) (retry bool, err error) {
var res statusResponse
ok, _ := r.apiRequest(ctx, http.MethodGet, path, nil, &res, diagnostics, cancelInfo)
ok, statusCode := r.apiRequest(ctx, http.MethodGet, path, nil, &res, diagnostics, cancelInfo)
if !ok {
if statusCode == 429 {
return true, fmt.Errorf("rate limit exceeded")
}
return false, fmt.Errorf("ready-check failed") // already set in diag
}
cancelInfo.CancelInfo = fmt.Sprintf("(waiting for ready - status: %s)", res.Status)
Expand Down
5 changes: 4 additions & 1 deletion kaleido/platform/firefly_registration.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,11 @@ func (r *firefly_registrationResource) ensureRegistered(ctx context.Context, dat
orgSubmitted := false
cancelInfo := APICancelInfo()
_ = kaleidobase.Retry.Do(ctx, "register", func(attempt int) (retry bool, err error) {
ok, _ := r.apiRequest(ctx, http.MethodGet, r.apiPath(data, "status"), nil, &status, diagnostics, cancelInfo)
ok, statusCode := r.apiRequest(ctx, http.MethodGet, r.apiPath(data, "status"), nil, &status, diagnostics, cancelInfo)
if !ok {
if statusCode == 429 {
return true, fmt.Errorf("rate limit exceeded")
}
return false, fmt.Errorf("status-check failed") // already set in diag
}
if registered := status.toData(data, diagnostics); registered {
Expand Down
Loading