implement announcer as a consumer task on gather
This commit is contained in:
parent
7ded8a1333
commit
f05943ff79
1 changed files with 19 additions and 19 deletions
|
@ -28,14 +28,16 @@ class BlobAnnouncer:
|
||||||
self.announce_task: asyncio.Task = None
|
self.announce_task: asyncio.Task = None
|
||||||
self.announce_queue: typing.List[str] = []
|
self.announce_queue: typing.List[str] = []
|
||||||
self._done = asyncio.Event()
|
self._done = asyncio.Event()
|
||||||
|
self.announced = set()
|
||||||
|
|
||||||
async def _submit_announcement(self, blob_hash):
|
async def _run_consumer(self):
|
||||||
|
while self.announce_queue:
|
||||||
try:
|
try:
|
||||||
|
blob_hash = self.announce_queue.pop()
|
||||||
peers = len(await self.node.announce_blob(blob_hash))
|
peers = len(await self.node.announce_blob(blob_hash))
|
||||||
self.announcements_sent_metric.labels(peers=peers, error=False).inc()
|
self.announcements_sent_metric.labels(peers=peers, error=False).inc()
|
||||||
if peers > 4:
|
if peers > 4:
|
||||||
return blob_hash
|
self.announced.add(blob_hash)
|
||||||
else:
|
else:
|
||||||
log.debug("failed to announce %s, could only find %d peers, retrying soon.", blob_hash[:8], peers)
|
log.debug("failed to announce %s, could only find %d peers, retrying soon.", blob_hash[:8], peers)
|
||||||
except Exception as err:
|
except Exception as err:
|
||||||
|
@ -57,14 +59,12 @@ class BlobAnnouncer:
|
||||||
log.debug("announcer task wake up, %d blobs to announce", len(self.announce_queue))
|
log.debug("announcer task wake up, %d blobs to announce", len(self.announce_queue))
|
||||||
while len(self.announce_queue) > 0:
|
while len(self.announce_queue) > 0:
|
||||||
log.info("%i blobs to announce", len(self.announce_queue))
|
log.info("%i blobs to announce", len(self.announce_queue))
|
||||||
announced = await asyncio.gather(*[
|
await asyncio.gather(*[self._run_consumer() for _ in range(batch_size)], loop=self.loop)
|
||||||
self._submit_announcement(
|
announced = list(filter(None, self.announced))
|
||||||
self.announce_queue.pop()) for _ in range(batch_size) if self.announce_queue
|
|
||||||
], loop=self.loop)
|
|
||||||
announced = list(filter(None, announced))
|
|
||||||
if announced:
|
if announced:
|
||||||
await self.storage.update_last_announced_blobs(announced)
|
await self.storage.update_last_announced_blobs(announced)
|
||||||
log.info("announced %i blobs", len(announced))
|
log.info("announced %i blobs", len(announced))
|
||||||
|
self.announced.clear()
|
||||||
self._done.set()
|
self._done.set()
|
||||||
self._done.clear()
|
self._done.clear()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue