Skip to content

Commit c0b3a11

Browse files
committed
address comments
1 parent 3a33118 commit c0b3a11

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

pkg/event/dispatcher.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ func (ed *QueueEventDispatcher) waitForDispatchingEventsOnClose(timeout time.Dur
120120
}
121121
}
122122

123-
// getRetryInterval calculates exponential backoff interval starting at 200ms, doubling each retry, capped at 1s
123+
// getRetryInterval calculates exponential backoff interval.
124+
// Uses bit-shift (1<<retryCount) to compute 2^retryCount for doubling: 200ms, 400ms, 800ms, ... capped at 1s.
124125
func getRetryInterval(retryCount int) time.Duration {
125-
interval := initialRetryInterval * time.Duration(1<<retryCount) // 200ms, 400ms, 800ms, ...
126+
interval := initialRetryInterval * time.Duration(1<<retryCount)
126127
return min(interval, maxRetryInterval)
127128
}
128129

pkg/odp/event/event_manager.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ const maxRetries = 3
4141
const initialRetryInterval = 200 * time.Millisecond
4242
const maxRetryInterval = 1 * time.Second
4343

44+
// getRetryInterval calculates exponential backoff interval.
45+
// Uses bit-shift (1<<retryCount) to compute 2^retryCount for doubling: 200ms, 400ms, 800ms, ... capped at 1s.
46+
func getRetryInterval(retryCount int) time.Duration {
47+
interval := initialRetryInterval * time.Duration(1<<retryCount)
48+
return min(interval, maxRetryInterval)
49+
}
50+
4451
// Manager represents the event manager.
4552
type Manager interface {
4653
// odpConfig is required here since it can be updated anytime and ticker needs to be aware of latest changes
@@ -306,13 +313,9 @@ func (bm *BatchEventManager) FlushEvents(apiKey, apiHost string) {
306313
break
307314
}
308315
retryCount++
309-
// Exponential backoff before next retry: 200ms, 400ms, 800ms, ... capped at 1s
316+
// Exponential backoff before next retry
310317
if retryCount < maxRetries {
311-
delay := initialRetryInterval * time.Duration(1<<(retryCount-1))
312-
if delay > maxRetryInterval {
313-
delay = maxRetryInterval
314-
}
315-
time.Sleep(delay)
318+
time.Sleep(getRetryInterval(retryCount - 1))
316319
}
317320
}
318321
}

pkg/odp/event/event_manager_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,3 +607,39 @@ func (m *MockEventAPIManager) SendOdpEvents(apiKey, apiHost string, events []Eve
607607
func newExecutionContext() *pkgUtils.ExecGroup {
608608
return pkgUtils.NewExecGroup(context.Background(), logging.GetLogger("", "NewExecGroup"))
609609
}
610+
611+
func TestGetRetryInterval(t *testing.T) {
612+
tests := []struct {
613+
name string
614+
retryCount int
615+
expected time.Duration
616+
}{
617+
{"first retry", 0, 200 * time.Millisecond},
618+
{"second retry", 1, 400 * time.Millisecond},
619+
{"third retry", 2, 800 * time.Millisecond},
620+
{"fourth retry capped at max", 3, 1 * time.Second},
621+
{"fifth retry capped at max", 4, 1 * time.Second},
622+
{"high retry count capped at max", 10, 1 * time.Second},
623+
}
624+
625+
for _, tt := range tests {
626+
t.Run(tt.name, func(t *testing.T) {
627+
result := getRetryInterval(tt.retryCount)
628+
if result != tt.expected {
629+
t.Errorf("getRetryInterval(%d) = %v, want %v", tt.retryCount, result, tt.expected)
630+
}
631+
})
632+
}
633+
}
634+
635+
func TestGetRetryInterval_ExponentialGrowth(t *testing.T) {
636+
// Verify exponential growth pattern: each interval should be double the previous
637+
prev := getRetryInterval(0)
638+
for i := 1; i <= 2; i++ {
639+
curr := getRetryInterval(i)
640+
if curr != prev*2 {
641+
t.Errorf("retry %d should be double retry %d: got %v, want %v", i, i-1, curr, prev*2)
642+
}
643+
prev = curr
644+
}
645+
}

pkg/utils/requester_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func TestGetRetry(t *testing.T) {
281281

282282
// With exponential backoff: 200ms + 400ms + 800ms + 1000ms = 2400ms for 4 delays (5 attempts)
283283
// Allow some tolerance for test execution overhead
284-
assert.True(t, elapsed >= 2*time.Second && elapsed <= 10*time.Second, "took %s", elapsed)
284+
assert.True(t, elapsed >= 2*time.Second && elapsed <= 4*time.Second, "took %s", elapsed)
285285

286286
httpreq = NewHTTPRequester(logging.GetLogger("", ""), Retries(3))
287287
called = 0

0 commit comments

Comments
 (0)