Guard against very small or negative rates
Pointed out by: slackpad
This commit is contained in:
parent
89311a5859
commit
485b153db2
|
@ -14,6 +14,10 @@ func RandomStagger(intv time.Duration) time.Duration {
|
||||||
// order to target an aggregate number of actions per second across the whole
|
// order to target an aggregate number of actions per second across the whole
|
||||||
// cluster.
|
// cluster.
|
||||||
func RateScaledInterval(rate float64, min time.Duration, n int) time.Duration {
|
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)
|
interval := time.Duration(float64(time.Second) * float64(n) / rate)
|
||||||
if interval < min {
|
if interval < min {
|
||||||
return min
|
return min
|
||||||
|
|
|
@ -16,7 +16,7 @@ func TestRandomStagger(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRateScaledInterval(t *testing.T) {
|
func TestRateScaledInterval(t *testing.T) {
|
||||||
min := 1 * time.Second
|
const min = 1 * time.Second
|
||||||
rate := 200.0
|
rate := 200.0
|
||||||
if v := RateScaledInterval(rate, min, 0); v != min {
|
if v := RateScaledInterval(rate, min, 0); v != min {
|
||||||
t.Fatalf("Bad: %v", v)
|
t.Fatalf("Bad: %v", v)
|
||||||
|
@ -36,4 +36,13 @@ func TestRateScaledInterval(t *testing.T) {
|
||||||
if v := RateScaledInterval(rate, min, 10000); v != 50*time.Second {
|
if v := RateScaledInterval(rate, min, 10000); v != 50*time.Second {
|
||||||
t.Fatalf("Bad: %v", v)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue