mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-14 10:20:00 +08:00
32 lines
864 B
Go
32 lines
864 B
Go
|
|
package backoff
|
||
|
|
|
||
|
|
import (
|
||
|
|
"math/rand"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
type Func func(attempt int) time.Duration
|
||
|
|
|
||
|
|
// LinearWithJitter waits a set period of time, allowing for jitter (fractional adjustment).
|
||
|
|
func LinearWithJitter(waitBetween time.Duration, jitterFraction float64) Func {
|
||
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||
|
|
return func(attempt int) time.Duration {
|
||
|
|
multiplier := jitterFraction * (r.Float64()*2 - 1)
|
||
|
|
return time.Duration(float64(waitBetween) * (1 + multiplier))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Interval it waits for a fixed period of time between calls.
|
||
|
|
func Interval(interval time.Duration) Func {
|
||
|
|
return func(attempt int) time.Duration {
|
||
|
|
return interval
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Exponential produces increasing intervals for each attempt.
|
||
|
|
func Exponential(scalar time.Duration) Func {
|
||
|
|
return func(attempt int) time.Duration {
|
||
|
|
return scalar * time.Duration((1<<attempt)>>1)
|
||
|
|
}
|
||
|
|
}
|