From aaa7702d9d19e431071a53e1177eddf9d6a964f4 Mon Sep 17 00:00:00 2001 From: "github@kiekerjan.isdronken.nl" Date: Mon, 18 Apr 2022 21:40:20 +0200 Subject: [PATCH] make dns resolver retrying explicit --- management/dns_update.py | 36 +++++++++++++++++------------------- management/status_checks.py | 29 ++++++++++++----------------- 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/management/dns_update.py b/management/dns_update.py index ad55868b..d934f50b 100755 --- a/management/dns_update.py +++ b/management/dns_update.py @@ -1087,7 +1087,7 @@ def get_secondary_dns(custom_dns, mode=None): response = resolver.resolve(hostname+'.', "A", raise_on_no_answer=False) values.extend(map(str, response)) except dns.exception.DNSException: - logging.debug("Secondary dns Alookup exception %s", hostname) + logging.debug("Secondary dns A lookup exception %s", hostname) try: response = resolver.resolve(hostname+'.', "AAAA", raise_on_no_answer=False) @@ -1110,35 +1110,33 @@ def set_secondary_dns(hostnames, env): if len(hostnames) > 0: # Validate that all hostnames are valid and that all zone-xfer IP addresses are valid. resolver = dns.resolver.get_default_resolver() - resolver.timeout = 3 - resolver.lifetime = 3 + resolver.timeout = 5 + resolver.lifetime = 5 for item in hostnames: if not item.startswith("xfr:"): # Resolve hostname. - try: - response = resolver.resolve(item, "A") - except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): - logging.debug('Error on resolving ipv4 address, trying ipv6') - try: - response = resolver.resolve(item, "AAAA") - except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): - raise ValueError("Could not resolve the IP address of %s." % item) - except (dns.resolver.Timeout): - resolver.timeout = 5 - resolver.lifetime = 5 - logging.debug('Timeout on resolving ipv4 address re-trying') + tries = 2 + + while tries > 0: + tries = tries - 1 try: response = resolver.resolve(item, "A") + tries = 0 except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): - logging.debug('Error on resolving ipv4 address, trying ipv6 (2)') + logging.debug('Error on resolving ipv4 address, trying ipv6') try: response = resolver.resolve(item, "AAAA") + tries = 0 except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): raise ValueError("Could not resolve the IP address of %s." % item) + except (dns.resolver.Timeout): + logging.debug('Timeout on resolving ipv6 address') + if tries < 1: + raise ValueError("Could not resolve the IP address of %s due to timeout." % item) except (dns.resolver.Timeout): - raise ValueError("Could not resolve the IP address of %s due to timeout." % item) - resolver.timeout = 3 - resolver.lifetime = 3 + logging.debug('Timeout on resolving ipv4 address') + if tries < 1: + raise ValueError("Could not resolve the IP address of %s due to timeout." % item) else: # Validate IP address. try: diff --git a/management/status_checks.py b/management/status_checks.py index bd03890e..94b2ca4a 100755 --- a/management/status_checks.py +++ b/management/status_checks.py @@ -801,31 +801,26 @@ def query_dns(qname, rtype, nxdomain='[Not Set]', at=None, as_list=False): resolver.nameservers = [at] # Set a timeout so that a non-responsive server doesn't hold us back. - resolver.timeout = 3 - resolver.lifetime = 3 + resolver.timeout = 5 + resolver.lifetime = 5 + tries = 2 # Do the query. - try: - response = resolver.resolve(qname, rtype, search=True) - except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): - # Host did not have an answer for this query; not sure what the - # difference is between the two exceptions. - logging.debug("No result for dns lookup %s, %s", qname, rtype) - return nxdomain - except dns.exception.Timeout: - logging.debug("Timeout on dns lookup %s, %s. Retrying", qname, rtype) - resolver.timeout = 5 - resolver.lifetime = 5 + while tries > 0: + tries = tries - 1 try: response = resolver.resolve(qname, rtype, search=True) + tries = 0 except (dns.resolver.NoNameservers, dns.resolver.NXDOMAIN, dns.resolver.NoAnswer): # Host did not have an answer for this query; not sure what the # difference is between the two exceptions. - logging.debug("No result for dns lookup %s, %s (2)", qname, rtype) - return nxdomain + logging.debug("No result for dns lookup %s, %s (%d)", qname, rtype, tries) + if tries < 1: + return nxdomain except dns.exception.Timeout: - logging.debug("Timeout on dns lookup %s, %s.", qname, rtype) - return "[timeout]" + logging.debug("Timeout on dns lookup %s, %s (%d)", qname, rtype, tries) + if tries < 1: + return "[timeout]" # Normalize IP addresses. IP address --- especially IPv6 addresses --- can # be expressed in equivalent string forms. Canonicalize the form before