From 6d0b9f4dace83204b3b67eca189203deb812e001 Mon Sep 17 00:00:00 2001 From: Alex Dadgar Date: Mon, 30 Oct 2017 16:46:11 -0700 Subject: [PATCH] Integer division rounding to zero for rate scaling This fixes an issue in which integer division was scaling down to zero. --- lib/cluster.go | 7 ++++++- lib/cluster_test.go | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/cluster.go b/lib/cluster.go index d65938e273..72d58b9ee3 100644 --- a/lib/cluster.go +++ b/lib/cluster.go @@ -5,6 +5,12 @@ import ( "time" ) +const ( + // minRate is the minimum rate at which we allow an action to be performed + // acorss the whole cluster. The value is once a day: 1 / (1 * time.Day) + minRate = 1.0 / 86400 +) + // DurationMinusBuffer returns a duration, minus a buffer and jitter // subtracted from the duration. This function is used primarily for // servicing Consul TTL Checks in advance of the TTL. @@ -43,7 +49,6 @@ 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 } diff --git a/lib/cluster_test.go b/lib/cluster_test.go index 6747a9698a..19a65179a4 100644 --- a/lib/cluster_test.go +++ b/lib/cluster_test.go @@ -158,6 +158,10 @@ func TestRateScaledInterval(t *testing.T) { if v := RateScaledInterval(rate, min, 10000); v != 50*time.Second { t.Fatalf("Bad: %v", v) } + halfMin := minRate / 2.0 + if v := RateScaledInterval(halfMin, min, 100); v != min { + t.Fatalf("Bad: %v", v) + } if v := RateScaledInterval(0, min, 10000); v != min { t.Fatalf("Bad: %v", v) }