Skip to content
This repository has been archived by the owner on Jan 21, 2022. It is now read-only.

Commit

Permalink
Move neighbour task work to tick task.
Browse files Browse the repository at this point in the history
  • Loading branch information
richardkiss committed May 15, 2017
1 parent 71f55f0 commit 35c6176
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions magneticod/magneticod/dht.py
Expand Up @@ -48,8 +48,8 @@ def __init__(self, address: typing.Tuple[str, int], complete_info_hashes, max_me
self._complete_info_hashes = complete_info_hashes
self.__max_metadata_size = max_metadata_size
self._metadata_q = asyncio.Queue()
self._tasks = []
self._is_paused = False
self._tick_task = None

logging.info("SybilNode %s on %s initialized!", self.__true_id.hex().upper(), address)

Expand All @@ -58,8 +58,7 @@ async def launch(self, loop):
await loop.create_datagram_endpoint(lambda: self, local_addr=self.__address)

def connection_made(self, transport):
self._tasks.append(self._loop.create_task(self.on_tick()))
self._tasks.append(self._loop.create_task(self.increase_neighbour_task()))
self._tick_task = self._loop.create_task(self.on_tick())
self._transport = transport

def connection_lost(self, exc):
Expand Down Expand Up @@ -93,6 +92,8 @@ async def on_tick(self) -> None:
self.__bootstrap()
self.__make_neighbours()
self._routing_table.clear()
if not self._is_paused:
self.__n_max_neighbours = self.__n_max_neighbours * 101 // 100

def datagram_received(self, data, addr) -> None:
# Ignore nodes that uses port 0 (assholes).
Expand All @@ -114,14 +115,10 @@ def datagram_received(self, data, addr) -> None:
elif message.get(b"q") == b"announce_peer":
self.__on_ANNOUNCE_PEER_query(message, addr)

async def increase_neighbour_task(self):
while True:
await asyncio.sleep(10)
self.__n_max_neighbours = self.__n_max_neighbours * 101 // 100

async def shutdown(self) -> None:
futures = [peer for peer in itertools.chain.from_iterable(self.__peers.values())]
futures.extend(self._tasks)
if self._tick_task:
futures.append(self._tick_task)
for future in futures:
future.cancel()
await asyncio.wait(futures)
Expand Down

0 comments on commit 35c6176

Please sign in to comment.