Browse Source

Methods for random staggering

pull/19/head
Armon Dadgar 11 years ago
parent
commit
554ebdfc9a
  1. 6
      command/agent/util.go
  2. 10
      command/agent/util_test.go

6
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))
}

10
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)
}
}
}

Loading…
Cancel
Save