Skip to content

Commit

Permalink
Raise an error in the case of unsuccessful DHT check
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Feb 1, 2023
1 parent adb9d33 commit 41b4634
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ def update_ip_filter(self, lt_session, ip_addresses):
ip_filter.add_rule(ip, ip, 0)
lt_session.set_ip_filter(ip_filter)

async def get_metainfo(self, infohash, timeout=30, hops=None, url=None) -> Optional[Dict]:
async def get_metainfo(self, infohash: bytes, timeout: float = 30, hops: Optional[int] = None,
url: Optional[str] = None, raise_errors: bool = False) -> Optional[Dict]:
"""
Lookup metainfo for a given infohash. The mechanism works by joining the swarm for the infohash connecting
to a few peers, and downloading the metadata for the torrent.
Expand Down Expand Up @@ -490,17 +491,23 @@ async def get_metainfo(self, infohash, timeout=30, hops=None, url=None) -> Optio
dcfg.set_dest_dir(self.metadata_tmpdir)
try:
download = self.start_download(tdef=tdef, config=dcfg, hidden=True, checkpoint_disabled=True)
except TypeError:
except TypeError as e:
self._logger.warning(e)
if raise_errors:
raise e
return None
self.metainfo_requests[infohash] = [download, 1]

try:
metainfo = download.tdef.get_metainfo() or await wait_for(shield(download.future_metainfo), timeout)
self._logger.info('Successfully retrieved metainfo for %s', infohash_hex)
self.metainfo_cache[infohash] = {'time': timemod.time(), 'meta_info': metainfo}
except (CancelledError, asyncio.TimeoutError):
metainfo = None
except (CancelledError, asyncio.TimeoutError) as e:
self._logger.warning(f'{type(e).__name__}: {e} (timeout={timeout})')
self._logger.info('Failed to retrieve metainfo for %s', infohash_hex)
if raise_errors:
raise e
metainfo = None

if infohash in self.metainfo_requests:
self.metainfo_requests[infohash][1] -= 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from tribler.core.utilities.utilities import has_bep33_support, is_valid_url

TRACKER_SELECTION_INTERVAL = 1 # The interval for querying a random tracker
TORRENT_SELECTION_INTERVAL = 120 # The interval for checking the health of a random torrent
TORRENT_SELECTION_INTERVAL = 10 # The interval for checking the health of a random torrent
USER_CHANNEL_TORRENT_SELECTION_INTERVAL = 10 * 60 # The interval for checking the health of torrents in user's channel.
MIN_TORRENT_CHECK_INTERVAL = 900 # How much time we should wait before checking a torrent again
TORRENT_CHECK_RETRY_INTERVAL = 30 # Interval when the torrent was successfully checked for the last time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,15 +438,15 @@ class FakeDHTSession(TrackerSession):
Fake TrackerSession that manages DHT requests
"""

def __init__(self, download_manager: DownloadManager, timeout: int):
def __init__(self, download_manager: DownloadManager, timeout: float):
super().__init__(DHT, DHT, DHT, DHT, timeout)

self.download_manager = download_manager

async def connect_to_tracker(self) -> TrackerResponse:
responses = []
for infohash in self.infohash_list:
metainfo = await self.download_manager.get_metainfo(infohash, timeout=self.timeout)
metainfo = await self.download_manager.get_metainfo(infohash, timeout=self.timeout, raise_errors=True)
health = InfohashHealth(infohash=infohash, seeders=metainfo[b'seeders'], leechers=metainfo[b'leechers'])
responses.append(health)

Expand Down

0 comments on commit 41b4634

Please sign in to comment.