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

feat(swarm)!: Allow NetworkBehaviours to manage incoming connections #3099

Closed
wants to merge 27 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d501019
Introduce `NetworkBehaviour::DialPayload`
thomaseizinger Nov 9, 2022
77c85f6
WIP Migrate production code
thomaseizinger Nov 9, 2022
118ffb6
Deprecate `new_handler`
thomaseizinger Nov 9, 2022
377c351
WIP: Completely remove `IntoConnectionHandler`
thomaseizinger Nov 14, 2022
9972eb4
Set supported protocols upon connection establishment
thomaseizinger Nov 15, 2022
fb9cdbc
Remove TODOs
thomaseizinger Nov 15, 2022
a96780d
Fix bad boolean logic
thomaseizinger Nov 15, 2022
3c9d98e
Merge branch 'master' into 2824-remove-into-connection-handler
thomaseizinger Nov 15, 2022
900edef
Fix gossipsub tests
thomaseizinger Nov 15, 2022
d1eea3a
Fix clippy warning
thomaseizinger Nov 15, 2022
98bf927
Update docs
thomaseizinger Nov 15, 2022
1cef941
Reduce diff
thomaseizinger Nov 15, 2022
ca3ef3e
Fix clippy errors
thomaseizinger Nov 15, 2022
a7f0685
Merge branch 'master' into 2824-remove-into-connection-handler
thomaseizinger Nov 16, 2022
d95b038
Update swarm/src/behaviour.rs
thomaseizinger Nov 16, 2022
edbbe41
Add changelog entry
thomaseizinger Nov 17, 2022
fef8d85
Merge branch 'master' into 2824-remove-into-connection-handler
thomaseizinger Nov 18, 2022
a942248
Remove unnecessary bounds
thomaseizinger Nov 18, 2022
0578134
Remove old example
thomaseizinger Nov 18, 2022
f7def2b
fmt
thomaseizinger Nov 18, 2022
a5a728b
Make `new_handler` fallible
thomaseizinger Nov 18, 2022
27e1b2d
Merge branch 'master' into 2824-remove-into-connection-handler
thomaseizinger Nov 19, 2022
19348e9
Allow each `NetworkBehaviour` to have their own `ConnectionDenied` re…
thomaseizinger Nov 22, 2022
9c55601
Merge branch 'master' into 2824-remove-into-connection-handler
thomaseizinger Nov 23, 2022
527eeda
Merge branch 'master' into 2824-remove-into-connection-handler
thomaseizinger Nov 29, 2022
a269e9d
Revert "Allow each `NetworkBehaviour` to have their own `ConnectionDe…
thomaseizinger Dec 7, 2022
dcb4f96
Always box cause for denied connection
thomaseizinger Dec 7, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions misc/metrics/src/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub struct Metrics {
connections_established: Family<ConnectionEstablishedLabels, Counter>,
connections_closed: Family<ConnectionClosedLabels, Counter>,

connections_denied: Family<AddressLabels, Counter>,

new_listen_addr: Family<AddressLabels, Counter>,
expired_listen_addr: Family<AddressLabels, Counter>,

Expand Down Expand Up @@ -60,6 +62,13 @@ impl Metrics {
Box::new(connections_incoming_error.clone()),
);

let connections_denied = Family::default();
sub_registry.register(
"connections_denied",
"Number of denied connections",
Box::new(connections_denied.clone()),
);

let new_listen_addr = Family::default();
sub_registry.register(
"new_listen_addr",
Expand Down Expand Up @@ -128,6 +137,7 @@ impl Metrics {
connections_incoming_error,
connections_established,
connections_closed,
connections_denied,
new_listen_addr,
expired_listen_addr,
listener_closed,
Expand Down Expand Up @@ -269,6 +279,13 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
libp2p_swarm::SwarmEvent::Dialing(_) => {
self.dial_attempt.inc();
}
libp2p_swarm::SwarmEvent::ConnectionDenied { endpoint, .. } => {
self.connections_denied
.get_or_create(&AddressLabels {
protocols: protocol_stack::as_string(endpoint.get_remote_address()),
})
.inc();
}
}
}
}
Expand Down
33 changes: 13 additions & 20 deletions protocols/autonat/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ use libp2p_request_response::{
ProtocolSupport, RequestId, RequestResponse, RequestResponseConfig, RequestResponseEvent,
RequestResponseMessage, ResponseChannel,
};
use libp2p_swarm::behaviour::THandlerInEvent;
use libp2p_swarm::{
behaviour::{
AddressChange, ConnectionClosed, ConnectionEstablished, DialFailure, ExpiredExternalAddr,
ExpiredListenAddr, FromSwarm,
},
ConnectionHandler, IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction,
PollParameters,
ConnectionHandler,
};
use libp2p_swarm::{NetworkBehaviour, NetworkBehaviourAction, PollParameters};
use std::{
collections::{HashMap, VecDeque},
iter,
Expand Down Expand Up @@ -374,20 +375,9 @@ impl Behaviour {
}
}

fn on_dial_failure(
&mut self,
DialFailure {
peer_id,
handler,
error,
}: DialFailure<<Self as NetworkBehaviour>::ConnectionHandler>,
) {
fn on_dial_failure(&mut self, DialFailure { peer_id, error }: DialFailure) {
self.inner
.on_swarm_event(FromSwarm::DialFailure(DialFailure {
peer_id,
handler,
error,
}));
.on_swarm_event(FromSwarm::DialFailure(DialFailure { peer_id, error }));
if let Some(event) = self.as_server().on_outbound_dial_error(peer_id, error) {
self.pending_out_events
.push_back(Event::InboundProbe(event));
Expand Down Expand Up @@ -467,8 +457,12 @@ impl NetworkBehaviour for Behaviour {
}
}

fn new_handler(&mut self) -> Self::ConnectionHandler {
self.inner.new_handler()
fn new_handler(
&mut self,
peer: &PeerId,
connected_point: &ConnectedPoint,
) -> Result<Self::ConnectionHandler, Box<dyn std::error::Error + Send + 'static>> {
self.inner.new_handler(peer, connected_point)
}

fn addresses_of_peer(&mut self, peer: &PeerId) -> Vec<Multiaddr> {
Expand Down Expand Up @@ -529,8 +523,7 @@ impl NetworkBehaviour for Behaviour {
&mut self,
peer_id: PeerId,
connection_id: ConnectionId,
event: <<Self::ConnectionHandler as IntoConnectionHandler>::Handler as
ConnectionHandler>::OutEvent,
event: <Self::ConnectionHandler as ConnectionHandler>::OutEvent,
) {
self.inner
.on_connection_handler_event(peer_id, connection_id, event)
Expand All @@ -539,7 +532,7 @@ impl NetworkBehaviour for Behaviour {

type Action = NetworkBehaviourAction<
<Behaviour as NetworkBehaviour>::OutEvent,
<Behaviour as NetworkBehaviour>::ConnectionHandler,
THandlerInEvent<<Behaviour as NetworkBehaviour>::ConnectionHandler>,
>;

// Trait implemented for `AsClient` as `AsServer` to handle events from the inner [`RequestResponse`] Protocol.
Expand Down
3 changes: 1 addition & 2 deletions protocols/autonat/src/behaviour/as_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use libp2p_request_response::{
};
use libp2p_swarm::{
dial_opts::{DialOpts, PeerCondition},
DialError, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
DialError, NetworkBehaviourAction, PollParameters,
};
use std::{
collections::{HashMap, HashSet, VecDeque},
Expand Down Expand Up @@ -137,7 +137,6 @@ impl<'a> HandleInnerEvent for AsServer<'a> {
.override_dial_concurrency_factor(NonZeroU8::new(1).expect("1 > 0"))
.addresses(addrs)
.build(),
handler: self.inner.new_handler(),
});
}
Err((status_text, error)) => {
Expand Down
Loading