mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Add events in Pubsub
To ensure `handle_peer_queue` and `handle_dead_peer_queue` are indeed run before the tests finish. Previously, we get errors when performing `iter_dag` after cancellation. This is because `handle_peer_queue` or `handle_dead_peer_queue` is not actually run before the Service is cancelled.
This commit is contained in:
@ -86,6 +86,9 @@ class Pubsub(IPubsub, Service):
|
||||
strict_signing: bool
|
||||
sign_key: PrivateKey
|
||||
|
||||
event_handle_peer_queue_started: trio.Event
|
||||
event_handle_dead_peer_queue_started: trio.Event
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
host: IHost,
|
||||
@ -159,6 +162,9 @@ class Pubsub(IPubsub, Service):
|
||||
|
||||
self.counter = int(time.time())
|
||||
|
||||
self.event_handle_peer_queue_started = trio.Event()
|
||||
self.event_handle_dead_peer_queue_started = trio.Event()
|
||||
|
||||
async def run(self) -> None:
|
||||
self.manager.run_daemon_task(self.handle_peer_queue)
|
||||
self.manager.run_daemon_task(self.handle_dead_peer_queue)
|
||||
@ -331,12 +337,14 @@ class Pubsub(IPubsub, Service):
|
||||
"""Continuously read from peer queue and each time a new peer is found,
|
||||
open a stream to the peer using a supported pubsub protocol pubsub
|
||||
protocols we support."""
|
||||
self.event_handle_peer_queue_started.set()
|
||||
async with self.peer_receive_channel:
|
||||
async for peer_id in self.peer_receive_channel:
|
||||
# Add Peer
|
||||
self.manager.run_task(self._handle_new_peer, peer_id)
|
||||
|
||||
async def handle_dead_peer_queue(self) -> None:
|
||||
self.event_handle_dead_peer_queue_started.set()
|
||||
"""Continuously read from dead peer channel and close the stream
|
||||
between that peer and remove peer info from pubsub and pubsub
|
||||
router."""
|
||||
|
||||
@ -245,6 +245,8 @@ class PubsubFactory(factory.Factory):
|
||||
strict_signing=strict_signing,
|
||||
)
|
||||
async with background_trio_service(pubsub):
|
||||
await pubsub.event_handle_peer_queue_started.wait()
|
||||
await pubsub.event_handle_dead_peer_queue_started.wait()
|
||||
yield pubsub
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user