Skip to content

Commit

Permalink
Add retry for urlopen
Browse files Browse the repository at this point in the history
Many a times open url returns errors including 503,
handling it by introducing retries.

Signed-off-by: Srikanth Aithal <[email protected]>
  • Loading branch information
Srikanth Aithal committed Dec 2, 2024
1 parent 5b65f4f commit 4378201
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions avocado/utils/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,17 @@ def url_open(url, data=None, timeout=5):
:return: file-like object.
:raises: `URLError`.
"""
try:
result = urlopen(url, data=data, timeout=timeout)
except (socket.timeout, HTTPError) as ex:
msg = f"Failed downloading file: {str(ex)}"
log.error(msg)
retry_attempt = 0
while retry_attempt < 10:
try:
result = urlopen(url, data=data, timeout=timeout)
break
except (socket.timeout, HTTPError) as ex:
msg = f"Failed downloading file: {str(ex)}"
log.error(msg)
retry_attempt+ = 1
log.debug(f"retry attempt: {retry_attempt}")
if "result" not in locals():
return None

msg = (
Expand Down

0 comments on commit 4378201

Please sign in to comment.