diff --git a/libp2p/pubsub/pubsub.py b/libp2p/pubsub/pubsub.py index 94c33b4..0314074 100644 --- a/libp2p/pubsub/pubsub.py +++ b/libp2p/pubsub/pubsub.py @@ -95,15 +95,15 @@ class Pubsub(Service, IPubsub): # Attach this new Pubsub object to the router self.router.attach(self) - peer_channels = trio.open_memory_channel[ID](0) - dead_peer_channels = trio.open_memory_channel[ID](0) + peer_send, peer_receive = trio.open_memory_channel[ID](math.inf) + dead_peer_send, dead_peer_receive = trio.open_memory_channel[ID](math.inf) # Only keep the receive channels in `Pubsub`. # Therefore, we can only close from the receive side. - self.peer_receive_channel = peer_channels[1] - self.dead_peer_receive_channel = dead_peer_channels[1] + self.peer_receive_channel = peer_receive + self.dead_peer_receive_channel = dead_peer_receive # Register a notifee self.host.get_network().register_notifee( - PubsubNotifee(peer_channels[0], dead_peer_channels[0]) + PubsubNotifee(peer_send, dead_peer_send) ) # Register stream handlers for each pubsub router protocol to handle diff --git a/libp2p/pubsub/pubsub_notifee.py b/libp2p/pubsub/pubsub_notifee.py index be48190..08b4a5b 100644 --- a/libp2p/pubsub/pubsub_notifee.py +++ b/libp2p/pubsub/pubsub_notifee.py @@ -16,7 +16,6 @@ class PubsubNotifee(INotifee): initiator_peers_queue: "trio.MemorySendChannel[ID]" dead_peers_queue: "trio.MemorySendChannel[ID]" - dead_peers_queue_lock: trio.Lock def __init__( self, @@ -30,15 +29,13 @@ class PubsubNotifee(INotifee): can process dead peers after we disconnect from each other """ self.initiator_peers_queue = initiator_peers_queue - self.initiator_peers_queue_lock = trio.Lock() self.dead_peers_queue = dead_peers_queue - self.dead_peers_queue_lock = trio.Lock() async def opened_stream(self, network: INetwork, stream: INetStream) -> None: - pass + ... async def closed_stream(self, network: INetwork, stream: INetStream) -> None: - pass + ... async def connected(self, network: INetwork, conn: INetConn) -> None: """ @@ -49,17 +46,11 @@ class PubsubNotifee(INotifee): :param network: network the connection was opened on :param conn: connection that was opened """ - async with self.initiator_peers_queue_lock: - try: - await self.initiator_peers_queue.send(conn.muxed_conn.peer_id) - except ( - trio.BrokenResourceError, - trio.ClosedResourceError, - trio.BusyResourceError, - ): - # Raised when the receive channel is closed. - # TODO: Do something with loggers? - ... + try: + await self.initiator_peers_queue.send(conn.muxed_conn.peer_id) + except trio.BrokenResourceError: + # The receive channel is closed by Pubsub. We should do nothing here. + ... async def disconnected(self, network: INetwork, conn: INetConn) -> None: """ @@ -69,20 +60,14 @@ class PubsubNotifee(INotifee): :param network: network the connection was opened on :param conn: connection that was opened """ - async with self.dead_peers_queue_lock: - try: - await self.dead_peers_queue.send(conn.muxed_conn.peer_id) - except ( - trio.BrokenResourceError, - trio.ClosedResourceError, - trio.BusyResourceError, - ): - # Raised when the receive channel is closed. - # TODO: Do something with loggers? - ... + try: + await self.dead_peers_queue.send(conn.muxed_conn.peer_id) + except trio.BrokenResourceError: + # The receive channel is closed by Pubsub. We should do nothing here. + ... async def listen(self, network: INetwork, multiaddr: Multiaddr) -> None: - pass + ... async def listen_close(self, network: INetwork, multiaddr: Multiaddr) -> None: - pass + ...