]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/downloader/http.py
Standardize retry mechanism (#1649)
[yt-dlp.git] / yt_dlp / downloader / http.py
index 27d147513cded8f41a427d7ec3a8d4480d9f7f7c..95c870ee8ba8818a31c1ee8d8236fd6ad57cf7a6 100644 (file)
@@ -9,6 +9,7 @@
 from .common import FileDownloader
 from ..utils import (
     ContentTooShortError,
+    RetryManager,
     ThrottledDownload,
     XAttrMetadataError,
     XAttrUnavailableError,
@@ -72,9 +73,6 @@ class DownloadContext(dict):
 
         ctx.is_resume = ctx.resume_len > 0
 
-        count = 0
-        retries = self.params.get('retries', 0)
-
         class SucceedDownload(Exception):
             pass
 
@@ -349,9 +347,7 @@ def retry(e):
 
             if data_len is not None and byte_counter != data_len:
                 err = ContentTooShortError(byte_counter, int(data_len))
-                if count <= retries:
-                    retry(err)
-                raise err
+                retry(err)
 
             self.try_rename(ctx.tmpfilename, ctx.filename)
 
@@ -370,24 +366,20 @@ def retry(e):
 
             return True
 
-        while count <= retries:
+        for retry in RetryManager(self.params.get('retries'), self.report_retry):
             try:
                 establish_connection()
                 return download()
-            except RetryDownload as e:
-                count += 1
-                if count <= retries:
-                    self.report_retry(e.source_error, count, retries)
-                else:
-                    self.to_screen(f'[download] Got server HTTP error: {e.source_error}')
+            except RetryDownload as err:
+                retry.error = err.source_error
                 continue
             except NextFragment:
+                retry.error = None
+                retry.attempt -= 1
                 continue
             except SucceedDownload:
                 return True
             except:  # noqa: E722
                 close_stream()
                 raise
-
-        self.report_error('giving up after %s retries' % retries)
         return False