From 7b425eb2ac74d50d4369526e3658501ae0257a07 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Sat, 5 Mar 2022 04:36:26 -0300 Subject: [PATCH] add tracker announcer component --- lbry/extras/daemon/components.py | 39 ++++++++++++++++++++++++++++++++ lbry/file/source.py | 1 + lbry/stream/downloader.py | 1 + 3 files changed, 41 insertions(+) diff --git a/lbry/extras/daemon/components.py b/lbry/extras/daemon/components.py index 03bef1534..5049b6bad 100644 --- a/lbry/extras/daemon/components.py +++ b/lbry/extras/daemon/components.py @@ -3,6 +3,7 @@ import os import asyncio import logging import binascii +import time import typing import base58 @@ -48,6 +49,7 @@ BACKGROUND_DOWNLOADER_COMPONENT = "background_downloader" PEER_PROTOCOL_SERVER_COMPONENT = "peer_protocol_server" UPNP_COMPONENT = "upnp" EXCHANGE_RATE_MANAGER_COMPONENT = "exchange_rate_manager" +TRACKER_ANNOUNCER_COMPONENT = "tracker_announcer_component" LIBTORRENT_COMPONENT = "libtorrent_component" @@ -708,3 +710,40 @@ class ExchangeRateManagerComponent(Component): async def stop(self): self.exchange_rate_manager.stop() + + +class TrackerAnnouncerComponent(Component): + component_name = TRACKER_ANNOUNCER_COMPONENT + depends_on = [FILE_MANAGER_COMPONENT] + + def __init__(self, component_manager): + super().__init__(component_manager) + self.file_manager = None + self.announce_task = None + + @property + def component(self) -> ExchangeRateManager: + return self.exchange_rate_manager + + async def announce_forever(self): + while True: + to_sleep = 60 * 10 + for file in self.file_manager.get_filtered(): + if not file.downloader: + continue + next_announce = file.downloader.next_tracker_announce_time + if next_announce is None or next_announce <= time.time(): + await file.downloader.refresh_from_trackers(False) + else: + to_sleep = min(to_sleep, next_announce - time.time()) + await asyncio.sleep(to_sleep + 1) + + async def start(self): + self.file_manager = self.component_manager.get_component(FILE_MANAGER_COMPONENT) + self.announce_task = asyncio.create_task(self.announce_forever()) + + async def stop(self): + self.file_manager = None + if self.announce_task and not self.announce_task.done(): + self.announce_task.cancel() + self.announce_task = None diff --git a/lbry/file/source.py b/lbry/file/source.py index b661eb594..0cded2f6c 100644 --- a/lbry/file/source.py +++ b/lbry/file/source.py @@ -45,6 +45,7 @@ class ManagedDownloadSource: self.purchase_receipt = None self._added_on = added_on self.analytics_manager = analytics_manager + self.downloader = None self.saving = asyncio.Event(loop=self.loop) self.finished_writing = asyncio.Event(loop=self.loop) diff --git a/lbry/stream/downloader.py b/lbry/stream/downloader.py index 874715c6f..608b33849 100644 --- a/lbry/stream/downloader.py +++ b/lbry/stream/downloader.py @@ -76,6 +76,7 @@ class StreamDownloader: try: announcement = await get_peer_list( bytes.fromhex(self.sd_hash)[:20], node_id, port, server[0], server[1]) + log.info("Announced %s to %s", self.sd_hash[:8], server) self.next_tracker_announce_time = max(self.next_tracker_announce_time or 0, time.time() + announcement.interval) except asyncio.CancelledError: