Skip to content

Commit

Permalink
Avoid instantiating Resolver when it is not necessary for DoH (#1123)
Browse files Browse the repository at this point in the history
* Avoid instantiating Resolver when bootstrap_address is present for DoH

* Avoid instantiating Resolver when hostname is ip address for DoH
  • Loading branch information
URenko authored Aug 28, 2024
1 parent c97da58 commit 3f4bf3d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 23 deletions.
2 changes: 1 addition & 1 deletion dns/_asyncio_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def __init__(
family=socket.AF_UNSPEC,
**kwargs,
):
if resolver is None:
if resolver is None and bootstrap_address is None:
# pylint: disable=import-outside-toplevel,redefined-outer-name
import dns.asyncresolver

Expand Down
2 changes: 1 addition & 1 deletion dns/_trio_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def __init__(
family=socket.AF_UNSPEC,
**kwargs,
):
if resolver is None:
if resolver is None and bootstrap_address is None:
# pylint: disable=import-outside-toplevel,redefined-outer-name
import dns.asyncresolver

Expand Down
22 changes: 12 additions & 10 deletions dns/asyncquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,21 +560,23 @@ async def https(
else:
url = where

if bootstrap_address is None:
parsed = urllib.parse.urlparse(url)
if parsed.hostname is None:
raise ValueError("no hostname in URL")
if dns.inet.is_address(parsed.hostname):
bootstrap_address = parsed.hostname
if parsed.port is not None:
port = parsed.port

if http_version == HTTPVersion.H3 or (
http_version == HTTPVersion.DEFAULT and not have_doh
):
if bootstrap_address is None:
parsed = urllib.parse.urlparse(url)
resolver = _maybe_get_resolver(resolver)
if parsed.hostname is None:
raise ValueError("no hostname in URL")
if dns.inet.is_address(parsed.hostname):
bootstrap_address = parsed.hostname
else:
answers = await resolver.resolve_name(parsed.hostname, family)
bootstrap_address = random.choice(list(answers.addresses()))
if parsed.port is not None:
port = parsed.port
assert parsed.hostname is not None # for mypy
answers = await resolver.resolve_name(parsed.hostname, family)
bootstrap_address = random.choice(list(answers.addresses()))
return await _http3(
q,
bootstrap_address,
Expand Down
24 changes: 13 additions & 11 deletions dns/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def __init__(
family=socket.AF_UNSPEC,
**kwargs,
):
if resolver is None:
if resolver is None and bootstrap_address is None:
# pylint: disable=import-outside-toplevel,redefined-outer-name
import dns.resolver

Expand Down Expand Up @@ -449,21 +449,23 @@ def https(
else:
url = where

if bootstrap_address is None:
parsed = urllib.parse.urlparse(url)
if parsed.hostname is None:
raise ValueError("no hostname in URL")
if dns.inet.is_address(parsed.hostname):
bootstrap_address = parsed.hostname
if parsed.port is not None:
port = parsed.port

if http_version == HTTPVersion.H3 or (
http_version == HTTPVersion.DEFAULT and not have_doh
):
if bootstrap_address is None:
parsed = urllib.parse.urlparse(url)
resolver = _maybe_get_resolver(resolver)
if parsed.hostname is None:
raise ValueError("no hostname in URL")
if dns.inet.is_address(parsed.hostname):
bootstrap_address = parsed.hostname
else:
answers = resolver.resolve_name(parsed.hostname, family)
bootstrap_address = random.choice(list(answers.addresses()))
if parsed.port is not None:
port = parsed.port
assert parsed.hostname is not None # for mypy
answers = resolver.resolve_name(parsed.hostname, family)
bootstrap_address = random.choice(list(answers.addresses()))
return _http3(
q,
bootstrap_address,
Expand Down

0 comments on commit 3f4bf3d

Please sign in to comment.