From 4fec6a9608889843893baee5613590b80edf39cb Mon Sep 17 00:00:00 2001 From: Sean Chittenden Date: Fri, 25 Mar 2016 13:26:29 -0700 Subject: [PATCH] Guard against very small or negative rates Pointed out by: slackpad --- lib/cluster.go | 4 ++++ lib/cluster_test.go | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/cluster.go b/lib/cluster.go index 0062c61375..33a7788c78 100644 --- a/lib/cluster.go +++ b/lib/cluster.go @@ -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 diff --git a/lib/cluster_test.go b/lib/cluster_test.go index 40949d0204..d517e4677f 100644 --- a/lib/cluster_test.go +++ b/lib/cluster_test.go @@ -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) + } }