Peer stream is more efficient

This commit is contained in:
Igor Katson 2021-07-14 15:29:59 +01:00
parent 9340c231ba
commit 3a9a858438
6 changed files with 109 additions and 63 deletions

View file

@ -211,7 +211,6 @@ async fn async_main(opts: Opts, spawner: BlockingSpawner) -> anyhow::Result<()>
.ok_or_else(|| anyhow::anyhow!("magnet links without DHT are not supported"))?
.get_peers(info_hash)
.await?;
let dht_rx = flatten_dht_peers_stream(dht_rx);
let trackers = trackers
.into_iter()
@ -254,9 +253,7 @@ async fn async_main(opts: Opts, spawner: BlockingSpawner) -> anyhow::Result<()>
torrent_from_file(&opts.torrent_path)?
};
let dht_rx = match dht.as_ref() {
Some(dht) => Some(flatten_dht_peers_stream(
dht.get_peers(torrent.info_hash).await?,
)),
Some(dht) => Some(dht.get_peers(torrent.info_hash).await?),
None => None,
};
let trackers = torrent
@ -293,21 +290,6 @@ async fn async_main(opts: Opts, spawner: BlockingSpawner) -> anyhow::Result<()>
}
}
fn flatten_dht_peers_stream(
rx: impl Stream<Item = Result<SocketAddr, anyhow::Error>> + Unpin,
) -> impl Stream<Item = SocketAddr> + Unpin {
let rx = rx.filter_map(|addr| async move {
match addr {
Ok(addr) => Some(addr),
Err(e) => {
warn!("DHT peer receiver got an error: {:#}", e);
None
}
}
});
Box::pin(rx)
}
#[allow(clippy::too_many_arguments)]
async fn main_torrent_info(
opts: Opts,