[Git][NTPsec/ntpsec][master] Moved DNS caching to timed_canonicalize_dns() function
Ian Bruene
gitlab at mg.gitlab.com
Mon Jan 1 19:18:41 UTC 2018
Ian Bruene pushed to branch master at NTPsec / ntpsec
Commits:
9795f701 by Ian Bruene at 2018-01-01T13:17:35-06:00
Moved DNS caching to timed_canonicalize_dns() function
- - - - -
2 changed files:
- pylib/util.py
- tests/pylib/test_util.py
Changes:
=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -558,6 +558,9 @@ import subprocess
def timed_canonicalize_dns(inhost, family=socket.AF_UNSPEC, ttl=1.0):
+ resname = canonicalization_cache.get(inhost)
+ if resname is not None:
+ return resname
cmd = "import ntp.util; print(ntp.util.canonicalize_dns('%s', %s))"
cmd = cmd % (str(inhost), str(family))
p = subprocess.Popen(["python", "-c", cmd],
@@ -571,13 +574,12 @@ def timed_canonicalize_dns(inhost, family=socket.AF_UNSPEC, ttl=1.0):
p.terminate()
return inhost
time.sleep(.01) # don't waste cycles with spinning
- return p.communicate()[0].strip("\n")
+ result = p.communicate()[0].strip("\n")
+ canonicalization_cache.set(inhost, result)
+ return result
def canonicalize_dns(inhost, family=socket.AF_UNSPEC):
"Canonicalize a hostname or numeric IP address."
- resname = canonicalization_cache.get(inhost)
- if resname is not None:
- return resname
# Catch garbaged hostnames in corrupted Mode 6 responses
m = re.match("([:.[\]]|\w)*", inhost)
if not m:
@@ -597,7 +599,6 @@ def canonicalize_dns(inhost, family=socket.AF_UNSPEC):
# Fall back to the hostname.
canonicalized = canonname or hostname
result = canonicalized.lower() + portsuffix
- canonicalization_cache.set(inhost, result)
return result
=====================================
tests/pylib/test_util.py
=====================================
--- a/tests/pylib/test_util.py
+++ b/tests/pylib/test_util.py
@@ -510,17 +510,9 @@ class TestPylibUtilMethods(unittest.TestCase):
f = ntp.util.canonicalize_dns
fakesockmod = jigs.SocketModuleJig()
- mycache = ntp.util.Cache()
- mycache.set("foo", "bar")
try:
- cachetemp = ntp.util.canonicalization_cache
- ntp.util.canonicalization_cache = mycache
sockettemp = ntp.util.socket
ntp.util.socket = fakesockmod
- # Test cache hit
- print("DNS:", f)
- self.assertEqual(f("foo"), "bar")
- self.assertEqual(fakesockmod.gai_calls, [])
# Test addrinfo fail
fakesockmod.__init__()
fakesockmod.gai_error_count = 1
@@ -539,7 +531,6 @@ class TestPylibUtilMethods(unittest.TestCase):
self.assertEqual(f("bar:42"), "san.hastur.invalid:42")
# Test nameinfo fail, no canonname
fakesockmod.__init__()
- mycache.__init__()
fakesockmod.gni_error_count = 1
fakesockmod.gni_returns = [("www.Hastur.invalid", 42)]
fakesockmod.gai_returns = [(("family", "socktype", "proto",
@@ -547,13 +538,11 @@ class TestPylibUtilMethods(unittest.TestCase):
self.assertEqual(f("bar:42"), "bar:42")
# Test success
fakesockmod.__init__()
- mycache.__init__()
fakesockmod.gni_returns = [("www.Hastur.invalid", 42)]
fakesockmod.gai_returns = [(("family", "socktype", "proto",
None, "42.23.%$.(#"),)]
self.assertEqual(f("bar:42"), "www.hastur.invalid:42")
finally:
- ntp.util.canonicalization_cache = cachetemp
ntp.util.socket = sockettemp
def test_termsize(self):
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/9795f701fc82fb78698a962dddcbd025c933fcca
---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/9795f701fc82fb78698a962dddcbd025c933fcca
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20180101/237f8a84/attachment.html>
More information about the vc
mailing list