Skip to content

Commit

Permalink
Add Happy Eyeballs support to TCPConnector
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-v committed Feb 13, 2021
1 parent 10c8ce9 commit 44e4a76
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGES/4451.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add Happy Eyeballs support to ``TCPConnector`` and enable it by default.
Note, if Python 3.7 is used, the feature is disabled and related arguments are ignored.
21 changes: 20 additions & 1 deletion aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
)
from .client_proto import ResponseHandler
from .client_reqrep import SSL_ALLOWED_TYPES, ClientRequest, Fingerprint
from .helpers import _SENTINEL, ceil_timeout, is_ip_address, sentinel
from .helpers import _SENTINEL, PY_38, ceil_timeout, is_ip_address, sentinel
from .http import RESPONSES
from .locks import EventResultOrError
from .resolver import DefaultResolver
Expand Down Expand Up @@ -700,6 +700,13 @@ class TCPConnector(BaseConnector):
resolver
use_dns_cache - Use memory cache for DNS lookups.
ttl_dns_cache - Max seconds having cached a DNS entry, None forever.
happy_eyeballs_delay - Number of seconds to wait before spawning an
RFC 8305 Happy Eyeballs connection. None value disables Happy
Eyeballs support.
This is ignored if Python 3.7 is used.
interleave_address_families - Controls address reordering when a
host name resolves to multiple IP addresses.
This is ignored if Python 3.7 is used.
family - socket address family
local_addr - local tuple of (host, port) to bind socket to
Expand All @@ -718,6 +725,8 @@ def __init__(
*,
use_dns_cache: bool = True,
ttl_dns_cache: Optional[int] = 10,
happy_eyeballs_delay: float = 0.25, # 250 ms recommended by RFC 8305.
interleave_address_families: int = 1,
family: int = 0,
ssl: Union[None, bool, Fingerprint, SSLContext] = None,
local_addr: Optional[Tuple[str, int]] = None,
Expand Down Expand Up @@ -748,6 +757,8 @@ def __init__(

self._use_dns_cache = use_dns_cache
self._cached_hosts = _DNSCacheTable(ttl=ttl_dns_cache)
self._happy_eyeballs_delay = happy_eyeballs_delay
self._interleave_address_families = interleave_address_families
self._throttle_dns_events = (
{}
) # type: Dict[Tuple[str, int], EventResultOrError]
Expand Down Expand Up @@ -994,6 +1005,13 @@ def drop_exception(fut: "asyncio.Future[List[Dict[str, Any]]]") -> None:
port = hinfo["port"]

try:
if PY_38:
happy_eyeballs_kwargs = {
"happy_eyeballs_delay": self._happy_eyeballs_delay,
"interleave": self._interleave_address_families,
}
else:
happy_eyeballs_kwargs = {}
transp, proto = await self._wrap_create_connection(
self._factory,
host,
Expand All @@ -1007,6 +1025,7 @@ def drop_exception(fut: "asyncio.Future[List[Dict[str, Any]]]") -> None:
local_addr=self._local_addr,
req=req,
client_error=client_error,
**happy_eyeballs_kwargs,
)
except ClientConnectorError as exc:
last_exc = exc
Expand Down

0 comments on commit 44e4a76

Please sign in to comment.