-
Notifications
You must be signed in to change notification settings - Fork 0
Add download retries #198
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
Merged
Merged
Add download retries #198
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
986792d
add download retries
newokaerinasai 23ea457
Update files.py
newokaerinasai 0d6f8dc
Update files.py
newokaerinasai 4e7f38f
comments from review
newokaerinasai 4fe9509
Update constants.py
newokaerinasai 5eb7604
Update files.py
newokaerinasai 9beeb15
code style
newokaerinasai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import os | ||
| import math | ||
| import stat | ||
| import time | ||
| import uuid | ||
| import shutil | ||
| import asyncio | ||
|
|
@@ -28,13 +29,16 @@ | |
| DOWNLOAD_BLOCK_SIZE, | ||
| MAX_MULTIPART_PARTS, | ||
| TARGET_PART_SIZE_MB, | ||
| DOWNLOAD_RETRY_DELAY, | ||
| MAX_CONCURRENT_PARTS, | ||
| MAX_DOWNLOAD_RETRIES, | ||
| MULTIPART_THRESHOLD_GB, | ||
| DOWNLOAD_MAX_RETRY_DELAY, | ||
| MULTIPART_UPLOAD_TIMEOUT, | ||
| ) | ||
| from ..._resource import SyncAPIResource, AsyncAPIResource | ||
| from ..types.error import DownloadError, FileTypeError | ||
| from ..._exceptions import APIStatusError, AuthenticationError | ||
| from ..._exceptions import APIStatusError, APIConnectionError, AuthenticationError | ||
|
|
||
| log: logging.Logger = logging.getLogger(__name__) | ||
|
|
||
|
|
@@ -198,21 +202,78 @@ def download( | |
|
|
||
| assert file_size != 0, "Unable to retrieve remote file." | ||
|
|
||
| # Download with retry logic | ||
| bytes_downloaded = 0 | ||
| retry_count = 0 | ||
| retry_delay = DOWNLOAD_RETRY_DELAY | ||
|
|
||
| with tqdm( | ||
| total=file_size, | ||
| unit="B", | ||
| unit_scale=True, | ||
| desc=f"Downloading file {file_path.name}", | ||
| disable=bool(DISABLE_TQDM), | ||
| ) as pbar: | ||
| for chunk in response.iter_bytes(DOWNLOAD_BLOCK_SIZE): | ||
| pbar.update(len(chunk)) | ||
| temp_file.write(chunk) # type: ignore | ||
| while bytes_downloaded < file_size: | ||
| try: | ||
| # If this is a retry, close the previous response and create a new one with Range header | ||
| if bytes_downloaded > 0: | ||
| if 'response' in locals(): | ||
|
||
| response.close() | ||
|
|
||
| log.info(f"Resuming download from byte {bytes_downloaded}") | ||
| response = self._client.get( | ||
| path=url, | ||
| cast_to=httpx.Response, | ||
| stream=True, | ||
| options=RequestOptions( | ||
| headers={"Range": f"bytes={bytes_downloaded}-"}, | ||
| ), | ||
| ) | ||
|
|
||
| # Download chunks | ||
| for chunk in response.iter_bytes(DOWNLOAD_BLOCK_SIZE): | ||
| temp_file.write(chunk) # type: ignore | ||
| bytes_downloaded += len(chunk) | ||
| pbar.update(len(chunk)) | ||
|
|
||
| # Successfully completed download | ||
| break | ||
|
|
||
| except (httpx.RequestError, httpx.StreamError, APIConnectionError) as e: | ||
| if retry_count >= MAX_DOWNLOAD_RETRIES: | ||
| log.error(f"Download failed after {retry_count} retries") | ||
| raise DownloadError( | ||
| f"Download failed after {retry_count} retries. Last error: {str(e)}" | ||
| ) from e | ||
|
|
||
| retry_count += 1 | ||
| log.warning( | ||
| f"Download interrupted at {bytes_downloaded}/{file_size} bytes. " | ||
| f"Retry {retry_count}/{MAX_DOWNLOAD_RETRIES} in {retry_delay}s..." | ||
| ) | ||
| time.sleep(retry_delay) | ||
|
|
||
| # Exponential backoff with max delay cap | ||
| retry_delay = min(retry_delay * 2, DOWNLOAD_MAX_RETRY_DELAY) | ||
|
|
||
| except APIStatusError as e: | ||
| # For API errors, don't retry | ||
| log.error(f"API error during download: {e}") | ||
| raise APIStatusError( | ||
| "Error downloading file", | ||
| response=e.response, | ||
| body=e.response, | ||
| ) from e | ||
|
|
||
| # Close the response | ||
| if 'response' in locals(): | ||
| response.close() | ||
|
|
||
| # Raise exception if remote file size does not match downloaded file size | ||
| if os.stat(temp_file.name).st_size != file_size: | ||
| DownloadError( | ||
| f"Downloaded file size `{pbar.n}` bytes does not match remote file size `{file_size}` bytes." | ||
| raise DownloadError( | ||
| f"Downloaded file size `{bytes_downloaded}` bytes does not match remote file size `{file_size}` bytes." | ||
| ) | ||
|
|
||
| # Moves temp file to output file path | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd name DOWNLOAD_INITIAL_RETRY_DELAY according to the comment for consistency