init monlet repo with stage 0-2 (docs, contract, agent MVP)
This commit is contained in:
29
agent/internal/client/backoff.go
Normal file
29
agent/internal/client/backoff.go
Normal file
@@ -0,0 +1,29 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user