Skip to content

Commit cb9ecb4

Browse files
refactor(swarm)!: remove handler from NetworkBehaviourAction::Dial (libp2p#3328)
We create the `ConnectionId` for the new connection as part of `DialOpts`. This allows `NetworkBehaviour`s to accurately track state regarding their own dial attempts. This patch is the main enabler of libp2p#3254. Removing the `handler` field will allow us to deprecate the `NetworkBehaviour::new_handler` function in favor of four new ones that give more control over the connection lifecycle.
1 parent 2980b78 commit cb9ecb4

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

src/behaviour.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use libp2p_swarm::{
3838
ExpiredListenAddr, FromSwarm,
3939
},
4040
ConnectionId, ExternalAddresses, ListenAddresses, NetworkBehaviour, NetworkBehaviourAction,
41-
PollParameters, THandlerOutEvent,
41+
PollParameters, THandlerInEvent, THandlerOutEvent,
4242
};
4343
use std::{
4444
collections::{HashMap, VecDeque},
@@ -208,10 +208,7 @@ pub struct Behaviour {
208208
last_probe: Option<Instant>,
209209

210210
pending_actions: VecDeque<
211-
NetworkBehaviourAction<
212-
<Self as NetworkBehaviour>::OutEvent,
213-
<Self as NetworkBehaviour>::ConnectionHandler,
214-
>,
211+
NetworkBehaviourAction<<Self as NetworkBehaviour>::OutEvent, THandlerInEvent<Self>>,
215212
>,
216213

217214
probe_id: ProbeId,
@@ -389,14 +386,14 @@ impl Behaviour {
389386
&mut self,
390387
DialFailure {
391388
peer_id,
392-
handler,
389+
connection_id,
393390
error,
394-
}: DialFailure<<Self as NetworkBehaviour>::ConnectionHandler>,
391+
}: DialFailure,
395392
) {
396393
self.inner
397394
.on_swarm_event(FromSwarm::DialFailure(DialFailure {
398395
peer_id,
399-
handler,
396+
connection_id,
400397
error,
401398
}));
402399
if let Some(event) = self.as_server().on_outbound_dial_error(peer_id, error) {
@@ -560,10 +557,8 @@ impl NetworkBehaviour for Behaviour {
560557
}
561558
}
562559

563-
type Action = NetworkBehaviourAction<
564-
<Behaviour as NetworkBehaviour>::OutEvent,
565-
<Behaviour as NetworkBehaviour>::ConnectionHandler,
566-
>;
560+
type Action =
561+
NetworkBehaviourAction<<Behaviour as NetworkBehaviour>::OutEvent, THandlerInEvent<Behaviour>>;
567562

568563
// Trait implemented for `AsClient` and `AsServer` to handle events from the inner [`request_response::Behaviour`] Protocol.
569564
trait HandleInnerEvent {

src/behaviour/as_server.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use libp2p_request_response::{
2929
};
3030
use libp2p_swarm::{
3131
dial_opts::{DialOpts, PeerCondition},
32-
ConnectionId, DialError, NetworkBehaviour, NetworkBehaviourAction, PollParameters,
32+
ConnectionId, DialError, NetworkBehaviourAction, PollParameters,
3333
};
3434
use std::{
3535
collections::{HashMap, HashSet, VecDeque},
@@ -138,7 +138,6 @@ impl<'a> HandleInnerEvent for AsServer<'a> {
138138
)
139139
.addresses(addrs)
140140
.build(),
141-
handler: self.inner.new_handler(),
142141
},
143142
])
144143
}

0 commit comments

Comments
 (0)