Skip to content

Commit

Permalink
feat: retry requests on 502 responses (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-sentry authored Mar 8, 2024
1 parent c198e38 commit ced5094
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
14 changes: 13 additions & 1 deletion codecov_cli/helpers/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,27 @@ def backoff_time(curr_retry):
return 2 ** (curr_retry - 1)


class RetryException(Exception):
...


def retry_request(func):
def wrapper(*args, **kwargs):
retry = 0
while retry < MAX_RETRIES:
try:
return func(*args, **kwargs)
response = func(*args, **kwargs)
if response.status_code == 502:
logger.warning(
"Response status code was 502.",
extra=dict(extra_log_attributes=dict(retry=retry)),
)
raise RetryException
return response
except (
requests.exceptions.ConnectionError,
requests.exceptions.Timeout,
RetryException,
) as exp:
logger.warning(
"Request failed. Retrying",
Expand Down
18 changes: 18 additions & 0 deletions tests/helpers/test_upload_sender.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import re
from pathlib import Path

import pytest
Expand Down Expand Up @@ -304,6 +305,23 @@ def test_upload_sender_result_fail_post_400(

assert sender.warnings is not None

def test_upload_sender_result_fail_post_502(
self, mocker, mocked_responses, mocked_legacy_upload_endpoint, capsys
):
mocker.patch("codecov_cli.helpers.request.sleep")
mocked_legacy_upload_endpoint.status = 502

with pytest.raises(Exception, match="Request failed after too many retries"):
_ = UploadSender().send_upload_data(
upload_collection, random_sha, random_token, **named_upload_data
)

matcher = re.compile(
r"(warning.*((Response status code was 502)|(Request failed\. Retrying)).*(\n)?){6}"
)

assert matcher.match(capsys.readouterr().err) is not None

def test_upload_sender_result_fail_put_400(
self, mocked_responses, mocked_legacy_upload_endpoint, mocked_storage_server
):
Expand Down

0 comments on commit ced5094

Please sign in to comment.