You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
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)
|
|
}
|
|
}
|