diff --git a/service/adapters/sqlite/pubsub.go b/service/adapters/sqlite/pubsub.go index 59adaa9..6810b9a 100644 --- a/service/adapters/sqlite/pubsub.go +++ b/service/adapters/sqlite/pubsub.go @@ -293,11 +293,18 @@ func NewDefaultBackoffManager() DefaultBackoffManager { func (d DefaultBackoffManager) GetMessageErrorBackoff(nackCount int) time.Duration { a := time.Duration(math.Pow(4, float64(nackCount-1))) * time.Second + value := min(a, maxDefaultMessageErrorBackoff) + if value <= 0 { + value = maxDefaultMessageErrorBackoff + } randFraction := 1 - randomizeMessageErrorBackoffByFraction + 2*randomizeMessageErrorBackoffByFraction*rand.Float64() - return time.Duration(float64(min(a, maxDefaultMessageErrorBackoff)) * randFraction) + return time.Duration(float64(value) * randFraction) } func (d DefaultBackoffManager) GetNoMessagesBackoff(tick int) time.Duration { a := time.Duration(math.Pow(2, float64(tick-1))) * time.Second + if a <= 0 { + return maxDefaultNoMessagesBackoff + } return min(a, maxDefaultNoMessagesBackoff) } diff --git a/service/adapters/sqlite/pubsub_test.go b/service/adapters/sqlite/pubsub_test.go index a91f0f4..104b37b 100644 --- a/service/adapters/sqlite/pubsub_test.go +++ b/service/adapters/sqlite/pubsub_test.go @@ -213,12 +213,14 @@ func TestDefaultBackoffManager_GetMessageErrorBackoffStatisticallyFallsWithinCer const numSamples = 1000 m := sqlite.NewDefaultBackoffManager() - for i := 1; i < 10; i++ { + for i := 1; i < 100; i++ { var sum float64 var avg float64 for samples := 0; samples < numSamples; samples++ { backoff := m.GetMessageErrorBackoff(i) + require.Positive(t, backoff) + if samples > numSamples/2 { require.InEpsilonf(t, avg, backoff, 0.15, "failed for i=%d and sample=%d", i, samples) } @@ -230,3 +232,12 @@ func TestDefaultBackoffManager_GetMessageErrorBackoffStatisticallyFallsWithinCer t.Log(i, time.Duration(avg)) } } + +func TestDefaultBackoffManager_NoMessagesBackoffIsPositive(t *testing.T) { + m := sqlite.NewDefaultBackoffManager() + for i := 1; i < 100; i++ { + backoff := m.GetNoMessagesBackoff(i) + t.Log(backoff) + require.Positive(t, backoff) + } +}