Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eric/fix retry logic #55

Merged
merged 5 commits into from
Jul 9, 2024
Merged
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
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