diff --git a/command/agent/util.go b/command/agent/util.go index 9d2531a938..f6ce7747e9 100644 --- a/command/agent/util.go +++ b/command/agent/util.go @@ -2,6 +2,7 @@ package agent import ( "math" + "math/rand" "time" ) @@ -23,3 +24,8 @@ func aeScale(interval time.Duration, n int) time.Duration { multiplier := math.Ceil(math.Log2(float64(n))-math.Log2(aeScaleThreshold)) + 1.0 return time.Duration(multiplier) * interval } + +// 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)) +} diff --git a/command/agent/util_test.go b/command/agent/util_test.go index d33894d31f..538d052aa2 100644 --- a/command/agent/util_test.go +++ b/command/agent/util_test.go @@ -20,3 +20,13 @@ func TestAEScale(t *testing.T) { t.Fatalf("Bad: %v", v) } } + +func TestRandomStagger(t *testing.T) { + intv := time.Minute + for i := 0; i < 10; i++ { + stagger := randomStagger(intv) + if stagger < 0 || stagger >= intv { + t.Fatalf("Bad: %v", stagger) + } + } +}