mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-07-31 17:01:58 +00:00
feat(swarm): Make executor for connection tasks explicit (#3097)
Previously, the executor for connection tasks silently defaulted to a `futures::executor::ThreadPool`. This causes issues such as https://github.com/libp2p/rust-libp2p/issues/2230. With this patch, we force the user to choose, which executor they want to run the connection tasks on which results in overall simpler API with less footguns. Closes #3068.
This commit is contained in:
@@ -39,7 +39,7 @@ use libp2p::{
|
||||
TokioMdns,
|
||||
},
|
||||
mplex, noise,
|
||||
swarm::{NetworkBehaviour, SwarmBuilder, SwarmEvent},
|
||||
swarm::{NetworkBehaviour, SwarmEvent},
|
||||
tcp, Multiaddr, PeerId, Transport,
|
||||
};
|
||||
use std::error::Error;
|
||||
@@ -97,23 +97,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
}
|
||||
|
||||
// Create a Swarm to manage peers and events.
|
||||
let mut swarm = {
|
||||
let mdns = TokioMdns::new(Default::default())?;
|
||||
let mut behaviour = MyBehaviour {
|
||||
floodsub: Floodsub::new(peer_id),
|
||||
mdns,
|
||||
};
|
||||
|
||||
behaviour.floodsub.subscribe(floodsub_topic.clone());
|
||||
|
||||
SwarmBuilder::new(transport, behaviour, peer_id)
|
||||
// We want the connection background tasks to be spawned
|
||||
// onto the tokio runtime.
|
||||
.executor(Box::new(|fut| {
|
||||
tokio::spawn(fut);
|
||||
}))
|
||||
.build()
|
||||
let mdns = TokioMdns::new(Default::default())?;
|
||||
let behaviour = MyBehaviour {
|
||||
floodsub: Floodsub::new(peer_id),
|
||||
mdns,
|
||||
};
|
||||
let mut swarm = libp2p_swarm::Swarm::with_tokio_executor(transport, behaviour, peer_id);
|
||||
|
||||
// Reach out to another node if specified
|
||||
if let Some(to_dial) = std::env::args().nth(1) {
|
||||
|
Reference in New Issue
Block a user