From a7bc9fc358cd56c57f155b48dc7350fcfdcb433f Mon Sep 17 00:00:00 2001 From: mhchia Date: Thu, 12 Sep 2019 17:06:10 +0800 Subject: [PATCH] Asynchronously handling the accepted stream. --- libp2p/network/connection/swarm_connection.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libp2p/network/connection/swarm_connection.py b/libp2p/network/connection/swarm_connection.py index 946a6adb..f53ac7ee 100644 --- a/libp2p/network/connection/swarm_connection.py +++ b/libp2p/network/connection/swarm_connection.py @@ -46,13 +46,16 @@ class SwarmConn(INetConn): while True: print("!@# SwarmConn._handle_new_streams") stream = await self.conn.accept_stream() - print("!@# SwarmConn._handle_new_streams: accept_stream:", stream) - net_stream = await self._add_stream(stream) - print("!@# SwarmConn.calling common_stream_handler") - if self.swarm.common_stream_handler is not None: - await self.run_task(self.swarm.common_stream_handler(net_stream)) + # Asynchronously handle the accepted stream, to avoid blocking the next stream. + await self.run_task(self._handle_muxed_stream(stream)) + await self.close() + async def _handle_muxed_stream(self, muxed_stream: IMuxedStream) -> None: + net_stream = await self._add_stream(muxed_stream) + if self.swarm.common_stream_handler is not None: + await self.run_task(self.swarm.common_stream_handler(net_stream)) + async def _add_stream(self, muxed_stream: IMuxedStream) -> NetStream: print("!@# SwarmConn._add_stream:", muxed_stream) net_stream = NetStream(muxed_stream)