Skip to content

Commit 2b91382

Browse files
authored
Refactor download function for robustness and retries (#15933)
1 parent b4d72f1 commit 2b91382

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

backends/qualcomm/scripts/download_qnn_sdk.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import zipfile
1515
from typing import Dict, List, Optional, Tuple
1616

17+
import requests
18+
from requests.adapters import HTTPAdapter, Retry
1719

1820
logger = logging.getLogger(__name__)
1921
logger.addHandler(logging.NullHandler())
@@ -115,12 +117,36 @@ def _atomic_download(url: str, dest: pathlib.Path):
115117

116118

117119
def _download_archive(url: str, archive_path: pathlib.Path) -> bool:
118-
"""Download archive from URL with progress reporting."""
120+
"""Robust streaming download with retries."""
121+
119122
logger.debug("Archive will be saved to: %s", archive_path)
120123

124+
session = requests.Session()
125+
retries = Retry(
126+
total=5,
127+
backoff_factor=1.0,
128+
status_forcelist=[429, 500, 502, 503, 504],
129+
allowed_methods=["GET"],
130+
)
131+
session.mount("https://", HTTPAdapter(max_retries=retries))
132+
121133
try:
122-
urllib.request.urlretrieve(url, archive_path, _make_report_progress())
134+
with session.get(url, stream=True) as r:
135+
r.raise_for_status()
136+
137+
total = int(r.headers.get("content-length", 0))
138+
downloaded = 0
139+
chunk_size = 1024 * 1024 # 1MB
140+
141+
with open(archive_path, "wb") as f:
142+
for chunk in r.iter_content(chunk_size):
143+
if chunk:
144+
f.write(chunk)
145+
downloaded += len(chunk)
146+
_make_report_progress()(downloaded, downloaded, total)
147+
123148
logger.info("Download completed!")
149+
124150
except Exception as e:
125151
logger.exception("Error during download: %s", e)
126152
return False
@@ -131,6 +157,7 @@ def _download_archive(url: str, archive_path: pathlib.Path) -> bool:
131157
elif not archive_path.exists():
132158
logger.error("File was not downloaded!")
133159
return False
160+
134161
return True
135162

136163

backends/qualcomm/scripts/install_qnn_sdk.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ setup_android_ndk() {
2727
mkdir -p "${NDK_INSTALL_DIR}"
2828
NDK_ZIP="android-ndk-${NDK_VERSION}-linux.zip"
2929

30-
curl --retry 3 -Lo "/tmp/${NDK_ZIP}" "https://dl.google.com/android/repository/${NDK_ZIP}"
30+
curl --retry 3 --retry-delay 5 --retry-connrefused --continue-at - -Lo "/tmp/${NDK_ZIP}" "https://dl.google.com/android/repository/${NDK_ZIP}"
3131
unzip -q "/tmp/${NDK_ZIP}" -d "${NDK_INSTALL_DIR}"
3232
mv "${NDK_INSTALL_DIR}/android-ndk-${NDK_VERSION}" "${NDK_INSTALL_DIR}/ndk"
3333

0 commit comments

Comments
 (0)