File size: 830 Bytes
ea775bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | package scanloop
import (
"math/rand/v2"
"time"
)
const (
// DefaultMinInterval and DefaultJitterRange define the shared scan cadence.
DefaultMinInterval = 13 * time.Second
DefaultJitterRange = 4 * time.Second
)
// Run executes fn at a jittered interval until stopCh is closed.
// The interval is: minInterval + random([0, jitterRange)).
func Run(stopCh <-chan struct{}, minInterval, jitterRange time.Duration, fn func()) {
if minInterval <= 0 {
minInterval = time.Second
}
if jitterRange < 0 {
jitterRange = 0
}
timer := time.NewTimer(0)
defer timer.Stop()
<-timer.C // drain initial fire
for {
interval := minInterval
if jitterRange > 0 {
interval += time.Duration(rand.Int64N(int64(jitterRange)))
}
timer.Reset(interval)
select {
case <-stopCh:
return
case <-timer.C:
}
fn()
}
}
|