at most 10 announces concurrently

This commit is contained in:
Victor Shyba 2022-03-08 01:02:43 -03:00
parent 2d9c5742c7
commit 30acde0afc

View file

@ -129,6 +129,7 @@ class TrackerClient:
self.announce_port = announce_port self.announce_port = announce_port
self.servers = servers self.servers = servers
self.results = {} # we can't probe the server before the interval, so we keep the result here until it expires self.results = {} # we can't probe the server before the interval, so we keep the result here until it expires
self.semaphore = asyncio.Semaphore(10)
async def start(self): async def start(self):
self.transport, _ = await asyncio.get_running_loop().create_datagram_endpoint( self.transport, _ = await asyncio.get_running_loop().create_datagram_endpoint(
@ -162,8 +163,9 @@ class TrackerClient:
return result return result
try: try:
tracker_ip = await resolve_host(tracker_host, tracker_port, 'udp') tracker_ip = await resolve_host(tracker_host, tracker_port, 'udp')
result = await self.client.announce( async with self.semaphore:
info_hash, self.node_id, self.announce_port, tracker_ip, tracker_port, stopped) result = await self.client.announce(
info_hash, self.node_id, self.announce_port, tracker_ip, tracker_port, stopped)
except asyncio.TimeoutError: except asyncio.TimeoutError:
log.info("Tracker timed out: %s:%d", tracker_host, tracker_port) log.info("Tracker timed out: %s:%d", tracker_host, tracker_port)
return None return None