From 6e5c7a1927f7284cc4a6fe95f0620003269d8252 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Wed, 9 Mar 2022 20:11:30 -0300 Subject: [PATCH] use cache_concurrent to avoid requesting the same connection_id multiple times --- lbry/torrent/tracker.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lbry/torrent/tracker.py b/lbry/torrent/tracker.py index 5e78ed045..9a6e98b6f 100644 --- a/lbry/torrent/tracker.py +++ b/lbry/torrent/tracker.py @@ -5,7 +5,7 @@ import logging import time from collections import namedtuple -from lbry.utils import resolve_host, async_timed_cache +from lbry.utils import resolve_host, async_timed_cache, cache_concurrent from lbry.wallet.stream import StreamController log = logging.getLogger(__name__) @@ -83,6 +83,7 @@ class UDPTrackerClientProtocol(asyncio.DatagramProtocol): return decode(ConnectResponse, await self.request(ConnectRequest(0x41727101980, 0, transaction_id), tracker_ip, tracker_port)) + @cache_concurrent @async_timed_cache(CONNECTION_EXPIRES_AFTER_SECONDS) async def ensure_connection_id(self, peer_id, tracker_ip, tracker_port): # peer_id is just to ensure cache coherency @@ -98,9 +99,7 @@ class UDPTrackerClientProtocol(asyncio.DatagramProtocol): return decode(AnnounceResponse, await self.request(req, tracker_ip, tracker_port)) async def scrape(self, infohashes, tracker_ip, tracker_port, connection_id=None): - if not connection_id: - reply = await self.connect(tracker_ip, tracker_port) - connection_id = reply.connection_id + connection_id = await self.ensure_connection_id(None, tracker_ip, tracker_port) transaction_id = random.getrandbits(32) reply = await self.request( ScrapeRequest(connection_id, 2, transaction_id, infohashes), tracker_ip, tracker_port)