daemon/CS: randomize next query interval
@@ -16,6 +16,7 @@ package segfetcher
import (
"context"
+ "math/rand"
"net"
"time"
@@ -143,11 +144,17 @@ func (f *Fetcher) nextQuery(segs Segments) time.Time {
// nearestNextQueryTime finds the nearest next query time in the interval spanned
// by the minimum and the configured query interval.
func (f *Fetcher) nearestNextQueryTime(now, nextQuery time.Time) time.Time {
+ // Adding +-10% random jitter
+ jitterPercent := time.Duration(rand.Intn(20) - 10)
+
if earliest := now.Add(minQueryInterval); nextQuery.Before(earliest) {
- return earliest
+ jitter := minQueryInterval * jitterPercent / 100
+ return earliest.Add(jitter)
}
+
if latest := now.Add(f.QueryInterval); nextQuery.After(latest) {
- return latest
+ jitter := f.QueryInterval * jitterPercent / 100
+ return latest.Add(jitter)
}
return nextQuery
}
