lbry-sdk/lbrynet/dht/delay.py

22 lines
716 B
Python
Raw Normal View History

2017-10-10 19:08:22 +02:00
class Delay(object):
maxToSendDelay = 10 ** -3 # 0.05
minToSendDelay = 10 ** -5 # 0.01
2018-03-05 19:28:59 +01:00
def __init__(self, start=0, getTime=None):
2017-10-10 19:08:22 +02:00
self._next = start
2018-03-05 19:28:59 +01:00
if not getTime:
from time import time as getTime
self._getTime = getTime
2017-10-10 19:08:22 +02:00
# 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):
2018-03-05 19:28:59 +01:00
ts = self._getTime()
2017-10-10 19:08:22 +02:00
if ts >= self._next:
delay = self.minToSendDelay
self._next = ts + self.minToSendDelay
else:
delay = (self._next - ts) + self.maxToSendDelay
self._next += self.maxToSendDelay
return delay