Skip to content

Commit

Permalink
Fix backoff duration overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq committed Nov 13, 2023
1 parent d338b3a commit 489d05c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 8 additions & 1 deletion service/adapters/sqlite/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
13 changes: 12 additions & 1 deletion service/adapters/sqlite/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
}

0 comments on commit 489d05c

Please sign in to comment.