Skip to content

Commit

Permalink
reuse resolver instance
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsasha committed Dec 3, 2024
1 parent fe73ef3 commit 164f3b7
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions checks/resolver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import enum
from os import getenv
from threading import Lock
from typing import List, cast, Tuple, Optional

import dns
Expand All @@ -10,7 +11,6 @@
from dns.rdatatype import RdataType
from dns.resolver import Resolver

# TODO: prevent recreation of resolver on every call
# TODO: see how timeouts are handled


Expand Down Expand Up @@ -57,8 +57,17 @@ def resolve(label: str, rr_type: RdataType, allow_bogus=False):
raise ValidationFailure()
return answer.rrset, dnssec_status

_resolver = None

def get_resolver():
# Resolvers are thread safe once configured
global _resolver
if not _resolver:
_resolver = _create_resolver()
return _resolver


def _create_resolver() -> Resolver:
resolver = Resolver(configure=False)
resolver.nameservers = [getenv("IPV4_IP_RESOLVER_INTERNAL_VALIDATING")]
# TODO: revert to
Expand All @@ -68,7 +77,6 @@ def get_resolver():
resolver.ednsflags = EDNSFlag.DO
return resolver


# dnspython 2.7 has this built in on Message
def extended_errors_from_answer(message: Message) -> List[dns.edns.EDEOption]:
ede_options = [option for option in message.options if option.otype == dns.edns.OptionType.EDE]
Expand Down

0 comments on commit 164f3b7

Please sign in to comment.