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

Full support for multiple connections per peer in libp2p-swarm. #1519

Merged
merged 14 commits into from Mar 31, 2020

Conversation

romanb
Copy link
Contributor

@romanb romanb commented Mar 25, 2020

This is a follow-up to #1440 that introduces "first class" support for multiple connections per peer to libp2p-swarm. Concretely:

  • New callbacks inject_connection_established and inject_connection_closed are added to the NetworkBehaviour with empty default impls.
  • inject_connected and inject_disconnected no longer get the endpoint as an argument. This is primarily because these callbacks are invoked for the first established connection, respectively last closed connection, whose endpoints may now differ, as opposed to the previous behaviour. Network behaviours that made use of the endpoint will naturally have to implement the new callbacks and split the code previously in inject_connected / inject_disconnected, as is demonstrated in some of the behaviours updated in this PR.
  • NetworkBehaviourAction::DialPeer as well es ExpandedSwarm::dialnow take another parameter, a DialPeerCondition, which specifies the conditions under which a new dialing attempt should be initiated. The default (and what most behaviours migrating old code will want to use) is DialPeerCondition::Disconnected.
  • The NetworkConfig of a swarm (which includes connection limits) can now be fully customised via SwarmBuilder::network_config, in order to avoid code duplication. For example, substrate will probably want to configure the maximum connections per peer to 2 (just to permit concurrent dialing attempts) and possibly also configure limits for concurrent inbound and outbound connections.

It is worth mentioning that I took the opportunity to change inject_connected to take the peer ID by reference, rather than by value, just like inject_disconnected and most other methods. This simplifies things a bit in the derive-macro code and can avoid a bit of unnecessary cloning. I further did this because the signature of inject_connected changed anyway in this PR.

This PR is expected to be rebased over #1515 eventually.

This commit makes the notion of multiple connections per peer
first-class in the API of libp2p-swarm, introducing the new
callbacks `inject_connection_established` and
`inject_connection_closed`. The `endpoint` parameter from
`inject_connected` and `inject_disconnected` is removed,
since the first connection to open may not be the last
connection to close, i.e. it cannot be guaranteed,
as was previously the case, that the endpoints passed
to these callbacks match up.
So that identify requests can be answered with the correct
observed address of the connection on which the request
arrives.
swarm/src/behaviour.rs Show resolved Hide resolved
swarm/src/lib.rs Outdated Show resolved Hide resolved
swarm/src/lib.rs Outdated Show resolved Hide resolved
swarm/src/lib.rs Outdated Show resolved Hide resolved
@tomaka
Copy link
Member

tomaka commented Mar 26, 2020

The only thing that I think should be resolved before merging is the SwarmEvent::Dialing. My other two remarks are mostly for discussion.

Copy link
Member

@tomaka tomaka left a comment

Choose a reason for hiding this comment

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

👍

@romanb romanb merged commit be97046 into libp2p:master Mar 31, 2020
@romanb romanb deleted the multicon-swarm branch March 31, 2020 13:41
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.

None yet

3 participants