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

identify: Replace FuturesUnordered with FuturesStream #302

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
18 changes: 11 additions & 7 deletions src/protocol/libp2p/identify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ use crate::{
substream::Substream,
transport::Endpoint,
types::{protocol::ProtocolName, SubstreamId},
utils::futures_stream::FuturesStream,
PeerId, DEFAULT_CHANNEL_SIZE,
};

use futures::{future::BoxFuture, stream::FuturesUnordered, Stream, StreamExt};
use futures::{future::BoxFuture, Stream, StreamExt};
use multiaddr::Multiaddr;
use prost::Message;
use tokio::sync::mpsc::{channel, Sender};
Expand Down Expand Up @@ -181,10 +182,10 @@ pub(crate) struct Identify {
protocols: Vec<String>,

/// Pending outbound substreams.
pending_outbound: FuturesUnordered<BoxFuture<'static, crate::Result<IdentifyResponse>>>,
pending_outbound: FuturesStream<BoxFuture<'static, crate::Result<IdentifyResponse>>>,

/// Pending inbound substreams.
pending_inbound: FuturesUnordered<BoxFuture<'static, ()>>,
pending_inbound: FuturesStream<BoxFuture<'static, ()>>,
}

impl Identify {
Expand All @@ -197,8 +198,8 @@ impl Identify {
public: config.public.expect("public key to be supplied"),
protocol_version: config.protocol_version,
user_agent: config.user_agent.unwrap_or(DEFAULT_AGENT.to_string()),
pending_inbound: FuturesUnordered::new(),
pending_outbound: FuturesUnordered::new(),
pending_inbound: FuturesStream::new(),
pending_outbound: FuturesStream::new(),
protocols: config.protocols.iter().map(|protocol| protocol.to_string()).collect(),
}
}
Expand Down Expand Up @@ -356,7 +357,10 @@ impl Identify {
loop {
tokio::select! {
event = self.service.next() => match event {
None => return,
None => {
tracing::warn!(target: LOG_TARGET, "transport service stream ended, terminating identify event loop");
Copy link

@iulianbarbu iulianbarbu Dec 11, 2024

Choose a reason for hiding this comment

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

Is this a warn because a terminated identify loop is not expected in normal operation?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep, we don't expect any of these components to exit under normal circumstances. If they exit however, this should give us enough information about what happened to isolate the issue 🙏

return
},
Some(TransportEvent::ConnectionEstablished { peer, endpoint }) => {
let _ = self.on_connection_established(peer, endpoint);
}
Expand Down Expand Up @@ -390,7 +394,7 @@ impl Identify {
.await;
}
Some(Err(error)) => tracing::debug!(target: LOG_TARGET, ?error, "failed to read ipfs identify response"),
None => return,
None => {}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/utils/futures_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ impl<F> FuturesStream<F> {
self.futures.len()
}

/// Check if the stream is empty.
pub fn is_empty(&self) -> bool {
self.futures.is_empty()
}

/// Push a future for processing.
pub fn push(&mut self, future: F) {
self.futures.push(future);
Expand Down
Loading