Let tenacity.retry look for our EAGAIN instead of plain error.

This commit is contained in:
Ethan Dalool 2020-03-23 19:27:00 -07:00
parent 13595f724a
commit 5c2aa09db4

View file

@ -17,6 +17,7 @@ import shutil
import requests import requests
from tenacity import retry, wait_exponential, retry_if_exception_type from tenacity import retry, wait_exponential, retry_if_exception_type
from . import errors
from .errors import ValidationError, RequestError, error_for_code from .errors import ValidationError, RequestError, error_for_code
from .crypto import ( from .crypto import (
a32_to_base64, encrypt_key, base64_url_encode, encrypt_attr, base64_to_a32, a32_to_base64, encrypt_key, base64_url_encode, encrypt_attr, base64_to_a32,
@ -43,7 +44,7 @@ class Mega:
self.options = options self.options = options
@retry( @retry(
retry=retry_if_exception_type(RuntimeError), retry=retry_if_exception_type(errors.EAGAIN),
wait=wait_exponential(multiplier=2, min=2, max=60) wait=wait_exponential(multiplier=2, min=2, max=60)
) )
def _api_request(self, data, params={}): def _api_request(self, data, params={}):
@ -67,11 +68,10 @@ class Mega:
timeout=self.timeout, timeout=self.timeout,
) )
json_resp = json.loads(req.text) json_resp = json.loads(req.text)
if isinstance(json_resp, list) and isinstance(json_resp[0], int):
json_resp = json_resp[0]
if isinstance(json_resp, int): if isinstance(json_resp, int):
if json_resp == -3: # If this raises EAGAIN it'll be caught by tenacity retry.
msg = 'Request failed, retrying'
logger.info(msg)
raise RuntimeError(msg)
raise error_for_code(json_resp) raise error_for_code(json_resp)
return json_resp[0] return json_resp[0]