Guard against very small or negative rates

Pointed out by: slackpad
This commit is contained in:
Sean Chittenden 2016-03-25 13:26:29 -07:00
parent d9251e30c8
commit 4fec6a9608
2 changed files with 14 additions and 1 deletions

View File

@ -14,6 +14,10 @@ func RandomStagger(intv time.Duration) time.Duration {
// 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 {
const minRate = 1 / 86400 // 1/(1 * time.Day)
if rate <= minRate {
return min
}
interval := time.Duration(float64(time.Second) * float64(n) / rate)
if interval < min {
return min

View File

@ -16,7 +16,7 @@ func TestRandomStagger(t *testing.T) {
}
func TestRateScaledInterval(t *testing.T) {
min := 1 * time.Second
const min = 1 * time.Second
rate := 200.0
if v := RateScaledInterval(rate, min, 0); v != min {
t.Fatalf("Bad: %v", v)
@ -36,4 +36,13 @@ func TestRateScaledInterval(t *testing.T) {
if v := RateScaledInterval(rate, min, 10000); v != 50*time.Second {
t.Fatalf("Bad: %v", v)
}
if v := RateScaledInterval(0, min, 10000); v != min {
t.Fatalf("Bad: %v", v)
}
if v := RateScaledInterval(0.0, min, 10000); v != min {
t.Fatalf("Bad: %v", v)
}
if v := RateScaledInterval(-1, min, 10000); v != min {
t.Fatalf("Bad: %v", v)
}
}