Merge pull request #2216 from sigmavirus24/retries-take-2
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Retries logic
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 7aa6c62, adf475e · Browse files · 3 files·+31 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+31 added−5 removed
- other:+31 added−5 removed
Provenance
- Commit
90857b8- Tree
016cc58- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent 1
7aa6c62- Parent 2
adf475e
3 files changed+31 added−5 removed
modified:requests/adapters.py+13 added-4 removed
24 unmodified lines25 from .packages.urllib3.exceptions import ProxyError as _ProxyError26 from .packages.urllib3.exceptions import ProtocolError27 from .packages.urllib3.exceptions import ReadTimeoutError28 from .packages.urllib3.exceptions import SSLError as _SSLError29+from .packages.urllib3.exceptions import ResponseError30 from .cookies import extract_cookies_to_jar31 from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError,31- ProxyError)32+ ProxyError, RetryError)33 from .auth import _basic_auth_str34 35 DEFAULT_POOLBLOCK = False36 DEFAULT_POOLSIZE = 1026 unmodified lines63 :param int max_retries: The maximum number of retries each connection64 should attempt. Note, this applies only to failed DNS lookups, socket65 connections and connection timeouts, never to requests where data has66 made it to the server. By default, Requests does not retry failed66- connections.67+ connections. If you need granular control over the conditions under68+ which we retry a request, import urllib3's ``Retry`` class and pass69+ that instead.70 :param pool_block: Whether the connection pool should block for connections.71 72 Usage::73 7 unmodified lines81 82 def __init__(self, pool_connections=DEFAULT_POOLSIZE,83 pool_maxsize=DEFAULT_POOLSIZE, max_retries=DEFAULT_RETRIES,84 pool_block=DEFAULT_POOLBLOCK):82- self.max_retries = max_retries85+ if max_retries == DEFAULT_RETRIES:86+ self.max_retries = Retry(0, read=False)87+ else:88+ self.max_retries = Retry.from_int(max_retries)89 self.config = {}90 self.proxy_manager = {}91 92 super(HTTPAdapter, self).__init__()272 unmodified lines365 redirect=False,366 assert_same_host=False,367 preload_content=False,368 decode_content=False,363- retries=Retry(self.max_retries, read=False),369+ retries=self.max_retries,370 timeout=timeout371 )372 373 # Send the request.43 unmodified lines417 except MaxRetryError as e:418 if isinstance(e.reason, ConnectTimeoutError):419 raise ConnectTimeout(e, request=request)420 421+ if isinstance(e.reason, ResponseError):422+ raise RetryError(e, request=request)423+424 raise ConnectionError(e, request=request)425 426 except _ProxyError as e:427 raise ProxyError(e)10 unmodified linesmodified:requests/exceptions.py+5 added
88 unmodified lines89 90 class ContentDecodingError(RequestException, BaseHTTPError):91 """Failed to decode response content"""92 93+94 class StreamConsumedError(RequestException, TypeError):95 """The content for this response was already consumed"""96+97+98+class RetryError(RequestException):99+ """Custom retries logic failed"""modified:test_requests.py+13 added-1 removed
18 unmodified lines19 Morsel, cookielib, getproxies, str, urljoin, urlparse, is_py3, builtin_str)20 from requests.cookies import cookiejar_from_dict, morsel_to_cookie21 from requests.exceptions import (ConnectionError, ConnectTimeout,22 InvalidSchema, InvalidURL, MissingSchema,23- ReadTimeout, Timeout)23+ ReadTimeout, Timeout, RetryError)24 from requests.models import PreparedRequest25 from requests.structures import CaseInsensitiveDict26 from requests.sessions import SessionRedirectMixin27 from requests.models import urlencode1491 unmodified lines1519 cookies={'foo': 'bar'}1520 )1521 assert_copy(p, p.copy())1522 1523+1524 def test_prepare_unicode_url():1525 p = PreparedRequest()1526 p.prepare(1527 method='GET',1 unmodified line1529 hooks=[]1530 )1531 assert_copy(p, p.copy())1532 1533+1534+def test_urllib3_retries():1535+ from requests.packages.urllib3.util import Retry1536+ s = requests.Session()1537+ s.mount('https://', HTTPAdapter(max_retries=Retry(1538+ total=2, status_forcelist=[500]1539+ )))1540+1541+ with pytest.raises(RetryError):1542+ s.get('https://httpbin.org/status/500')1543+1544 if __name__ == '__main__':1545 unittest.main()