Add dns-query-timeout option to config.yaml
Introduced a new configuration option 'dns-query-timeout' to set the DNS query timeout (in seconds) for the charm. Closes-Bug: #2112374 Change-Id: I552457b588183726314faaded21442f062a71ae2 Signed-off-by: Zhang Hua <joshua.zhang@canonical.com>
This commit is contained in:
@@ -469,9 +469,21 @@ def ns_query(address):
|
||||
return None
|
||||
|
||||
try:
|
||||
answers = dns.resolver.query(address, rtype)
|
||||
resolv = dns.resolver.Resolver()
|
||||
# The dnspython library sets a default DNS query lifetime of 5.0 seconds,
|
||||
# as defined in BaseResolver.reset() (see https://github.com/rthalley/
|
||||
# dnspython/blob/7ed1648b/dns/resolver.py#L709, commit 7ed1648b).
|
||||
resolv.lifetime = config('dns-query-timeout') or 5.0
|
||||
if hasattr(resolv, 'resolve'):
|
||||
answers = resolv.resolve(address, rtype)
|
||||
else:
|
||||
answers = resolv.query(address, rtype)
|
||||
except (dns.resolver.NXDOMAIN, dns.resolver.NoNameservers):
|
||||
return None
|
||||
except dns.exception.Timeout:
|
||||
log("DNS query timed out for address {} with rtype {} and timeout "
|
||||
"{}".format(address, rtype, resolv.lifetime), level=WARNING)
|
||||
return None
|
||||
|
||||
if answers:
|
||||
return str(answers[0])
|
||||
|
||||
@@ -428,3 +428,12 @@ options:
|
||||
this option sets True as the default value, which is consistent with the
|
||||
default value 'WSGISocketRotation On' in Apache. This option should be
|
||||
used with caution. Please read the Apache doc page for more information.
|
||||
dns-query-timeout:
|
||||
type: float
|
||||
default: 5.0
|
||||
description: |
|
||||
Sets the timeout (in seconds) for DNS queries in the charm. If not
|
||||
configured, the default value is 5.0 seconds. Adjust this value to
|
||||
balance query speed and reliability, especially in environments with
|
||||
slow or unstable DNS servers. Use with caution, as very low values
|
||||
may lead to frequent query failures.
|
||||
|
||||
Reference in New Issue
Block a user