c470553b6b
Consolidate code duplication and tests into a single lib package. Most of these functions were from various **/util.go functions that couldn't be imported due to cyclic imports. The consul/lib package is intended to be a terminal node in an import DAG and a place to stash various consul-only helper functions. Pulled in hashicorp/go-uuid instead of consolidating UUID access.
24 lines
599 B
Go
24 lines
599 B
Go
package lib
|
|
|
|
import (
|
|
"math/rand"
|
|
"time"
|
|
)
|
|
|
|
// Returns a random stagger interval between 0 and the duration
|
|
func RandomStagger(intv time.Duration) time.Duration {
|
|
return time.Duration(uint64(rand.Int63()) % uint64(intv))
|
|
}
|
|
|
|
// RateScaledInterval is used to choose an interval to perform an action in
|
|
// order to target an aggregate number of actions per second across the whole
|
|
// cluster.
|
|
func RateScaledInterval(rate float64, min time.Duration, n int) time.Duration {
|
|
interval := time.Duration(float64(time.Second) * float64(n) / rate)
|
|
if interval < min {
|
|
return min
|
|
}
|
|
|
|
return interval
|
|
}
|