use reactor time in Delay

This commit is contained in:
Jack Robison 2018-03-05 13:28:59 -05:00
parent 4eab77fa10
commit a96d827c0f
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
2 changed files with 6 additions and 7 deletions

View file

@ -1,18 +1,17 @@
import time
class Delay(object):
maxToSendDelay = 10 ** -3 # 0.05
minToSendDelay = 10 ** -5 # 0.01
def __init__(self, start=0):
def __init__(self, start=0, getTime=None):
self._next = start
if not getTime:
from time import time as getTime
self._getTime = getTime
# TODO: explain why this logic is like it is. And add tests that
# show that it actually does what it needs to do.
def __call__(self):
ts = time.time()
delay = 0
ts = self._getTime()
if ts >= self._next:
delay = self.minToSendDelay
self._next = ts + self.minToSendDelay

View file

@ -29,7 +29,7 @@ class KademliaProtocol(protocol.DatagramProtocol):
self._sentMessages = {}
self._partialMessages = {}
self._partialMessagesProgress = {}
self._delay = Delay()
self._delay = Delay(0, self._node.clock.seconds)
# keep track of outstanding writes so that they
# can be cancelled on shutdown
self._call_later_list = {}