[Git][NTPsec/ntpsec][master] In pylib/util.py, refactor DNS lookup caching...

Eric S. Raymond gitlab at mg.gitlab.com
Thu Aug 24 11:25:16 UTC 2017


Eric S. Raymond pushed to branch master at NTPsec / ntpsec


Commits:
d8d6bda8 by Eric S. Raymond at 2017-08-24T07:24:59-04:00
In pylib/util.py, refactor DNS lookup caching...

...and apply it to forward confirmation as well as lookup.

>From an idea by Matt Nordhoff.

- - - - -


1 changed file:

- pylib/util.py


Changes:

=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -479,16 +479,27 @@ def monoclock():
     except AttributeError:
         return time.time()
 
+class Cache:
+    "Simple time-based cache"
+    ttl = 300
+    def __init__(self):
+        self._cache = {}
+    def get(self, key):
+        if key in self._cache:
+            value, settime = self._cache[key]
+            if settime >= monoclock() - self.ttl:
+                return value
+    def set(self, key, value):
+        self._cache[key] = (value, monoclock())
+
 # A hack to avoid repeatedly hammering on DNS when ntpmon runs.
-canonicalization_cache = {}
+canonicalization_cache = Cache()
 
 def canonicalize_dns(inhost, family=socket.AF_UNSPEC):
     "Canonicalize a hostname or numeric IP address."
-    TTL = 300
-    if inhost in canonicalization_cache:
-        (resname, restime) = canonicalization_cache[inhost]
-        if restime >= monoclock() - TTL:
-            return resname
+    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:
@@ -508,12 +519,11 @@ def canonicalize_dns(inhost, family=socket.AF_UNSPEC):
         # Fall back to the hostname.
         canonicalized = canonname or hostname
         result = canonicalized.lower() + portsuffix
-    canonicalization_cache[inhost] = (result, monoclock())
+    canonicalization_cache.set(inhost, result)
     return result
 
 TermSize = collections.namedtuple("TermSize", ["width", "height"])
 
-
 def termsize():
     "Return the current terminal size."
     # Alternatives at http://stackoverflow.com/questions/566746
@@ -1106,15 +1116,19 @@ class MRUSummary:
             else:
                 dns = canonicalize_dns(ip)
                 # Forward-confirm the returned DNS
-                confirmed = False
-                try:
-                    ai = socket.getaddrinfo(dns, None)
-                    for (family, socktype, proto, canonname, sockaddr) in ai:
-                        if sockaddr and sockaddr[0] == ip:
-                            confirmed = True
-                            break
-                except socket.gaierror as e:
-                    pass
+                confirmed = confirmation_cache.get(dns)
+                if confirmed is None:
+                    confirmed = False
+                    try:
+                        ai = socket.getaddrinfo(dns, None)
+                        for (family, socktype, proto, canonname, sockaddr) in \
+                            ai:
+                            if sockaddr and sockaddr[0] == ip:
+                                confirmed = True
+                                break
+                    except socket.gaierror as e:
+                        pass
+                    confirmation_cache.set(dns, confirmed)
                 if not confirmed:
                     dns = "%s (%s)" % (ip, dns)
             if not self.wideremote:



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/d8d6bda866f7ad1326b033654c7246de89537bb5

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/d8d6bda866f7ad1326b033654c7246de89537bb5
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/20170824/b1b98219/attachment.html>


More information about the vc mailing list