30 lines
509 B
Go
30 lines
509 B
Go
package client
|
|
|
|
import (
|
|
"math/rand/v2"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
BackoffMin = 1 * time.Second
|
|
BackoffMax = 30 * time.Second
|
|
)
|
|
|
|
// Delay computes exponential backoff with ±20% jitter.
|
|
// attempt starts at 1 for the first failed try.
|
|
func Delay(attempt int) time.Duration {
|
|
if attempt < 1 {
|
|
attempt = 1
|
|
}
|
|
d := BackoffMin
|
|
for i := 1; i < attempt; i++ {
|
|
d *= 2
|
|
if d >= BackoffMax {
|
|
d = BackoffMax
|
|
break
|
|
}
|
|
}
|
|
jitter := 1.0 + (rand.Float64()*0.4 - 0.2)
|
|
return time.Duration(float64(d) * jitter)
|
|
}
|