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

Connector caching #415

Merged
merged 9 commits into from
Jun 20, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Rename to use_dns_cache
  • Loading branch information
asvetlov committed Jun 20, 2015
commit d5f2ac87231c0bd6b55cd7f849a0262f8fd20123
30 changes: 15 additions & 15 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,7 @@ class TCPConnector(BaseConnector):
"""

def __init__(self, *, verify_ssl=True, fingerprint=None,
resolve=_marker, cache_dns=_marker,
resolve=_marker, use_dns_cache=_marker,
family=socket.AF_INET, ssl_context=None,
**kwargs):
super().__init__(**kwargs)
Expand All @@ -424,21 +424,21 @@ def __init__(self, *, verify_ssl=True, fingerprint=None,

if resolve is not _marker:
warnings.warn(("resolve parameter is deprecated, "
"use cache_dns instead"),
"use use_dns_cache instead"),
DeprecationWarning, stacklevel=2)

if cache_dns is not _marker and resolve is not _marker:
if cache_dns != resolve:
raise ValueError("cache_dns must agree with resolve")
_cache_dns = cache_dns
elif cache_dns is not _marker:
_cache_dns = cache_dns
if use_dns_cache is not _marker and resolve is not _marker:
if use_dns_cache != resolve:
raise ValueError("use_dns_cache must agree with resolve")
_use_dns_cache = use_dns_cache
elif use_dns_cache is not _marker:
_use_dns_cache = use_dns_cache
elif resolve is not _marker:
_cache_dns = resolve
_use_dns_cache = resolve
else:
_cache_dns = False
_use_dns_cache = False

self._cache_dns = _cache_dns
self._use_dns_cache = _use_dns_cache
self._cached_hosts = {}
self._ssl_context = ssl_context
self._family = family
Expand Down Expand Up @@ -486,9 +486,9 @@ def family(self):
return self._family

@property
def dns_cache(self):
def use_dns_cache(self):
"""True if local DNS caching is enabled."""
return self._cache_dns
return self._use_dns_cache

@property
def cached_hosts(self):
Expand All @@ -511,7 +511,7 @@ def resolve(self):
warnings.warn((".resolve property is deprecated, "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If resolve is deprecated, may be raise warning in __init__ as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

"use .dns_cache instead"),
DeprecationWarning, stacklevel=2)
return self.dns_cache
return self.use_dns_cache

@property
def resolved_hosts(self):
Expand All @@ -533,7 +533,7 @@ def clear_resolved_hosts(self, host=None, port=None):

@asyncio.coroutine
def _resolve_host(self, host, port):
if self._cache_dns:
if self._use_dns_cache:
key = (host, port)

if key not in self._cached_hosts:
Expand Down
6 changes: 3 additions & 3 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ BaseConnector
TCPConnector
^^^^^^^^^^^^

.. class:: TCPConnector(*, verify_ssl=True, fingerprint=None, cache_dns=False, \
.. class:: TCPConnector(*, verify_ssl=True, fingerprint=None, use_dns_cache=False, \
family=socket.AF_INET, \
ssl_context=None, conn_timeout=None, \
keepalive_timeout=30, limit=None, share_cookies=False, \
Expand Down Expand Up @@ -492,7 +492,7 @@ TCPConnector

.. versionadded:: 0.16

:param bool cache_dns: use internal cache for DNS lookups, ``False``
:param bool use_dns_cache: use internal cache for DNS lookups, ``False``
by default.

Enabling an option *may* speedup connection
Expand All @@ -501,7 +501,7 @@ TCPConnector

.. versionadded:: 0.17

:param bool resolve: alias for *cache_dns* parameter.
:param bool resolve: alias for *use_dns_cache* parameter.

.. deprecated:: 0.17

Expand Down
17 changes: 9 additions & 8 deletions tests/test_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ def test_tcp_connector_ctor(self):

with self.assertWarns(DeprecationWarning):
self.assertFalse(conn.resolve)
self.assertFalse(conn.dns_cache)
self.assertFalse(conn.use_dns_cache)

self.assertEqual(conn.family, socket.AF_INET)

Expand Down Expand Up @@ -785,19 +785,20 @@ def test_connector_cookie_deprecation(self):

def test_ambiguous_ctor_params(self):
with self.assertRaises(ValueError):
aiohttp.TCPConnector(resolve=True, cache_dns=False, loop=self.loop)
aiohttp.TCPConnector(resolve=True, use_dns_cache=False,
loop=self.loop)

def test_both_resolve_and_cache_dns(self):
conn = aiohttp.TCPConnector(resolve=True, cache_dns=True,
def test_both_resolve_and_use_dns_cache(self):
conn = aiohttp.TCPConnector(resolve=True, use_dns_cache=True,
loop=self.loop)
self.assertTrue(conn.dns_cache)
self.assertTrue(conn.use_dns_cache)
with self.assertWarns(DeprecationWarning):
self.assertTrue(conn.resolve)

def test_both_cache_dns_only(self):
conn = aiohttp.TCPConnector(cache_dns=True,
def test_both_use_dns_cache_only(self):
conn = aiohttp.TCPConnector(use_dns_cache=True,
loop=self.loop)
self.assertTrue(conn.dns_cache)
self.assertTrue(conn.use_dns_cache)
with self.assertWarns(DeprecationWarning):
self.assertTrue(conn.resolve)

Expand Down