MSP-8292 Up deps
This commit is contained in:
1 parent
3a66223a89
commit
5daaea50a1
186 files changed
+7092
-1579
No files matched your search
+7
-4
@@ -1,6 +1,6 @@
|
||||
# Retry
|
||||
|
||||
[](https://pkg.go.dev/mod/github.com/sethvargo/go-retry)
|
||||
[](https://github.com/sethvargo/go-retry/actions/workflows/test.yml)[](https://pkg.go.dev/mod/github.com/sethvargo/go-retry)
|
||||
|
||||
Retry is a Go library for facilitating retry logic and backoff. It's highly
|
||||
extensible with full control over how and when retries occur. You can also write
|
||||
@@ -115,7 +115,7 @@ also write custom middleware.
|
||||
|
||||
### Jitter
|
||||
|
||||
To reduce the changes of a thundering herd, add random jitter to the returned
|
||||
To reduce the chances of a thundering herd, add random jitter to the returned
|
||||
value.
|
||||
|
||||
```golang
|
||||
@@ -126,6 +126,9 @@ b = WithJitter(500*time.Millisecond, b)
|
||||
|
||||
// Return the next value, +/- 5% of the result
|
||||
b = WithJitterPercent(5, b)
|
||||
|
||||
// Return a random value in [0, next value)
|
||||
b = WithFullJitter(b)
|
||||
```
|
||||
|
||||
### MaxRetries
|
||||
@@ -178,8 +181,8 @@ Benchmark/sethvargo-7 203,914,245 5.73 ns/op
|
||||
|
||||
## Notes and Caveats
|
||||
|
||||
- Randomization uses `math/rand` seeded with the Unix timestamp instead of
|
||||
`crypto/rand`.
|
||||
- Randomization uses `math/rand/v2` (non-cryptographic, automatically seeded),
|
||||
not `crypto/rand`.
|
||||
- Ordering of addition of multiple modifiers will make a difference.
|
||||
For example; ensure you add `CappedDuration` before `WithMaxDuration`, otherwise it may early out too early.
|
||||
Another example is you could add `Jitter` before or after capping depending on your desired outcome.
|
||||
+35
-13
@@ -1,6 +1,7 @@
|
||||
package retry
|
||||
|
||||
import (
|
||||
"math/rand/v2"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -26,19 +27,18 @@ func (b BackoffFunc) Next() (time.Duration, bool) {
|
||||
// returned 20s, the value could be between 15 and 25 seconds. The value can
|
||||
// never be less than 0.
|
||||
func WithJitter(j time.Duration, next Backoff) Backoff {
|
||||
r := newLockedRandom(time.Now().UnixNano())
|
||||
|
||||
return BackoffFunc(func() (time.Duration, bool) {
|
||||
val, stop := next.Next()
|
||||
if stop {
|
||||
return 0, true
|
||||
}
|
||||
|
||||
diff := time.Duration(r.Int63n(int64(j)*2) - int64(j))
|
||||
val = val + diff
|
||||
if val < 0 {
|
||||
val = 0
|
||||
if j <= 0 {
|
||||
return val, false
|
||||
}
|
||||
|
||||
diff := time.Duration(rand.Int64N(int64(j)*2) - int64(j))
|
||||
val = max(val+diff, 0)
|
||||
return val, false
|
||||
})
|
||||
}
|
||||
@@ -48,26 +48,48 @@ func WithJitter(j time.Duration, next Backoff) Backoff {
|
||||
// the backoff returned 20s, the value could be between 19 and 21 seconds. The
|
||||
// value can never be less than 0 or greater than 100.
|
||||
func WithJitterPercent(j uint64, next Backoff) Backoff {
|
||||
r := newLockedRandom(time.Now().UnixNano())
|
||||
|
||||
return BackoffFunc(func() (time.Duration, bool) {
|
||||
val, stop := next.Next()
|
||||
if stop {
|
||||
return 0, true
|
||||
}
|
||||
|
||||
if j <= 0 {
|
||||
return val, false
|
||||
}
|
||||
|
||||
// Get a value between -j and j, the convert to a percentage
|
||||
top := r.Int63n(int64(j)*2) - int64(j)
|
||||
top := rand.Int64N(int64(j)*2) - int64(j)
|
||||
pct := 1 - float64(top)/100.0
|
||||
|
||||
val = time.Duration(float64(val) * pct)
|
||||
if val < 0 {
|
||||
val = 0
|
||||
}
|
||||
val = max(time.Duration(float64(val)*pct), 0)
|
||||
return val, false
|
||||
})
|
||||
}
|
||||
|
||||
// WithFullJitter wraps a backoff function and returns a random value between
|
||||
// zero (inclusive) and the wrapped backoff's returned value (exclusive). For
|
||||
// example, if the backoff returned 20s, the value could be anywhere in [0, 20s).
|
||||
//
|
||||
// Unlike WithJitter, which applies "+/- j" around the value, full jitter
|
||||
// spreads the wait across the entire range, which spreads out retrying clients
|
||||
// more effectively. See
|
||||
// https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/.
|
||||
func WithFullJitter(next Backoff) Backoff {
|
||||
return BackoffFunc(func() (time.Duration, bool) {
|
||||
val, stop := next.Next()
|
||||
if stop {
|
||||
return 0, true
|
||||
}
|
||||
|
||||
if val <= 0 {
|
||||
return val, false
|
||||
}
|
||||
|
||||
return time.Duration(rand.Int64N(int64(val))), false
|
||||
})
|
||||
}
|
||||
|
||||
// WithMaxRetries executes the backoff function up until the maximum attempts.
|
||||
func WithMaxRetries(max uint64, next Backoff) Backoff {
|
||||
var l sync.Mutex
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ func Constant(ctx context.Context, t time.Duration, f RetryFunc) error {
|
||||
|
||||
// NewConstant creates a new constant backoff using the value t. The wait time
|
||||
// is the provided constant value. It panics if the given base is less than
|
||||
// zero.
|
||||
// or equal to zero.
|
||||
func NewConstant(t time.Duration) Backoff {
|
||||
if t <= 0 {
|
||||
panic("t must be greater than 0")
|
||||
|
||||
+11
-7
@@ -9,7 +9,7 @@ import (
|
||||
|
||||
type exponentialBackoff struct {
|
||||
base time.Duration
|
||||
attempt uint64
|
||||
attempt atomic.Uint64
|
||||
}
|
||||
|
||||
// Exponential is a wrapper around Retry that uses an exponential backoff. See
|
||||
@@ -37,11 +37,15 @@ func NewExponential(base time.Duration) Backoff {
|
||||
|
||||
// Next implements Backoff. It is safe for concurrent use.
|
||||
func (b *exponentialBackoff) Next() (time.Duration, bool) {
|
||||
next := b.base << (atomic.AddUint64(&b.attempt, 1) - 1)
|
||||
if next <= 0 {
|
||||
atomic.AddUint64(&b.attempt, ^uint64(0))
|
||||
next = math.MaxInt64
|
||||
}
|
||||
for {
|
||||
attempt := b.attempt.Load()
|
||||
next := b.base << attempt
|
||||
if next <= 0 {
|
||||
return math.MaxInt64, false
|
||||
}
|
||||
|
||||
return next, false
|
||||
if b.attempt.CompareAndSwap(attempt, attempt+1) {
|
||||
return next, false
|
||||
}
|
||||
}
|
||||
}
|
||||
+7
-9
@@ -5,13 +5,12 @@ import (
|
||||
"math"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type state [2]time.Duration
|
||||
|
||||
type fibonacciBackoff struct {
|
||||
state unsafe.Pointer
|
||||
state atomic.Pointer[state]
|
||||
}
|
||||
|
||||
// Fibonacci is a wrapper around Retry that uses a Fibonacci backoff. See
|
||||
@@ -33,23 +32,22 @@ func NewFibonacci(base time.Duration) Backoff {
|
||||
panic("base must be greater than 0")
|
||||
}
|
||||
|
||||
return &fibonacciBackoff{
|
||||
state: unsafe.Pointer(&state{0, base}),
|
||||
}
|
||||
b := new(fibonacciBackoff)
|
||||
b.state.Store(&state{0, base})
|
||||
return b
|
||||
}
|
||||
|
||||
// Next implements Backoff. It is safe for concurrent use.
|
||||
func (b *fibonacciBackoff) Next() (time.Duration, bool) {
|
||||
for {
|
||||
curr := atomic.LoadPointer(&b.state)
|
||||
currState := (*state)(curr)
|
||||
next := currState[0] + currState[1]
|
||||
curr := b.state.Load()
|
||||
next := curr[0] + curr[1]
|
||||
|
||||
if next <= 0 {
|
||||
return math.MaxInt64, false
|
||||
}
|
||||
|
||||
if atomic.CompareAndSwapPointer(&b.state, curr, unsafe.Pointer(&state{currState[1], next})) {
|
||||
if b.state.CompareAndSwap(curr, &state{curr[1], next}) {
|
||||
return next, false
|
||||
}
|
||||
}
|
||||
|
||||
-54
@@ -1,54 +0,0 @@
|
||||
package retry
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type lockedSource struct {
|
||||
src *rand.Rand
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
var _ rand.Source64 = (*lockedSource)(nil)
|
||||
|
||||
func newLockedRandom(seed int64) *lockedSource {
|
||||
return &lockedSource{src: rand.New(rand.NewSource(seed))}
|
||||
}
|
||||
|
||||
// Int63 mimics math/rand.(*Rand).Int63 with mutex locked.
|
||||
func (r *lockedSource) Int63() int64 {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.src.Int63()
|
||||
}
|
||||
|
||||
// Seed mimics math/rand.(*Rand).Seed with mutex locked.
|
||||
func (r *lockedSource) Seed(seed int64) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.src.Seed(seed)
|
||||
}
|
||||
|
||||
// Uint64 mimics math/rand.(*Rand).Uint64 with mutex locked.
|
||||
func (r *lockedSource) Uint64() uint64 {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.src.Uint64()
|
||||
}
|
||||
|
||||
// Int63n mimics math/rand.(*Rand).Int63n with mutex locked.
|
||||
func (r *lockedSource) Int63n(n int64) int64 {
|
||||
if n <= 0 {
|
||||
panic("invalid argument to Int63n")
|
||||
}
|
||||
if n&(n-1) == 0 { // n is power of two, can mask
|
||||
return r.Int63() & (n - 1)
|
||||
}
|
||||
max := int64((1 << 63) - 1 - (1<<63)%uint64(n))
|
||||
v := r.Int63()
|
||||
for v > max {
|
||||
v = r.Int63()
|
||||
}
|
||||
return v % n
|
||||
}
|
||||
+4
-13
@@ -54,10 +54,8 @@ func DoValue[T any](ctx context.Context, b Backoff, f RetryFuncValue[T]) (T, err
|
||||
|
||||
for {
|
||||
// Return immediately if ctx is canceled
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nilT, ctx.Err()
|
||||
default:
|
||||
if err := context.Cause(ctx); err != nil {
|
||||
return nilT, err
|
||||
}
|
||||
|
||||
v, err := f(ctx)
|
||||
@@ -76,20 +74,13 @@ func DoValue[T any](ctx context.Context, b Backoff, f RetryFuncValue[T]) (T, err
|
||||
return nilT, rerr.Unwrap()
|
||||
}
|
||||
|
||||
// ctx.Done() has priority, so we test it alone first
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nilT, ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
// Wait until next attempt or until the context expires. Any error will
|
||||
// be caught at the top of the loop.
|
||||
t := time.NewTimer(next)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
t.Stop()
|
||||
return nilT, ctx.Err()
|
||||
case <-t.C:
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user