Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions optimizely/odp/odp_event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def _flush_batch(self) -> None:
self.logger.debug(f'ODP event queue: flushing batch size {batch_len}.')
should_retry = False
initial_retry_interval = 0.2 # 200ms
max_retry_interval = 1.0 # 1 second
max_retry_interval = 3.0 # 3 seconds

for i in range(1 + self.retry_count):
try:
Expand All @@ -178,7 +178,7 @@ def _flush_batch(self) -> None:
if not should_retry:
break
if i < self.retry_count:
# Exponential backoff: 200ms, 400ms, 800ms, ... capped at 1s
# Exponential backoff: 200ms, 400ms, 800ms, 1.6s, ... capped at 3s
delay = initial_retry_interval * (2 ** i)
if delay > max_retry_interval:
delay = max_retry_interval
Expand Down
38 changes: 38 additions & 0 deletions tests/test_odp_event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,44 @@ def test_odp_event_manager_retry_success(self, *args):
self.assertStrictTrue(event_manager.is_running)
event_manager.stop()

def test_odp_event_manager_retry_max_interval_cap(self, *args):
"""Test that retry delays are properly capped at 3 seconds with exponential backoff."""
mock_logger = mock.Mock()
event_manager = OdpEventManager(mock_logger)
# Set higher retry count to test the 3-second cap
event_manager.retry_count = 6
event_manager.start(self.odp_config)

number_of_tries = event_manager.retry_count + 1 # 7 total attempts

with mock.patch.object(
event_manager.api_manager, 'send_odp_events', new_callable=CopyingMock, return_value=True
) as mock_send, mock.patch('time.sleep') as mock_sleep:
event_manager.send_event(**self.events[0])
event_manager.send_event(**self.events[1])
event_manager.flush()
event_manager.event_queue.join()

mock_send.assert_has_calls(
[mock.call(self.api_key, self.api_host, self.processed_events)] * number_of_tries
)
self.assertEqual(len(event_manager._current_batch), 0)
# Verify exponential backoff with 3s cap: 0.2s, 0.4s, 0.8s, 1.6s, 3.0s (capped), 3.0s (capped)
expected_delays = [
mock.call(0.2), # 2^0 * 0.2 = 0.2
mock.call(0.4), # 2^1 * 0.2 = 0.4
mock.call(0.8), # 2^2 * 0.2 = 0.8
mock.call(1.6), # 2^3 * 0.2 = 1.6
mock.call(3.0), # 2^4 * 0.2 = 3.2, capped at 3.0
mock.call(3.0) # 2^5 * 0.2 = 6.4, capped at 3.0
]
mock_sleep.assert_has_calls(expected_delays)
mock_logger.debug.assert_any_call('Error dispatching ODP events, retrying after 3.0s.')
mock_logger.error.assert_called_once_with(
f'ODP event send failed (Failed after 6 retries: {self.processed_events}).'
)
event_manager.stop()

def test_odp_event_manager_send_failure(self, *args):
mock_logger = mock.Mock()
event_manager = OdpEventManager(mock_logger)
Expand Down