Skip to content

Commit

Permalink
Merge pull request #74 from scop/monotonic
Browse files Browse the repository at this point in the history
Use time.monotonic for timekeeping where available
  • Loading branch information
jasonrbriggs committed Jan 5, 2016
2 parents e58b163 + 2d07907 commit 457f83a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
6 changes: 6 additions & 0 deletions stomp/backward.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 7 additions & 6 deletions stomp/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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
Expand Down
5 changes: 3 additions & 2 deletions stomp/test/basic_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *

Expand Down Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions stomp/test/threading_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import unittest

import stomp
from stomp.backward import monotonic
from stomp.test.testutils import *


Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions stomp/transport.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 457f83a

Please sign in to comment.