fix: poll swarm first, use SwarmEvents
polling swarm first seems important, otherwise there'll be a race to
first listening endpoint getting up at least.
@@ -414,8 +414,31 @@ impl<Types: SwarmTypes> Future for IpfsFuture<Types> {
fn poll(mut self: Pin<&mut Self>, ctx: &mut Context) -> Poll<Self::Output> {
use futures::Stream;
- use libp2p::Swarm;
+ use libp2p::{Swarm, swarm::SwarmEvent};
+
+ // begin by polling the swarm so that initially it'll first have chance to bind listeners
+ // and such. TODO: this no longer needs to be a swarm event but perhaps we should
+ // consolidate logging of these events here, if necessary?
loop {
+ let inner = {
+ let next = self.swarm.next_event();
+ futures::pin_mut!(next);
+ match next.poll(ctx) {
+ Poll::Ready(inner) => inner,
+ Poll::Pending => break,
+ }
+ };
+ match inner {
+ SwarmEvent::Behaviour(()) => {},
+ SwarmEvent::Connected(_peer_id) => {},
+ SwarmEvent::Disconnected(_peer_id) => {},
+ SwarmEvent::NewListenAddr(_addr) => {},
+ SwarmEvent::ExpiredListenAddr(_addr) => {},
+ SwarmEvent::UnreachableAddr { peer_id: _peer_id, address: _address, error: _error } => {},
+ SwarmEvent::StartConnect(_peer_id) => {}
+ }
+ }
+
// temporary pinning of the receivers should be safe as we are pinning through the
// already pinned self. with the receivers we can also safely ignore exhaustion
// as those are fused.
@@ -478,20 +501,7 @@ impl<Types: SwarmTypes> Future for IpfsFuture<Types> {
}
}
- {
- let poll = Pin::new(&mut self.swarm).poll_next(ctx);
- match poll {
- Poll::Ready(Some(_)) => {}
- Poll::Ready(None) => {
- // this should never happen with libp2p swarm
- return Poll::Ready(());
- }
- Poll::Pending => {
- return Poll::Pending;
- }
- }
- }
- }
+ Poll::Pending
}
}
