Skip to content
Myr

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

3 files+31 added−5 removed

modified:requests/adapters.py+13 added-4 removed
24 unmodified lines
25from .packages.urllib3.exceptions import ProxyError as _ProxyError26from .packages.urllib3.exceptions import ProtocolError27from .packages.urllib3.exceptions import ReadTimeoutError28from .packages.urllib3.exceptions import SSLError as _SSLError29from .packages.urllib3.exceptions import ResponseError30from .cookies import extract_cookies_to_jar31from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError,31 ProxyError)32 ProxyError, RetryError)33from .auth import _basic_auth_str3435DEFAULT_POOLBLOCK = False36DEFAULT_POOLSIZE = 10
26 unmodified lines
63 :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.7172 Usage::73
7 unmodified lines
8182 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 = {}9192 super(HTTPAdapter, self).__init__()
272 unmodified lines
365 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 )372373 # Send the request.
43 unmodified lines
417 except MaxRetryError as e:418 if isinstance(e.reason, ConnectTimeoutError):419 raise ConnectTimeout(e, request=request)420421 if isinstance(e.reason, ResponseError):422 raise RetryError(e, request=request)423424 raise ConnectionError(e, request=request)425426 except _ProxyError as e:427 raise ProxyError(e)
10 unmodified lines
modified:requests/exceptions.py+5 added
88 unmodified lines
8990class ContentDecodingError(RequestException, BaseHTTPError):91 """Failed to decode response content"""929394class StreamConsumedError(RequestException, TypeError):95 """The content for this response was already consumed"""969798class RetryError(RequestException):99 """Custom retries logic failed"""
modified:test_requests.py+13 added-1 removed
18 unmodified lines
19 Morsel, cookielib, getproxies, str, urljoin, urlparse, is_py3, builtin_str)20from requests.cookies import cookiejar_from_dict, morsel_to_cookie21from requests.exceptions import (ConnectionError, ConnectTimeout,22 InvalidSchema, InvalidURL, MissingSchema,23 ReadTimeout, Timeout)23 ReadTimeout, Timeout, RetryError)24from requests.models import PreparedRequest25from requests.structures import CaseInsensitiveDict26from requests.sessions import SessionRedirectMixin27from requests.models import urlencode
1491 unmodified lines
1519 cookies={'foo': 'bar'}1520 )1521 assert_copy(p, p.copy())152215231524def test_prepare_unicode_url():1525 p = PreparedRequest()1526 p.prepare(1527 method='GET',
1 unmodified line
1529 hooks=[]1530 )1531 assert_copy(p, p.copy())153215331534def 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 )))15401541 with pytest.raises(RetryError):1542 s.get('https://httpbin.org/status/500')15431544if __name__ == '__main__':1545 unittest.main()