From 554ebdfc9afe286652995ea942eb0641a07aee76 Mon Sep 17 00:00:00 2001 From: Armon Dadgar Date: Wed, 15 Jan 2014 12:34:17 -1000 Subject: [PATCH] Methods for random staggering --- command/agent/util.go | 6 ++++++ command/agent/util_test.go | 10 ++++++++++ 2 files changed, 16 insertions(+) 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) + } + } +}