From 437d2d96393b616c306a7886ce13d9220ca95207 Mon Sep 17 00:00:00 2001 From: Erik Olof Gunnar Andersson Date: Sun, 7 May 2023 18:47:39 -0700 Subject: Use monotonic time to protect from system time jumps Change-Id: I44a51bef79109600a3f93844ac51a1f5738508b9 --- designate/backend/impl_dynect.py | 4 ++-- designate/dnsutils.py | 4 ++-- designate/tests/__init__.py | 8 ++++---- designate/worker/processing.py | 4 ++-- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/designate/backend/impl_dynect.py b/designate/backend/impl_dynect.py index 19f586f5..7e30443c 100755 --- a/designate/backend/impl_dynect.py +++ b/designate/backend/impl_dynect.py @@ -202,11 +202,11 @@ class DynClient(object): self._http_log_req(method, url, kwargs) if self.timings: - start_time = time.time() + start_time = time.monotonic() resp = self.http.request(method, url, **kwargs) if self.timings: self.times.append(("%s %s" % (method, url), - start_time, time.time())) + start_time, time.monotonic())) self._http_log_resp(resp) if resp.status_code >= 400: diff --git a/designate/dnsutils.py b/designate/dnsutils.py index 81df8409..43c45dff 100644 --- a/designate/dnsutils.py +++ b/designate/dnsutils.py @@ -203,12 +203,12 @@ class ZoneLock(object): with self.lock: # If no one holds the lock for the zone, grant it if zone not in self.data: - self.data[zone] = time.time() + self.data[zone] = time.monotonic() return True # Otherwise, get the time that it was locked locktime = self.data[zone] - now = time.time() + now = time.monotonic() period = now - locktime diff --git a/designate/tests/__init__.py b/designate/tests/__init__.py index d65ef2bf..ac371ab7 100644 --- a/designate/tests/__init__.py +++ b/designate/tests/__init__.py @@ -820,7 +820,7 @@ class TestCase(base.BaseTestCase): Zone imports spawn a thread to parse the zone file and insert the data. This waits for this process before continuing """ - start_time = time.time() + start_time = time.monotonic() while True: # Retrieve it, and ensure it's the same zone_import = self.central_service.get_zone_import( @@ -835,7 +835,7 @@ class TestCase(base.BaseTestCase): if error_is_ok and zone_import.status != 'PENDING': break - if (time.time() - start_time) > max_wait: + if (time.monotonic() - start_time) > max_wait: break time.sleep(0.5) @@ -870,8 +870,8 @@ class TestCase(base.BaseTestCase): Poll every `interval` seconds. `condition` can be a callable. (Caution: some mocks behave both as values and callables.) """ - t_max = time.time() + timeout - while time.time() < t_max: + t_max = time.monotonic() + timeout + while time.monotonic() < t_max: if callable(condition): result = condition() else: diff --git a/designate/worker/processing.py b/designate/worker/processing.py index 59f8fcbe..cf280033 100644 --- a/designate/worker/processing.py +++ b/designate/worker/processing.py @@ -70,9 +70,9 @@ class Executor(object): if callable(tasks): tasks = [tasks] - start_time = time.time() + start_time = time.monotonic() results = [r for r in self._executor.map(self.do, tasks)] - elapsed_time = time.time() - start_time + elapsed_time = time.monotonic() - start_time LOG.debug( 'Finished Task(s): %(tasks)s in %(time)fs', -- cgit v1.2.1