remove semaphore from ping queue

This commit is contained in:
Victor Shyba 2018-09-28 10:51:07 -03:00
parent 2f95c3a9d1
commit 4890fdfb50
2 changed files with 14 additions and 27 deletions

View file

@ -640,19 +640,14 @@ class Node(MockKademliaHelper):
replication/republishing as necessary """
yield self._refreshRoutingTable()
self._dataStore.removeExpiredPeers()
yield self._refreshStoringPeers()
self._refreshStoringPeers()
defer.returnValue(None)
def _refreshContacts(self):
return defer.DeferredList(
[self._protocol._ping_queue.enqueue_maybe_ping(contact, delay=0) for contact in self.contacts]
)
self._protocol._ping_queue.enqueue_maybe_ping(*self.contacts, delay=0)
def _refreshStoringPeers(self):
storing_contacts = self._dataStore.getStoringContacts()
return defer.DeferredList(
[self._protocol._ping_queue.enqueue_maybe_ping(contact, delay=0) for contact in storing_contacts]
)
self._protocol._ping_queue.enqueue_maybe_ping(*self._dataStore.getStoringContacts(), delay=0)
@defer.inlineCallbacks
def _refreshRoutingTable(self):

View file

@ -25,29 +25,25 @@ class PingQueue:
self._get_time = self._node.clock.seconds
self._queue = deque()
self._enqueued_contacts = {}
self._semaphore = defer.DeferredSemaphore(1)
self._ping_semaphore = defer.DeferredSemaphore(constants.alpha)
self._process_lc = node.get_looping_call(self._semaphore.run, self._process)
self._process_lc = node.get_looping_call(self._process)
def _add_contact(self, contact, delay=None):
if (contact.address, contact.port) in [(c.address, c.port) for c in self._enqueued_contacts]:
return defer.succeed(None)
delay = delay or constants.checkRefreshInterval
self._enqueued_contacts[contact] = self._get_time() + delay
self._queue.append(contact)
return defer.succeed(None)
def enqueue_maybe_ping(self, *contacts, **kwargs):
schedule = self._get_time() + (kwargs.get('delay', constants.checkRefreshInterval))
for contact in contacts:
if contact not in self._enqueued_contacts:
self._enqueued_contacts[contact] = schedule
self._queue.append(contact)
@defer.inlineCallbacks
def _process(self):
if not len(self._queue):
defer.returnValue(None)
return
contact = self._queue.popleft()
now = self._get_time()
# if the oldest contact in the queue isn't old enough to be pinged, add it back to the queue and return
if now < self._enqueued_contacts[contact]:
self._queue.appendleft(contact)
defer.returnValue(None)
return
pinged = []
checked = []
@ -70,15 +66,14 @@ class PingQueue:
except Exception as err:
log.warning("unexpected error: %s", err)
yield defer.DeferredList([_ping(contact) for contact in pinged])
d = defer.DeferredList([_ping(contact) for contact in pinged])
for contact in checked:
if contact in self._enqueued_contacts and contact in pinged:
del self._enqueued_contacts[contact]
elif contact not in self._queue:
self._queue.appendleft(contact)
defer.returnValue(None)
return d
def start(self):
return self._node.safe_start_looping_call(self._process_lc, 60)
@ -86,9 +81,6 @@ class PingQueue:
def stop(self):
return self._node.safe_stop_looping_call(self._process_lc)
def enqueue_maybe_ping(self, contact, delay=None):
return self._semaphore.run(self._add_contact, contact, delay)
class KademliaProtocol(protocol.DatagramProtocol):
""" Implements all low-level network-related functions of a Kademlia node """