Skip to content

Commit

Permalink
Change backoff to not be cumulative
Browse files Browse the repository at this point in the history
  • Loading branch information
gregfurman committed Dec 24, 2024
1 parent 9fd0712 commit 25468a4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
4 changes: 2 additions & 2 deletions internal/bundle/strict/processor_retry.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func wrapWithRetry(p iprocessor.V1, mgr bundle.NewManagement, maxRetries int) ip
backoffCtor := func() backoff.BackOff {
boff := backoff.NewExponentialBackOff()
boff.InitialInterval = time.Millisecond * 100
boff.MaxInterval = time.Second * 3
boff.MaxInterval = time.Second * 60
boff.MaxElapsedTime = 0
return boff
}
Expand Down Expand Up @@ -116,7 +116,7 @@ errorChecks:
).Trace("Error occurred, sleeping for next backoff period.")

select {
case <-time.After(r.backoffDuration):
case <-time.After(nextSleep):
return nil, err
case <-ctx.Done():
return nil, ctx.Err()
Expand Down
20 changes: 7 additions & 13 deletions internal/bundle/strict/processor_retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,17 @@ func TestProcessorWrapWithRetry(t *testing.T) {
msgs, err = retryProc.ProcessBatch(tCtx, msg)
require.Error(t, err, "Expected error on third attempt")
require.Empty(t, msgs)
end := time.Now()

elapsedTime := time.Since(start)
expectedMinimum := 200 * time.Millisecond // [200ms, 600ms]
require.GreaterOrEqualf(t, elapsedTime, expectedMinimum, "Total wait duration should be at least %v, got %v", expectedMinimum, elapsedTime)

msgs, err = retryProc.ProcessBatch(tCtx, msg)
require.NoError(t, err, "Expected no error as maxiumum retries exceeded")
require.Len(t, msgs, 1)

require.Equal(t, 4, mock.retries,
"Expected exactly %d attempts", 4)

expectedTotal := 1 * time.Second
require.WithinDurationf(t, end, start, expectedTotal,
"Total duration should be at least %v, got %v",
expectedTotal, end)
}

func TestProcessorWrapWithRetryMultiMessage(t *testing.T) {
Expand Down Expand Up @@ -101,18 +99,14 @@ func TestProcessorWrapWithRetryMultiMessage(t *testing.T) {
require.Error(t, err, "Expected error on fifth attempt")
require.Empty(t, msgs)

end := time.Now()
elapsedTime := time.Since(start)
expectedMinimum := 800 * time.Millisecond // [800ms, 2400ms]
require.GreaterOrEqualf(t, elapsedTime, expectedMinimum, "Total wait duration should be at least %v, got %v", expectedMinimum, elapsedTime)

msgs, err = retryProc.ProcessBatch(tCtx, msg)
require.NoError(t, err, "Expected no error as maxiumum retries exceeded")
require.Len(t, msgs, 1)

require.Equal(t, 6, mock.retries,
"Expected exactly %d attempts", 6)

expectedTotal := 4 * time.Second
require.WithinDurationf(t, end, start, expectedTotal,
"Total duration should be at least %v, got %v",
expectedTotal, end)

}

0 comments on commit 25468a4

Please sign in to comment.