github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/helper/cluster.go (about) 1 package helper 2 3 import ( 4 "math/rand" 5 "time" 6 ) 7 8 const ( 9 // minRate is the minimum rate at which we allow an action to be performed 10 // across the whole cluster. The value is once a day: 1 / (1 * time.Day) 11 minRate = 1.0 / 86400 12 ) 13 14 // RandomStagger returns an interval between 0 and the duration 15 func RandomStagger(interval time.Duration) time.Duration { 16 if interval <= 0 { 17 return 0 18 } 19 return time.Duration(uint64(rand.Int63()) % uint64(interval)) 20 } 21 22 // RateScaledInterval is used to choose an interval to perform an action in 23 // order to target an aggregate number of actions per second across the whole 24 // cluster. 25 func RateScaledInterval(rate float64, min time.Duration, n int) time.Duration { 26 if rate <= minRate { 27 return min 28 } 29 interval := time.Duration(float64(time.Second) * float64(n) / rate) 30 if interval < min { 31 return min 32 } 33 34 return interval 35 }