From 629812337b10d4193299a3b9cf183edaaac891b7 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Fri, 6 May 2022 04:01:01 -0300 Subject: [PATCH] changes from review --- lbry/extras/daemon/components.py | 10 +++++----- lbry/torrent/tracker.py | 26 +++++++++++++------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/lbry/extras/daemon/components.py b/lbry/extras/daemon/components.py index 88abe11a4..e061c4363 100644 --- a/lbry/extras/daemon/components.py +++ b/lbry/extras/daemon/components.py @@ -733,14 +733,14 @@ class TrackerAnnouncerComponent(Component): async def announce_forever(self): while True: - to_sleep = 60.0 - to_announce = [] + sleep_seconds = 60.0 + announce_sd_hashes = [] for file in self.file_manager.get_filtered(): if not file.downloader: continue - to_announce.append(bytes.fromhex(file.sd_hash)) - await self.tracker_client.announce_many(*to_announce) - await asyncio.sleep(to_sleep) + announce_sd_hashes.append(bytes.fromhex(file.sd_hash)) + await self.tracker_client.announce_many(*announce_sd_hashes) + await asyncio.sleep(sleep_seconds) async def start(self): node = self.component_manager.get_component(DHT_COMPONENT) \ diff --git a/lbry/torrent/tracker.py b/lbry/torrent/tracker.py index 14b782f62..16446bfdd 100644 --- a/lbry/torrent/tracker.py +++ b/lbry/torrent/tracker.py @@ -31,7 +31,7 @@ ScrapeRequest = namedtuple("ScrapeRequest", ["connection_id", "action", "transac ScrapeResponse = namedtuple("ScrapeResponse", ["action", "transaction_id", "items"]) ScrapeResponseItem = namedtuple("ScrapeResponseItem", ["seeders", "completed", "leechers"]) ErrorResponse = namedtuple("ErrorResponse", ["action", "transaction_id", "message"]) -STRUCTS = { +structs = { ConnectRequest: struct.Struct(">QII"), ConnectResponse: struct.Struct(">IIQ"), AnnounceRequest: struct.Struct(">QII20s20sQQQIIIiH"), @@ -45,26 +45,26 @@ STRUCTS = { def decode(cls, data, offset=0): - decoder = STRUCTS[cls] - if cls == AnnounceResponse: + decoder = structs[cls] + if cls is AnnounceResponse: return AnnounceResponse(*decoder.unpack_from(data, offset), peers=[decode(CompactIPv4Peer, data, index) for index in range(20, len(data), 6)]) - elif cls == ScrapeResponse: + elif cls is ScrapeResponse: return ScrapeResponse(*decoder.unpack_from(data, offset), items=[decode(ScrapeResponseItem, data, index) for index in range(8, len(data), 12)]) - elif cls == ErrorResponse: + elif cls is ErrorResponse: return ErrorResponse(*decoder.unpack_from(data, offset), data[decoder.size:]) return cls(*decoder.unpack_from(data, offset)) def encode(obj): if isinstance(obj, ScrapeRequest): - return STRUCTS[ScrapeRequest].pack(*obj[:-1]) + b''.join(obj.infohashes) + return structs[ScrapeRequest].pack(*obj[:-1]) + b''.join(obj.infohashes) elif isinstance(obj, ErrorResponse): - return STRUCTS[ErrorResponse].pack(*obj[:-1]) + obj.message + return structs[ErrorResponse].pack(*obj[:-1]) + obj.message elif isinstance(obj, AnnounceResponse): - return STRUCTS[AnnounceResponse].pack(*obj[:-1]) + b''.join([encode(peer) for peer in obj.peers]) - return STRUCTS[type(obj)].pack(*obj) + return structs[AnnounceResponse].pack(*obj[:-1]) + b''.join([encode(peer) for peer in obj.peers]) + return structs[type(obj)].pack(*obj) def make_peer_id(random_part: Optional[str] = None) -> bytes: @@ -136,7 +136,7 @@ class UDPTrackerClientProtocol(asyncio.DatagramProtocol): class TrackerClient: - EVENT_CONTROLLER = StreamController() + event_controller = StreamController() def __init__(self, node_id, announce_port, get_servers, timeout=10.0): self.client = UDPTrackerClientProtocol(timeout=timeout) @@ -150,7 +150,7 @@ class TrackerClient: async def start(self): self.transport, _ = await asyncio.get_running_loop().create_datagram_endpoint( lambda: self.client, local_addr=("0.0.0.0", 0)) - self.EVENT_CONTROLLER.stream.listen( + self.event_controller.stream.listen( lambda request: self.on_hash(request[1], request[2]) if request[0] == 'search' else None) def stop(self): @@ -160,7 +160,7 @@ class TrackerClient: self.transport.close() self.client = None self.transport = None - self.EVENT_CONTROLLER.close() + self.event_controller.close() def on_hash(self, info_hash, on_announcement=None): if info_hash not in self.tasks: @@ -230,7 +230,7 @@ def enqueue_tracker_search(info_hash: bytes, peer_q: asyncio.Queue): peers = await announcement_to_kademlia_peers(announcement) log.info("Found %d peers from tracker for %s", len(peers), info_hash.hex()[:8]) peer_q.put_nowait(peers) - TrackerClient.EVENT_CONTROLLER.add(('search', info_hash, on_announcement)) + TrackerClient.event_controller.add(('search', info_hash, on_announcement)) def announcement_to_kademlia_peers(*announcements: AnnounceResponse):