Skip to content

Commit

Permalink
custom linear jitter
Browse files Browse the repository at this point in the history
  • Loading branch information
ConvallariaMaj committed Aug 14, 2024
1 parent 1d0dfb0 commit de5cd55
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion activation/poet.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"math"
"math/rand"
"net/http"
"net/url"
"sync"
Expand Down Expand Up @@ -128,13 +129,31 @@ func WithLogger(logger *zap.Logger) PoetClientOpts {
}
}

func customLinearJitterBackoff(min, max time.Duration, attemptNum int, resp *http.Response) time.Duration {
attemptNum++
if max <= min {
return min * time.Duration(attemptNum)
}

source := rand.New(rand.NewSource(int64(time.Now().Nanosecond())))

jitter := source.Float64() * float64(max-min)
jitterMin := int64(jitter) + int64(min)

sleep := time.Duration(jitterMin * int64(attemptNum))
if sleep > max {
sleep = max
}
return sleep
}

// NewHTTPPoetClient returns new instance of HTTPPoetClient connecting to the specified url.
func NewHTTPPoetClient(server types.PoetServer, cfg PoetConfig, opts ...PoetClientOpts) (*HTTPPoetClient, error) {
client := &retryablehttp.Client{
RetryMax: cfg.MaxRequestRetries,
RetryWaitMin: cfg.RequestRetryDelay,
RetryWaitMax: 2 * cfg.RequestRetryDelay,
Backoff: retryablehttp.LinearJitterBackoff,
Backoff: customLinearJitterBackoff,
CheckRetry: checkRetry,
}

Expand All @@ -161,6 +180,7 @@ func NewHTTPPoetClient(server types.PoetServer, cfg PoetConfig, opts ...PoetClie
submitChallengeClient: submitChallengeClient,
logger: zap.NewNop(),
}

for _, opt := range opts {
opt(poetClient)
}
Expand Down

0 comments on commit de5cd55

Please sign in to comment.