another attempt at downloader retry behavior

This commit changes the general behavior from
'Retry on every exception and abort on DownloadError' to
'Only retry on DownloadRetry exceptions and abort on every other one'

The previous version would have retried on several states which
would have no chance of ever succeeding (invalid URLs, etc.)
This commit is contained in:
Mike Fährmann
2017-12-06 22:35:05 +01:00
parent 291369eab2
commit ebe9b0a04c
3 changed files with 17 additions and 13 deletions

View File

@@ -74,14 +74,14 @@ class DownloaderBase():
# connect to (remote) source # connect to (remote) source
try: try:
offset, size = self.connect(url, filesize) offset, size = self.connect(url, filesize)
except exception.DownloadError as exc: except exception.DownloadRetry as exc:
self.out.error(pathfmt.path, exc, 0, 0) msg = exc
return False continue
except exception.DownloadComplete: except exception.DownloadComplete:
break break
except Exception as exc: except Exception as exc:
msg = exc self.out.error(pathfmt.path, exc, 0, 0)
continue return False
# check response # check response
if not offset: if not offset:

View File

@@ -10,6 +10,7 @@
import time import time
import mimetypes import mimetypes
from requests.exceptions import ConnectionError, Timeout
from .common import DownloaderBase from .common import DownloaderBase
from .. import util, exception from .. import util, exception
@@ -38,9 +39,12 @@ class Downloader(DownloaderBase):
if offset: if offset:
headers["Range"] = "bytes={}-".format(offset) headers["Range"] = "bytes={}-".format(offset)
self.response = self.session.request( try:
"GET", url, stream=True, headers=headers, allow_redirects=True, self.response = self.session.request(
timeout=self.timeout, verify=self.verify) "GET", url, stream=True, headers=headers, allow_redirects=True,
timeout=self.timeout, verify=self.verify)
except (ConnectionError, Timeout) as exc:
raise exception.DownloadRetry(exc)
code = self.response.status_code code = self.response.status_code
if code == 200: # OK if code == 200: # OK
@@ -50,9 +54,9 @@ class Downloader(DownloaderBase):
size = self.response.headers["Content-Range"].rpartition("/")[2] size = self.response.headers["Content-Range"].rpartition("/")[2]
elif code == 416: # Requested Range Not Satisfiable elif code == 416: # Requested Range Not Satisfiable
raise exception.DownloadComplete() raise exception.DownloadComplete()
elif 400 <= code < 500 and code != 429: # Client Error elif code == 429 or 500 <= code < 600: # Server Error
raise exception.DownloadError( raise exception.DownloadRetry(
"{} Client Error: {} for url: {}".format( "{} Server Error: {} for url: {}".format(
code, self.response.reason, url)) code, self.response.reason, url))
else: else:
self.response.raise_for_status() self.response.raise_for_status()

View File

@@ -50,8 +50,8 @@ class HttpError(ExtractionError):
"""HTTP request during extraction failed""" """HTTP request during extraction failed"""
class DownloadError(GalleryDLException): class DownloadRetry(GalleryDLException):
"""Error during file download""" """Download attempt failed and should be retried"""
class DownloadComplete(GalleryDLException): class DownloadComplete(GalleryDLException):