Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Mplex, Swarm, and BasicHost #303

Merged
merged 8 commits into from
Sep 21, 2019

Conversation

mhchia
Copy link
Contributor

@mhchia mhchia commented Sep 19, 2019

  • Extracted from Refactor mplex and start to add close detection #293.
  • Fix Detect peers' disconnections in Swarm #281.
  • Refactor generic_protocol_handler out of Mplex. It makes protocol negotiation abstracted out of Mplex, which makes testing easier. For example, we can test with a MplexStream without the initial protocol negotiation.
    • generic_protocol_handler is no longer registered by default in Swarm. Now, it is implemented inBasicHost, and is registered as the stream_handler in Swarm.
  • Add SwarmConn, which is a wrapper of MuxedConn, has the reference to Swarm.

Copy link
Contributor

@ralexstokes ralexstokes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good! left some comments below


async def _handle_new_streams(self) -> None:
# TODO: Break the loop when anything wrong in the connection.
while True:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does it make sense to put this while block inside a try block and then put await self.close() inside the finally: clause?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems pretty convenient but is it also possible that we catch unwanted exceptions? In the latest code I catch only the MuxedConnUnavailable in this loop.

https://github.com/libp2p/py-libp2p/pull/304/files#diff-cc33a5151ea2f5acfde91e1dd54be5d4R52-R63

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On second thought, I'm thinking it might be possibly an easier pattern for the shutting down logic in Mplex in #304 , will investigate more. Thanks for bringing this up.

@@ -34,17 +33,13 @@ class Mplex(IMuxedConn):
next_channel_id: int
streams: Dict[StreamID, MplexStream]
streams_lock: asyncio.Lock
new_stream_queue: "asyncio.Queue[IMuxedStream]"
shutdown: asyncio.Event

_tasks: List["asyncio.Future[Any]"]

# TODO: `generic_protocol_handler` should be refactored out of mplex conn.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this comment still relevant?

Copy link
Contributor Author

@mhchia mhchia Sep 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I removed this line in #305 , but I can also fix it here. Good catch!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you removed it elsewhere, we can leave it here -- just want to keep master as clean as possible :)

@mhchia
Copy link
Contributor Author

mhchia commented Sep 19, 2019

Big thanks for reviewing! I will solve the conflicts first and then get this ready again tmr.

@mhchia mhchia merged commit 99ef3f0 into libp2p:master Sep 21, 2019
@mhchia mhchia deleted the fix/refactor-mplex-swarm-host branch September 21, 2019 09:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Detect peers' disconnections in Swarm
2 participants