Skip to content

Commit

Permalink
Merge pull request #1264 from dandi/more-retry-logs
Browse files Browse the repository at this point in the history
Log more information about retried HTTP requests
  • Loading branch information
yarikoptic committed Apr 5, 2023
2 parents 0be71d3 + 522c5ec commit af5eaa0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
2 changes: 2 additions & 0 deletions dandi/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,5 @@ class DandiInstance(NamedTuple):
"additional",
"required_if_not_empty",
}

REQUEST_RETRIES = 12
26 changes: 21 additions & 5 deletions dandi/dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
from .consts import (
DRAFT,
MAX_CHUNK_SIZE,
REQUEST_RETRIES,
RETRY_STATUSES,
ZARR_DELETE_BATCH_SIZE,
DandiInstance,
Expand Down Expand Up @@ -217,13 +218,11 @@ def request(
retry=tenacity.retry_if_exception_type(
(requests.ConnectionError, requests.HTTPError)
),
stop=tenacity.stop_after_attempt(12),
stop=tenacity.stop_after_attempt(REQUEST_RETRIES),
reraise=True,
)
):
with attempt:
if attempt.retry_state.attempt_number > 1:
lgr.warning("Retrying %s %s", method.upper(), url)
result = self.session.request(
method,
url,
Expand All @@ -237,9 +236,26 @@ def request(
if result.status_code in [*RETRY_STATUSES, *retry_statuses] or (
retry_if is not None and retry_if(result)
):
if attempt.retry_state.attempt_number < REQUEST_RETRIES:
lgr.warning(
"Will retry: Error %d while sending %s request to %s: %s",
result.status_code,
method,
url,
result.text,
)
result.raise_for_status()
except Exception:
lgr.exception("HTTP connection failed")
except Exception as e:
if isinstance(e, requests.HTTPError):
lgr.error(
"HTTP request failed repeatedly: Error %d while sending %s request to %s: %s",
e.response.status_code,
method,
url,
e.response.text,
)
else:
lgr.exception("HTTP connection failed")
raise

if i > 0:
Expand Down
2 changes: 1 addition & 1 deletion dandi/tests/test_dandiapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ def test_retry_logging(caplog: pytest.LogCaptureFixture) -> None:
assert (
"dandi",
logging.WARNING,
"Retrying GET https://test.nil/api/info/",
"Will retry: Error 503 while sending GET request to https://test.nil/api/info/: ",
) in caplog.record_tuples
assert (
"dandi",
Expand Down

0 comments on commit af5eaa0

Please sign in to comment.