[Git][NTPsec/ntpsec][master] In pylib/util.py, use monoclock() for DNS timeouts.

Eric S. Raymond gitlab at mg.gitlab.com
Wed Aug 16 09:02:35 UTC 2017


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


Commits:
a264ef7f by Eric S. Raymond at 2017-08-16T05:01:03-04:00
In pylib/util.py, use monoclock() for DNS timeouts.

- - - - -


2 changed files:

- ntpclients/ntpwait
- pylib/util.py


Changes:

=====================================
ntpclients/ntpwait
=====================================
--- a/ntpclients/ntpwait
+++ b/ntpclients/ntpwait
@@ -27,6 +27,7 @@ import time
 import socket
 import ntp.magic
 import ntp.packet
+import ntp.util
 
 # General notes on Python 2/3 compatibility:
 #
@@ -129,14 +130,6 @@ class Unbuffered(object):
         return getattr(self.stream, attr)
 
 
-def monoclock():
-    "Try to get a monotonic clock value unaffected by NTP stepping."
-    try:
-        # Available in Python 3.3 and up.
-        return time.monotonic()
-    except AttributeError:
-        return time.time()
-
 if __name__ == "__main__":
     try:
         (options, arguments) = getopt.getopt(sys.argv[1:], "hn:s:v", [
@@ -164,7 +157,7 @@ if __name__ == "__main__":
     # Autoflush stdout
     sys.stdout = Unbuffered(sys.stdout)
 
-    basetime = monoclock()
+    basetime = ntp.util.monoclock()
     if verbose:
         sys.stdout.write("Waiting for ntpd to synchronize...  ")
 
@@ -215,7 +208,7 @@ if __name__ == "__main__":
             # We could check "sync" here to make sure we like the source...
             if verbose:
                 sys.stdout.write("\bOK! (%.1f seconds)\n" %
-                                 (monoclock() - basetime))
+                                 (ntp.util.monoclock() - basetime))
             sys.exit(0)
 
         sys.stdout.write("\bUnexpected 'leap' status <%s>\n" % leap)


=====================================
pylib/util.py
=====================================
--- a/pylib/util.py
+++ b/pylib/util.py
@@ -467,16 +467,23 @@ def f8dot3(f):
     return fmt % f
 
 
+def monoclock():
+    "Try to get a monotonic clock value unaffected by NTP stepping."
+    try:
+        # Available in Python 3.3 and up.
+        return time.monotonic()
+    except AttributeError:
+        return time.time()
+
 # A hack to avoid repeatedly hammering on DNS when ntpmon runs.
 canonicalization_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 >= time.time() - TTL:
+        if restime >= monoclock() - TTL:
             return resname
     # Catch garbaged hostnames in corrupted Mode 6 responses
     m = re.match("([:.[\]]|\w)*", inhost)
@@ -497,7 +504,7 @@ 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, time.time())
+    canonicalization_cache[inhost] = (result, monoclock())
     return result
 
 TermSize = collections.namedtuple("TermSize", ["width", "height"])



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

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/a264ef7f8545fd6c0f0b0a4d8661a940e662365a
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/20170816/e3ac2170/attachment.html>


More information about the vc mailing list