Skip to content

Commit

Permalink
Merge pull request #55 from smartystreets/eric/fix-retry-logic
Browse files Browse the repository at this point in the history
Eric/fix retry logic
  • Loading branch information
RyanLCox1 authored Jul 9, 2024
2 parents 2772737 + 7296c52 commit dd2bbf1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
10 changes: 5 additions & 5 deletions smartystreets_python_sdk/retry_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@

class RetrySender:
MAX_BACKOFF_DURATION = 10
STATUS_OK = 200
TOO_MANY_REQUESTS = 429
STATUS_TO_RETRY = [408, 500, 502, 503, 504]


def __init__(self, max_retries, inner):
self.inner = inner
Expand All @@ -16,9 +17,6 @@ def send(self, request):
response = self.inner.send(request)

for i in range(self.max_retries):
if response.status_code == RetrySender.STATUS_OK:
break

if response.status_code == RetrySender.TOO_MANY_REQUESTS:
backoff_seconds = 10
retry_after = response.getHeader("Retry-After")
Expand All @@ -27,8 +25,10 @@ def send(self, request):
backoff_seconds = int(retry_after)

backoff(backoff_seconds, True)
else:
elif response.status_code in RetrySender.STATUS_TO_RETRY:
backoff(i)
else:
break

response = self.inner.send(request)

Expand Down
12 changes: 10 additions & 2 deletions test/retry_sender_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,17 @@ def test_success_does_not_retry(self, mocked):

self.assertEqual(1, inner.current_status_code_index)

@patch('smartystreets_python_sdk.retry_sender.backoff', side_effect=mock_backoff)
def test_payment_required_does_not_retry(self, mocked):
inner = FailingSender([402])

send_with_retry(5, inner)

self.assertEqual(1, inner.current_status_code_index)

@patch('smartystreets_python_sdk.retry_sender.backoff', side_effect=mock_backoff)
def test_retry_until_success(self, mocked):
inner = FailingSender([401, 402, 400, 200, 500])
inner = FailingSender([408, 500, 502, 200, 504])

send_with_retry(10, inner)

Expand All @@ -43,7 +51,7 @@ def test_return_response_if_retry_limit_exceeded(self, mocked):

@patch('smartystreets_python_sdk.retry_sender.backoff', side_effect=mock_backoff)
def test_backoff_does_not_exceed_max(self, mocked):
inner = FailingSender([401, 402, 400, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 200])
inner = FailingSender([408, 408, 408, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 200])

send_with_retry(20, inner)

Expand Down

0 comments on commit dd2bbf1

Please sign in to comment.