Skip to content

Commit e01c02e

Browse files
committed
review: Don't bubble the Substream from the handler
to the behaviour, instead keep track of the info as it doesn't change.
1 parent 3db1ec1 commit e01c02e

File tree

2 files changed

+63
-57
lines changed

2 files changed

+63
-57
lines changed

protocols/identify/src/behaviour.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
1919
// DEALINGS IN THE SOFTWARE.
2020

21-
use crate::handler::{self, InEvent, Proto, Reply};
22-
use crate::protocol::{Info, ReplySubstream, UpgradeError};
21+
use crate::handler::{self, InEvent, Proto};
22+
use crate::protocol::{Info, UpgradeError};
2323
use libp2p_core::{
2424
connection::ConnectionId, multiaddr::Protocol, ConnectedPoint, Multiaddr, PeerId, PublicKey,
2525
};
2626
use libp2p_swarm::behaviour::{ConnectionClosed, ConnectionEstablished, DialFailure, FromSwarm};
2727
use libp2p_swarm::{
2828
dial_opts::DialOpts, AddressScore, ConnectionHandler, ConnectionHandlerUpgrErr, DialError,
29-
IntoConnectionHandler, NegotiatedSubstream, NetworkBehaviour, NetworkBehaviourAction,
30-
NotifyHandler, PollParameters,
29+
IntoConnectionHandler, NetworkBehaviour, NetworkBehaviourAction, NotifyHandler, PollParameters,
3130
};
3231
use lru::LruCache;
3332
use std::num::NonZeroUsize;
@@ -63,7 +62,6 @@ pub struct Behaviour {
6362
/// A pending reply to an inbound identification request.
6463
struct Request {
6564
peer: PeerId,
66-
io: ReplySubstream<NegotiatedSubstream>,
6765
observed: Multiaddr,
6866
}
6967

@@ -273,7 +271,7 @@ impl NetworkBehaviour for Behaviour {
273271
peer_id,
274272
}));
275273
}
276-
handler::Event::Identify(sender) => {
274+
handler::Event::Identify => {
277275
let observed = self
278276
.connected
279277
.get(&peer_id)
@@ -285,7 +283,6 @@ impl NetworkBehaviour for Behaviour {
285283
);
286284
self.requests.push_back(Request {
287285
peer: peer_id,
288-
io: sender,
289286
observed: observed.clone(),
290287
});
291288
}
@@ -343,7 +340,7 @@ impl NetworkBehaviour for Behaviour {
343340
}
344341

345342
// Check for pending requests to send back to the handler for reply.
346-
if let Some(Request { peer, io, observed }) = self.requests.pop_front() {
343+
if let Some(Request { peer, observed }) = self.requests.pop_front() {
347344
let info = Info {
348345
listen_addrs: listen_addrs(params),
349346
protocols: supported_protocols(params),
@@ -355,7 +352,7 @@ impl NetworkBehaviour for Behaviour {
355352
return Poll::Ready(NetworkBehaviourAction::NotifyHandler {
356353
peer_id: peer,
357354
handler: NotifyHandler::Any,
358-
event: InEvent::Identify(Reply { peer, info, io }),
355+
event: InEvent::Identify(info),
359356
});
360357
}
361358

protocols/identify/src/handler.rs

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -68,22 +68,14 @@ impl IntoConnectionHandler for Proto {
6868
/// A pending reply to an inbound identification request.
6969
enum Pending {
7070
/// The reply is queued for sending.
71-
Queued(Reply),
71+
Queued(ReplySubstream<NegotiatedSubstream>),
7272
/// The reply is being sent.
7373
Sending {
7474
peer: PeerId,
7575
io: Pin<Box<dyn Future<Output = Result<(), UpgradeError>> + Send>>,
7676
},
7777
}
7878

79-
/// A reply to an inbound identification request.
80-
#[derive(Debug)]
81-
pub struct Reply {
82-
pub peer: PeerId,
83-
pub io: ReplySubstream<NegotiatedSubstream>,
84-
pub info: Info,
85-
}
86-
8779
/// Protocol handler for sending and receiving identification requests.
8880
///
8981
/// Outbound requests are sent periodically. The handler performs expects
@@ -102,6 +94,9 @@ pub struct Handler {
10294
>; 4],
10395
>,
10496

97+
/// Identify request information.
98+
info: Option<Info>,
99+
105100
/// Pending replies to send.
106101
pending_replies: VecDeque<Pending>,
107102

@@ -126,7 +121,7 @@ pub enum Event {
126121
/// We actively pushed our identification information to the remote.
127122
IdentificationPushed,
128123
/// We received a request for identification.
129-
Identify(ReplySubstream<NegotiatedSubstream>),
124+
Identify,
130125
/// Failed to identify the remote, or to reply to an identification request.
131126
IdentificationError(ConnectionHandlerUpgrErr<UpgradeError>),
132127
}
@@ -137,7 +132,7 @@ pub enum InEvent {
137132
/// Identifying information of the local node that is pushed to a remote.
138133
Push(Info),
139134
/// Identifying information requested from this node.
140-
Identify(Reply),
135+
Identify(Info),
141136
}
142137

143138
impl Handler {
@@ -147,6 +142,7 @@ impl Handler {
147142
remote_peer_id,
148143
inbound_identify_push: Default::default(),
149144
events: SmallVec::new(),
145+
info: None,
150146
pending_replies: VecDeque::new(),
151147
trigger_next_identify: Delay::new(initial_delay),
152148
keep_alive: KeepAlive::Yes,
@@ -164,9 +160,22 @@ impl Handler {
164160
>,
165161
) {
166162
match output {
167-
EitherOutput::First(substream) => self
168-
.events
169-
.push(ConnectionHandlerEvent::Custom(Event::Identify(substream))),
163+
EitherOutput::First(substream) => {
164+
// If we already have `Info` we can proceed responding to the Identify request,
165+
// if not, we request `Info` from the behaviour.
166+
if self.info.is_none() {
167+
self.events
168+
.push(ConnectionHandlerEvent::Custom(Event::Identify));
169+
}
170+
if !self.pending_replies.is_empty() {
171+
warn!(
172+
"New inbound identify request from {} while a previous one \
173+
is still pending. Queueing the new one.",
174+
self.remote_peer_id,
175+
);
176+
}
177+
self.pending_replies.push_back(Pending::Queued(substream));
178+
}
170179
EitherOutput::Second(fut) => {
171180
if self.inbound_identify_push.replace(fut).is_some() {
172181
warn!(
@@ -249,15 +258,8 @@ impl ConnectionHandler for Handler {
249258
),
250259
});
251260
}
252-
InEvent::Identify(reply) => {
253-
if !self.pending_replies.is_empty() {
254-
warn!(
255-
"New inbound identify request from {} while a previous one \
256-
is still pending. Queueing the new one.",
257-
reply.peer,
258-
);
259-
}
260-
self.pending_replies.push_back(Pending::Queued(reply));
261+
InEvent::Identify(info) => {
262+
self.info = Some(info);
261263
}
262264
}
263265
}
@@ -301,31 +303,38 @@ impl ConnectionHandler for Handler {
301303
}
302304

303305
// Check for pending replies to send.
304-
if let Some(mut pending) = self.pending_replies.pop_front() {
305-
loop {
306-
match pending {
307-
Pending::Queued(Reply { peer, io, info }) => {
308-
let io = Box::pin(io.send(info));
309-
pending = Pending::Sending { peer, io };
310-
}
311-
Pending::Sending { peer, mut io } => {
312-
match Future::poll(Pin::new(&mut io), cx) {
313-
Poll::Pending => {
314-
self.pending_replies
315-
.push_front(Pending::Sending { peer, io });
316-
return Poll::Pending;
317-
}
318-
Poll::Ready(Ok(())) => {
319-
return Poll::Ready(ConnectionHandlerEvent::Custom(
320-
Event::Identification(peer),
321-
));
322-
}
323-
Poll::Ready(Err(err)) => {
324-
return Poll::Ready(ConnectionHandlerEvent::Custom(
325-
Event::IdentificationError(ConnectionHandlerUpgrErr::Upgrade(
326-
libp2p_core::upgrade::UpgradeError::Apply(err),
327-
)),
328-
))
306+
if let Some(ref info) = self.info {
307+
if let Some(mut pending) = self.pending_replies.pop_front() {
308+
loop {
309+
match pending {
310+
Pending::Queued(io) => {
311+
let io = Box::pin(io.send(info.clone()));
312+
pending = Pending::Sending {
313+
peer: self.remote_peer_id,
314+
io,
315+
};
316+
}
317+
Pending::Sending { peer, mut io } => {
318+
match Future::poll(Pin::new(&mut io), cx) {
319+
Poll::Pending => {
320+
self.pending_replies
321+
.push_front(Pending::Sending { peer, io });
322+
return Poll::Pending;
323+
}
324+
Poll::Ready(Ok(())) => {
325+
return Poll::Ready(ConnectionHandlerEvent::Custom(
326+
Event::Identification(peer),
327+
));
328+
}
329+
Poll::Ready(Err(err)) => {
330+
return Poll::Ready(ConnectionHandlerEvent::Custom(
331+
Event::IdentificationError(
332+
ConnectionHandlerUpgrErr::Upgrade(
333+
libp2p_core::upgrade::UpgradeError::Apply(err),
334+
),
335+
),
336+
))
337+
}
329338
}
330339
}
331340
}

0 commit comments

Comments
 (0)