Skip to content

Commit

Permalink
Apply suggestions from code review: improved docstring, use of %s for…
Browse files Browse the repository at this point in the history
… Retry-After= etc

Co-authored-by: Isaac To <[email protected]>
  • Loading branch information
yarikoptic and candleindark authored Feb 11, 2025
1 parent b2736e4 commit d8bd104
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
18 changes: 16 additions & 2 deletions dandi/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,21 @@ def _check_attempts_and_sleep(
attempts_allowed: int,
downloaded_in_attempt: int = 0,
) -> int | None:
"""Check if we should retry the download, sleep if still allowed,
"""
Check if we should retry the download, sleep if still allowed,
and return potentially adjusted 'attempts_allowed'
:param path: Destination of the download
:param exc: Exception raised during the last download attempt
:param attempt: The index of the last download attempt
:param attempts_allowed: The number of download attempts currently allowed
:param downloaded_in_attempt: The number of bytes downloaded in the last attempt
:returns: The number of download attempts allowed, potentially adjusted, if download
should be retried. None if download should not be retried.
Note: If download should be retried, this function sleeps before returning.
otherwise, it returns immediately.
"""
and return potentially adjusted 'attempts_allowed'
"""
sleep_amount: float | None = None
Expand All @@ -1107,7 +1121,7 @@ def _check_attempts_and_sleep(
)
attempts_allowed += 1
if attempt >= attempts_allowed:
lgr.debug("%s - download failed after %d attempts: %s", path, attempt, exc)
lgr.debug("%s - download failed after %d attempts: %s", path, attempt + 1, exc)
return None
if exc.response is not None:
if exc.response.status_code not in (
Expand Down
6 changes: 3 additions & 3 deletions dandi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ def get_retry_after(response: requests.Response) -> Optional[int]:
# and continue with "if_unparsable" sleep logic
sleep_amount = None
lgr.warning(
"response %d has incorrect date in Retry-After=%r: %s. " "Returning %r",
"response %d has incorrect date in Retry-After=%s: %s. Returning %r",
response.status_code,
retry_after,
exc_ve,
Expand All @@ -937,7 +937,7 @@ def get_retry_after(response: requests.Response) -> Optional[int]:
elif sleep_amount < 0:
sleep_amount = None
lgr.warning(
"date in Retry-After=%r is in the past (current is %r). "
"date in Retry-After=%s is in the past (current is %r). "
"Returning %r",
retry_after,
current_date,
Expand All @@ -946,7 +946,7 @@ def get_retry_after(response: requests.Response) -> Optional[int]:
elif sleep_amount > 7 * 24 * 60 * 60: # week
sleep_amount = None
lgr.warning(
"date in Retry-After=%r is over a week in the future (current is %r). "
"date in Retry-After=%s is over a week in the future (current is %r). "
"Returning %r",
retry_after,
current_date,
Expand Down

0 comments on commit d8bd104

Please sign in to comment.