Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add connect retries #1196

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 54 additions & 2 deletions httpx/_client.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import functools
import time
import typing
from types import TracebackType

import httpcore

from ._auth import Auth, BasicAuth, FunctionAuth
from ._config import (
CONNECT_RETRIES_BACKOFF_FACTOR,
DEFAULT_CONNECT_RETRIES,
DEFAULT_LIMITS,
DEFAULT_MAX_REDIRECTS,
DEFAULT_TIMEOUT_CONFIG,
IDEMPOTENT_METHODS,
UNSET,
Limits,
Proxy,
Expand All @@ -19,6 +23,8 @@
from ._content_streams import ContentStream
from ._exceptions import (
HTTPCORE_EXC_MAP,
ConnectError,
ConnectTimeout,
InvalidURL,
RemoteProtocolError,
RequestBodyUnavailable,
Expand All @@ -45,9 +51,11 @@
from ._utils import (
NetRCInfo,
URLPattern,
exponential_backoff,
get_environment_proxies,
get_logger,
same_origin,
sleep,
warn_deprecated,
)

Expand All @@ -66,6 +74,7 @@ def __init__(
cookies: CookieTypes = None,
timeout: TimeoutTypes = DEFAULT_TIMEOUT_CONFIG,
max_redirects: int = DEFAULT_MAX_REDIRECTS,
connect_retries: int = DEFAULT_CONNECT_RETRIES,
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved
base_url: URLTypes = "",
trust_env: bool = True,
):
Expand All @@ -77,6 +86,7 @@ def __init__(
self._cookies = Cookies(cookies)
self._timeout = Timeout(timeout)
self.max_redirects = max_redirects
self.connect_retries = connect_retries
self._trust_env = trust_env
self._netrc = NetRCInfo()

Expand Down Expand Up @@ -484,6 +494,8 @@ class Client(BaseClient):
* **limits** - *(optional)* The limits configuration to use.
* **max_redirects** - *(optional)* The maximum number of redirect responses
that should be followed.
* **connect_retries** - *(optional)* The maximum number of retries when trying to
establish a connection.
* **base_url** - *(optional)* A URL to use as the base when building
request URLs.
* **transport** - *(optional)* A transport class to use for sending requests
Expand All @@ -509,6 +521,7 @@ def __init__(
limits: Limits = DEFAULT_LIMITS,
pool_limits: Limits = None,
max_redirects: int = DEFAULT_MAX_REDIRECTS,
connect_retries: int = DEFAULT_CONNECT_RETRIES,
base_url: URLTypes = "",
transport: httpcore.SyncHTTPTransport = None,
app: typing.Callable = None,
Expand All @@ -521,6 +534,7 @@ def __init__(
cookies=cookies,
timeout=timeout,
max_redirects=max_redirects,
connect_retries=connect_retries,
base_url=base_url,
trust_env=trust_env,
)
Expand Down Expand Up @@ -766,7 +780,7 @@ def _send_handling_auth(
auth_flow = auth.auth_flow(request)
request = next(auth_flow)
while True:
response = self._send_single_request(request, timeout)
response = self._send_handling_retries(request, timeout)
if auth.requires_response_body:
response.read()
try:
Expand All @@ -782,6 +796,22 @@ def _send_handling_auth(
request = next_request
history.append(response)

def _send_handling_retries(self, request: Request, timeout: Timeout) -> Response:
connect_retries_left = self.connect_retries
delays = exponential_backoff(factor=CONNECT_RETRIES_BACKOFF_FACTOR)

while True:
try:
return self._send_single_request(request, timeout)
except (ConnectError, ConnectTimeout):
if request.method not in IDEMPOTENT_METHODS:
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved
raise
if connect_retries_left <= 0:
raise
connect_retries_left -= 1
delay = next(delays)
time.sleep(delay)

def _send_single_request(self, request: Request, timeout: Timeout) -> Response:
"""
Sends a single request, without handling any redirections.
Expand Down Expand Up @@ -1091,6 +1121,8 @@ class AsyncClient(BaseClient):
* **limits** - *(optional)* The limits configuration to use.
* **max_redirects** - *(optional)* The maximum number of redirect responses
that should be followed.
* **connect_retries** - *(optional)* The maximum number of retries when trying to
establish a connection.
* **base_url** - *(optional)* A URL to use as the base when building
request URLs.
* **transport** - *(optional)* A transport class to use for sending requests
Expand All @@ -1116,6 +1148,7 @@ def __init__(
limits: Limits = DEFAULT_LIMITS,
pool_limits: Limits = None,
max_redirects: int = DEFAULT_MAX_REDIRECTS,
connect_retries: int = DEFAULT_CONNECT_RETRIES,
base_url: URLTypes = "",
transport: httpcore.AsyncHTTPTransport = None,
app: typing.Callable = None,
Expand All @@ -1128,6 +1161,7 @@ def __init__(
cookies=cookies,
timeout=timeout,
max_redirects=max_redirects,
connect_retries=connect_retries,
base_url=base_url,
trust_env=trust_env,
)
Expand Down Expand Up @@ -1375,7 +1409,7 @@ async def _send_handling_auth(
auth_flow = auth.auth_flow(request)
request = next(auth_flow)
while True:
response = await self._send_single_request(request, timeout)
response = await self._send_handling_retries(request, timeout)
if auth.requires_response_body:
await response.aread()
try:
Expand All @@ -1391,6 +1425,24 @@ async def _send_handling_auth(
request = next_request
history.append(response)

async def _send_handling_retries(
self, request: Request, timeout: Timeout
) -> Response:
connect_retries_left = self.connect_retries
delays = exponential_backoff(factor=CONNECT_RETRIES_BACKOFF_FACTOR)

while True:
try:
return await self._send_single_request(request, timeout)
except (ConnectError, ConnectTimeout):
if request.method not in IDEMPOTENT_METHODS:
raise
if connect_retries_left <= 0:
raise
connect_retries_left -= 1
delay = next(delays)
await sleep(delay)

async def _send_single_request(
self, request: Request, timeout: Timeout
) -> Response:
Expand Down
3 changes: 3 additions & 0 deletions httpx/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
]
)

IDEMPOTENT_METHODS = {"GET", "HEAD", "PUT", "DELETE", "OPTIONS", "TRACE"}

logger = get_logger(__name__)

Expand Down Expand Up @@ -413,3 +414,5 @@ def __repr__(self) -> str:
DEFAULT_TIMEOUT_CONFIG = Timeout(timeout=5.0)
DEFAULT_LIMITS = Limits(max_connections=100, max_keepalive_connections=20)
DEFAULT_MAX_REDIRECTS = 20
DEFAULT_CONNECT_RETRIES = 0
CONNECT_RETRIES_BACKOFF_FACTOR = 0.5 # 0s, 0.5s, 1s, 2s, 4s, etc.
21 changes: 21 additions & 0 deletions httpx/_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import asyncio
import codecs
import collections
import itertools
import logging
import mimetypes
import netrc
Expand All @@ -14,6 +16,8 @@
from types import TracebackType
from urllib.request import getproxies

import sniffio

from ._types import PrimitiveData

if typing.TYPE_CHECKING: # pragma: no cover
Expand Down Expand Up @@ -529,3 +533,20 @@ def __eq__(self, other: typing.Any) -> bool:

def warn_deprecated(message: str) -> None: # pragma: nocover
warnings.warn(message, DeprecationWarning, stacklevel=2)


async def sleep(seconds: float) -> None:
library = sniffio.current_async_library()
if library == "trio":
import trio

await trio.sleep(seconds)
else:
assert library == "asyncio"
await asyncio.sleep(seconds)
florimondmanca marked this conversation as resolved.
Show resolved Hide resolved


def exponential_backoff(factor: float) -> typing.Iterator[float]:
yield 0
for n in itertools.count(2):
yield factor * (2 ** (n - 2))
Loading