mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-12-01 14:47:38 +00:00
d52fd73f18
This should smooth CPU and RAM usage spikes related to these periodic tasks, by reducing the probability that multiple concurrent periodic tasks are performed at the same time.
19 lines
427 B
Go
19 lines
427 B
Go
package timeutil
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/valyala/fastrand"
|
|
)
|
|
|
|
// AddJitterToDuration adds up to 10% random jitter to d and returns the resulting duration.
|
|
//
|
|
// The maximum jitter is limited by 10 seconds.
|
|
func AddJitterToDuration(d time.Duration) time.Duration {
|
|
dv := d / 10
|
|
if dv > 10*time.Second {
|
|
dv = 10 * time.Second
|
|
}
|
|
p := float64(fastrand.Uint32()) / (1 << 32)
|
|
return d + time.Duration(p*float64(dv))
|
|
}
|