From 2d0790714a7a95dd631bf09d9f7a6b878ad4fa46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 5 Jan 2016 11:08:35 +0200 Subject: [PATCH] Use time.monotonic for timekeeping where available --- stomp/backward.py | 6 ++++++ stomp/listener.py | 13 +++++++------ stomp/test/basic_test.py | 5 +++-- stomp/test/threading_test.py | 5 +++-- stomp/transport.py | 6 +++--- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/stomp/backward.py b/stomp/backward.py index dc72ab38..a88e0e0e 100755 --- a/stomp/backward.py +++ b/stomp/backward.py @@ -35,3 +35,9 @@ def gcd(a, b): while b: a, b = b, a % b return a + + +try: + from time import monotonic +except ImportError: # Python < 3.3/3.5 + from time import time as monotonic diff --git a/stomp/listener.py b/stomp/listener.py index 14b1baf5..63a277c1 100755 --- a/stomp/listener.py +++ b/stomp/listener.py @@ -6,6 +6,7 @@ import threading import time +from stomp.backward import monotonic from stomp.constants import * import stomp.exception as exception import stomp.utils as utils @@ -155,7 +156,7 @@ def __init__(self, heartbeats): self.connected = False self.running = False self.heartbeats = heartbeats - self.received_heartbeat = time.time() + self.received_heartbeat = monotonic() self.heartbeat_thread = None def on_connected(self, headers, body): @@ -167,7 +168,7 @@ def on_connected(self, headers, body): :param headers: headers in the connection message :param body: the message body """ - self.received_heartbeat = time.time() + self.received_heartbeat = monotonic() if 'heart-beat' in headers.keys(): self.heartbeats = utils.calculate_heartbeats(headers['heart-beat'].replace(' ', '').split(','), self.heartbeats) if self.heartbeats != (0, 0): @@ -199,13 +200,13 @@ def on_message(self, headers, body): :param body: the message content """ # reset the heartbeat for any received message - self.received_heartbeat = time.time() + self.received_heartbeat = monotonic() def on_heartbeat(self): """ Reset the last received time whenever a heartbeat message is received. """ - self.received_heartbeat = time.time() + self.received_heartbeat = monotonic() def on_send(self, frame): """ @@ -221,12 +222,12 @@ def __heartbeat_loop(self): """ Main loop for sending (and monitoring received) heartbeats. """ - send_time = time.time() + send_time = monotonic() while self.running: time.sleep(self.sleep_time) - now = time.time() + now = monotonic() if now - send_time > self.send_sleep: send_time = now diff --git a/stomp/test/basic_test.py b/stomp/test/basic_test.py index 705dfa17..d4a077e8 100755 --- a/stomp/test/basic_test.py +++ b/stomp/test/basic_test.py @@ -5,6 +5,7 @@ import stomp from stomp import exception +from stomp.backward import monotonic from stomp.listener import TestListener from stomp.test.testutils import * @@ -81,12 +82,12 @@ def test_timeout(self): conn.set_listener('', self.listener) try: - ms = time.time() + ms = monotonic() conn.start() self.fail("shouldn't happen") except exception.ConnectFailedException: pass # success! - ms = time.time() - ms + ms = monotonic() - ms self.assertTrue(ms > 5.0, 'connection timeout should have been at least 5 seconds') def test_childinterrupt(self): diff --git a/stomp/test/threading_test.py b/stomp/test/threading_test.py index 97d643bc..9ec83585 100755 --- a/stomp/test/threading_test.py +++ b/stomp/test/threading_test.py @@ -8,6 +8,7 @@ import unittest import stomp +from stomp.backward import monotonic from stomp.test.testutils import * @@ -112,8 +113,8 @@ def send(i=i, Q=Q, Cmd=Cmd, Error=Error): def test_threads_dont_wedge(self): for t in self.threads: t.start() - start = time.time() - while time.time() - start < self.runfor: + start = monotonic() + while monotonic() - start < self.runfor: try: self.Q.put(1, False) time.sleep(1.0) diff --git a/stomp/transport.py b/stomp/transport.py index 73a1e2d0..82e1a22b 100644 --- a/stomp/transport.py +++ b/stomp/transport.py @@ -34,7 +34,7 @@ class SSLError(object): except ImportError: LINUX_KEEPALIVE_AVAIL = False -from stomp.backward import decode, encode, get_errno, pack +from stomp.backward import decode, encode, get_errno, monotonic, pack from stomp.backwardsock import get_socket import stomp.exception as exception import stomp.listener @@ -711,9 +711,9 @@ def attempt_connection(self): ((self.__reconnect_sleep_initial / (1.0 + self.__reconnect_sleep_increase)) * math.pow(1.0 + self.__reconnect_sleep_increase, sleep_exp))) * (1.0 + random.random() * self.__reconnect_sleep_jitter)) - sleep_end = time.time() + sleep_duration + sleep_end = monotonic() + sleep_duration log.debug("Sleeping for %.1f seconds before attempting reconnect", sleep_duration) - while self.running and time.time() < sleep_end: + while self.running and monotonic() < sleep_end: time.sleep(0.2) if sleep_duration < self.__reconnect_sleep_max: