Skip to content
Open
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
48 changes: 22 additions & 26 deletions nullplatform/null_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"log"
"math"
"math/rand/v2"
"net"
"net/http"
"strings"
Expand Down Expand Up @@ -229,16 +228,15 @@ func isRetryableClientError(err error) bool {

func (rc *RetryConfig) calculateBackoff(attempt int) time.Duration {
backoff := float64(rc.InitialInterval) * math.Pow(rc.Multiplier, float64(attempt))
delta := math.Abs(rc.RandomFactor * backoff)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Calculations using default config gave this:

Attempt 0:

backoff = 1s * (2^0) = 1s
delta = |0.1 * 1s| = 0.1s
finalBackoff = 1s * (1.1) = 1.1s

Attempt 1:

backoff = 1s * (2^1) = 2s
delta = |0.1 * 2s| = 0.2s
finalBackoff = 2s * (1.1) = 2.2s

Attempt 2:

backoff = 1s * (2^2) = 4s
delta = |0.1 * 4s| = 0.4s
finalBackoff = 4s * (1.1) = 4.4s

Attempt 3:

backoff = 1s * (2^3) = 8s
delta = |0.1 * 8s| = 0.8s
finalBackoff = 8s * (1.1) = 8.8s

Attempt 4:

backoff = 1s * (2^4) = 16s
delta = |0.1 * 16s| = 1.6s
finalBackoff = 16s * (1.1) = 17.6s

Attempt 5:

backoff = 1s * (2^5) = 32s
delta = |0.1 * 32s| = 3.2s
finalBackoff = 32s * (1.1) = 35.2s -> capped to 30s due to MaxInterval

finalBackoff := backoff * (delta + 1)

if backoff > float64(rc.MaxInterval) {
backoff = float64(rc.MaxInterval)
// Apply MaxInterval cap after jitter
if finalBackoff > float64(rc.MaxInterval) {
finalBackoff = float64(rc.MaxInterval)
}

delta := rc.RandomFactor * backoff
minBackoff := backoff - delta
maxBackoff := backoff + delta

return time.Duration(minBackoff + rand.Float64()*(maxBackoff-minBackoff))
return time.Duration(finalBackoff)
}

func (c *NullClient) MakeRequest(method, path string, body *bytes.Buffer) (*http.Response, error) {
Expand All @@ -248,31 +246,29 @@ func (c *NullClient) MakeRequest(method, path string, body *bytes.Buffer) (*http

retryConfig := DefaultRetryConfig()
var lastErr error
var req *http.Request
var err error
url := fmt.Sprintf("https://%s%s", c.ApiURL, path)
if body != nil {
bodyCopy := bytes.NewBuffer(body.Bytes())
req, err = http.NewRequest(method, url, bodyCopy)
} else {
req, err = http.NewRequest(method, url, nil)
}

if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token.AccessToken))

for attempt := 0; attempt <= retryConfig.MaxRetries; attempt++ {
if attempt > 0 {
backoff := retryConfig.calculateBackoff(attempt - 1)
time.Sleep(backoff)
}

var req *http.Request
var err error
url := fmt.Sprintf("https://%s%s", c.ApiURL, path)

if body != nil {
bodyCopy := bytes.NewBuffer(body.Bytes())
req, err = http.NewRequest(method, url, bodyCopy)
} else {
req, err = http.NewRequest(method, url, nil)
}

if err != nil {
return nil, fmt.Errorf("failed to create request: %v", err)
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.Token.AccessToken))

resp, err := c.Client.Do(req)
if err == nil {
return resp, nil
Expand Down