mirror of https://github.com/status-im/consul.git
Guard against very small or negative rates
Pointed out by: slackpad
This commit is contained in:
parent
d9251e30c8
commit
4fec6a9608
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue