mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-24 23:31:33 +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:
@ -67,7 +67,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
let behaviour = Behaviour::new(local_key.public());
|
||||
|
||||
let mut swarm = Swarm::new(transport, behaviour, local_peer_id);
|
||||
let mut swarm = Swarm::with_async_std_executor(transport, behaviour, local_peer_id);
|
||||
swarm.listen_on(
|
||||
Multiaddr::empty()
|
||||
.with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED))
|
||||
|
@ -57,7 +57,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
|
||||
let behaviour = Behaviour::new(local_key.public());
|
||||
|
||||
let mut swarm = Swarm::new(transport, behaviour, local_peer_id);
|
||||
let mut swarm = Swarm::with_async_std_executor(transport, behaviour, local_peer_id);
|
||||
swarm.listen_on(
|
||||
Multiaddr::empty()
|
||||
.with(Protocol::Ip4(Ipv4Addr::UNSPECIFIED))
|
||||
|
@ -40,7 +40,7 @@ async fn init_swarm(config: Config) -> Swarm<Behaviour> {
|
||||
let local_id = PeerId::from_public_key(&keypair.public());
|
||||
let transport = development_transport(keypair).await.unwrap();
|
||||
let behaviour = Behaviour::new(local_id, config);
|
||||
Swarm::new(transport, behaviour, local_id)
|
||||
Swarm::with_async_std_executor(transport, behaviour, local_id)
|
||||
}
|
||||
|
||||
async fn spawn_server(kill: oneshot::Receiver<()>) -> (PeerId, Multiaddr) {
|
||||
|
@ -39,7 +39,7 @@ async fn init_swarm(config: Config) -> Swarm<Behaviour> {
|
||||
let local_id = PeerId::from_public_key(&keypair.public());
|
||||
let transport = development_transport(keypair).await.unwrap();
|
||||
let behaviour = Behaviour::new(local_id, config);
|
||||
Swarm::new(transport, behaviour, local_id)
|
||||
Swarm::with_async_std_executor(transport, behaviour, local_id)
|
||||
}
|
||||
|
||||
async fn init_server(config: Option<Config>) -> (Swarm<Behaviour>, PeerId, Multiaddr) {
|
||||
|
@ -19,7 +19,7 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use clap::Parser;
|
||||
use futures::executor::block_on;
|
||||
use futures::executor::{block_on, ThreadPool};
|
||||
use futures::future::FutureExt;
|
||||
use futures::stream::StreamExt;
|
||||
use libp2p::core::multiaddr::{Multiaddr, Protocol};
|
||||
@ -155,9 +155,12 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
dcutr: dcutr::behaviour::Behaviour::new(),
|
||||
};
|
||||
|
||||
let mut swarm = SwarmBuilder::new(transport, behaviour, local_peer_id)
|
||||
.dial_concurrency_factor(10_u8.try_into().unwrap())
|
||||
.build();
|
||||
let mut swarm = match ThreadPool::new() {
|
||||
Ok(tp) => SwarmBuilder::with_executor(transport, behaviour, local_peer_id, tp),
|
||||
Err(_) => SwarmBuilder::without_executor(transport, behaviour, local_peer_id),
|
||||
}
|
||||
.dial_concurrency_factor(10_u8.try_into().unwrap())
|
||||
.build();
|
||||
|
||||
swarm
|
||||
.listen_on(
|
||||
|
@ -98,7 +98,7 @@ fn build_relay() -> Swarm<relay::Relay> {
|
||||
|
||||
let transport = build_transport(MemoryTransport::default().boxed(), local_public_key);
|
||||
|
||||
Swarm::new(
|
||||
Swarm::with_threadpool_executor(
|
||||
transport,
|
||||
relay::Relay::new(
|
||||
local_peer_id,
|
||||
@ -122,7 +122,7 @@ fn build_client() -> Swarm<Client> {
|
||||
local_public_key,
|
||||
);
|
||||
|
||||
Swarm::new(
|
||||
Swarm::with_threadpool_executor(
|
||||
transport,
|
||||
Client {
|
||||
relay: behaviour,
|
||||
|
@ -171,7 +171,7 @@ fn build_node() -> (Multiaddr, Swarm<Gossipsub>) {
|
||||
.build()
|
||||
.unwrap();
|
||||
let behaviour = Gossipsub::new(MessageAuthenticity::Author(peer_id), config).unwrap();
|
||||
let mut swarm = Swarm::new(transport, behaviour, peer_id);
|
||||
let mut swarm = Swarm::without_executor(transport, behaviour, peer_id);
|
||||
|
||||
let port = 1 + random::<u64>();
|
||||
let mut addr: Multiaddr = Protocol::Memory(port).into();
|
||||
|
@ -55,7 +55,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||
local_key.public(),
|
||||
));
|
||||
|
||||
let mut swarm = Swarm::new(transport, behaviour, local_peer_id);
|
||||
let mut swarm = Swarm::with_async_std_executor(transport, behaviour, local_peer_id);
|
||||
|
||||
// Tell the swarm to listen on all interfaces and a random, OS-assigned
|
||||
// port.
|
||||
|
@ -584,7 +584,7 @@ mod tests {
|
||||
let protocol = Behaviour::new(
|
||||
Config::new("a".to_string(), pubkey.clone()).with_agent_version("b".to_string()),
|
||||
);
|
||||
let swarm = Swarm::new(transport, protocol, pubkey.to_peer_id());
|
||||
let swarm = Swarm::with_async_std_executor(transport, protocol, pubkey.to_peer_id());
|
||||
(swarm, pubkey)
|
||||
};
|
||||
|
||||
@ -593,7 +593,7 @@ mod tests {
|
||||
let protocol = Behaviour::new(
|
||||
Config::new("c".to_string(), pubkey.clone()).with_agent_version("d".to_string()),
|
||||
);
|
||||
let swarm = Swarm::new(transport, protocol, pubkey.to_peer_id());
|
||||
let swarm = Swarm::with_async_std_executor(transport, protocol, pubkey.to_peer_id());
|
||||
(swarm, pubkey)
|
||||
};
|
||||
|
||||
@ -661,7 +661,7 @@ mod tests {
|
||||
let (mut swarm1, pubkey1) = {
|
||||
let (pubkey, transport) = transport();
|
||||
let protocol = Behaviour::new(Config::new("a".to_string(), pubkey.clone()));
|
||||
let swarm = Swarm::new(transport, protocol, pubkey.to_peer_id());
|
||||
let swarm = Swarm::with_async_std_executor(transport, protocol, pubkey.to_peer_id());
|
||||
(swarm, pubkey)
|
||||
};
|
||||
|
||||
@ -670,7 +670,7 @@ mod tests {
|
||||
let protocol = Behaviour::new(
|
||||
Config::new("a".to_string(), pubkey.clone()).with_agent_version("b".to_string()),
|
||||
);
|
||||
let swarm = Swarm::new(transport, protocol, pubkey.to_peer_id());
|
||||
let swarm = Swarm::with_async_std_executor(transport, protocol, pubkey.to_peer_id());
|
||||
(swarm, pubkey)
|
||||
};
|
||||
|
||||
@ -742,7 +742,7 @@ mod tests {
|
||||
.with_initial_delay(Duration::from_secs(10)),
|
||||
);
|
||||
|
||||
Swarm::new(transport, protocol, pubkey.to_peer_id())
|
||||
Swarm::with_async_std_executor(transport, protocol, pubkey.to_peer_id())
|
||||
};
|
||||
|
||||
let mut swarm2 = {
|
||||
@ -751,7 +751,7 @@ mod tests {
|
||||
Config::new("a".to_string(), pubkey.clone()).with_agent_version("b".to_string()),
|
||||
);
|
||||
|
||||
Swarm::new(transport, protocol, pubkey.to_peer_id())
|
||||
Swarm::with_async_std_executor(transport, protocol, pubkey.to_peer_id())
|
||||
};
|
||||
|
||||
let swarm1_peer_id = *swarm1.local_peer_id();
|
||||
|
@ -66,7 +66,7 @@ fn build_node_with_config(cfg: KademliaConfig) -> (Multiaddr, TestSwarm) {
|
||||
let store = MemoryStore::new(local_id);
|
||||
let behaviour = Kademlia::with_config(local_id, store, cfg);
|
||||
|
||||
let mut swarm = Swarm::new(transport, behaviour, local_id);
|
||||
let mut swarm = Swarm::without_executor(transport, behaviour, local_id);
|
||||
|
||||
let address: Multiaddr = Protocol::Memory(random::<u64>()).into();
|
||||
swarm.listen_on(address.clone()).unwrap();
|
||||
|
@ -62,7 +62,7 @@ async fn create_swarm(config: MdnsConfig) -> Result<Swarm<Mdns>, Box<dyn Error>>
|
||||
let peer_id = PeerId::from(id_keys.public());
|
||||
let transport = libp2p::development_transport(id_keys).await?;
|
||||
let behaviour = Mdns::new(config)?;
|
||||
let mut swarm = Swarm::new(transport, behaviour, peer_id);
|
||||
let mut swarm = Swarm::with_async_std_executor(transport, behaviour, peer_id);
|
||||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||
Ok(swarm)
|
||||
}
|
||||
|
@ -58,7 +58,7 @@ async fn create_swarm(config: MdnsConfig) -> Result<Swarm<TokioMdns>, Box<dyn Er
|
||||
let peer_id = PeerId::from(id_keys.public());
|
||||
let transport = libp2p::tokio_development_transport(id_keys)?;
|
||||
let behaviour = TokioMdns::new(config)?;
|
||||
let mut swarm = Swarm::new(transport, behaviour, peer_id);
|
||||
let mut swarm = Swarm::with_tokio_executor(transport, behaviour, peer_id);
|
||||
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
||||
Ok(swarm)
|
||||
}
|
||||
|
@ -43,10 +43,11 @@ fn ping_pong() {
|
||||
let cfg = ping::Config::new().with_interval(Duration::from_millis(10));
|
||||
|
||||
let (peer1_id, trans) = mk_transport(muxer);
|
||||
let mut swarm1 = Swarm::new(trans, Behaviour::new(cfg.clone()), peer1_id);
|
||||
let mut swarm1 =
|
||||
Swarm::with_async_std_executor(trans, Behaviour::new(cfg.clone()), peer1_id);
|
||||
|
||||
let (peer2_id, trans) = mk_transport(muxer);
|
||||
let mut swarm2 = Swarm::new(trans, Behaviour::new(cfg), peer2_id);
|
||||
let mut swarm2 = Swarm::with_async_std_executor(trans, Behaviour::new(cfg), peer2_id);
|
||||
|
||||
let (mut tx, mut rx) = mpsc::channel::<Multiaddr>(1);
|
||||
|
||||
@ -127,10 +128,11 @@ fn max_failures() {
|
||||
.with_max_failures(max_failures.into());
|
||||
|
||||
let (peer1_id, trans) = mk_transport(muxer);
|
||||
let mut swarm1 = Swarm::new(trans, Behaviour::new(cfg.clone()), peer1_id);
|
||||
let mut swarm1 =
|
||||
Swarm::with_async_std_executor(trans, Behaviour::new(cfg.clone()), peer1_id);
|
||||
|
||||
let (peer2_id, trans) = mk_transport(muxer);
|
||||
let mut swarm2 = Swarm::new(trans, Behaviour::new(cfg), peer2_id);
|
||||
let mut swarm2 = Swarm::with_async_std_executor(trans, Behaviour::new(cfg), peer2_id);
|
||||
|
||||
let (mut tx, mut rx) = mpsc::channel::<Multiaddr>(1);
|
||||
|
||||
@ -197,10 +199,10 @@ fn max_failures() {
|
||||
#[test]
|
||||
fn unsupported_doesnt_fail() {
|
||||
let (peer1_id, trans) = mk_transport(MuxerChoice::Mplex);
|
||||
let mut swarm1 = Swarm::new(trans, keep_alive::Behaviour, peer1_id);
|
||||
let mut swarm1 = Swarm::with_async_std_executor(trans, keep_alive::Behaviour, peer1_id);
|
||||
|
||||
let (peer2_id, trans) = mk_transport(MuxerChoice::Mplex);
|
||||
let mut swarm2 = Swarm::new(trans, Behaviour::default(), peer2_id);
|
||||
let mut swarm2 = Swarm::with_async_std_executor(trans, Behaviour::default(), peer2_id);
|
||||
|
||||
let (mut tx, mut rx) = mpsc::channel::<Multiaddr>(1);
|
||||
|
||||
|
@ -66,7 +66,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
)),
|
||||
};
|
||||
|
||||
let mut swarm = Swarm::new(transport, behaviour, local_peer_id);
|
||||
let mut swarm = Swarm::without_executor(transport, behaviour, local_peer_id);
|
||||
|
||||
// Listen on all interfaces
|
||||
let listen_addr = Multiaddr::empty()
|
||||
|
@ -291,7 +291,7 @@ fn build_relay() -> Swarm<Relay> {
|
||||
|
||||
let transport = upgrade_transport(MemoryTransport::default().boxed(), local_public_key);
|
||||
|
||||
Swarm::new(
|
||||
Swarm::with_threadpool_executor(
|
||||
transport,
|
||||
Relay {
|
||||
ping: ping::Behaviour::new(ping::Config::new()),
|
||||
@ -318,7 +318,7 @@ fn build_client() -> Swarm<Client> {
|
||||
local_public_key,
|
||||
);
|
||||
|
||||
Swarm::new(
|
||||
Swarm::with_threadpool_executor(
|
||||
transport,
|
||||
Client {
|
||||
ping: ping::Behaviour::new(ping::Config::new()),
|
||||
|
@ -25,7 +25,7 @@ use libp2p::multiaddr::Protocol;
|
||||
use libp2p::ping;
|
||||
use libp2p::swarm::{keep_alive, SwarmEvent};
|
||||
use libp2p::Swarm;
|
||||
use libp2p::{development_transport, rendezvous, Multiaddr};
|
||||
use libp2p::{rendezvous, tokio_development_transport, Multiaddr};
|
||||
use std::time::Duration;
|
||||
use void::Void;
|
||||
|
||||
@ -41,8 +41,8 @@ async fn main() {
|
||||
.parse()
|
||||
.unwrap();
|
||||
|
||||
let mut swarm = Swarm::new(
|
||||
development_transport(identity.clone()).await.unwrap(),
|
||||
let mut swarm = Swarm::with_tokio_executor(
|
||||
tokio_development_transport(identity.clone()).unwrap(),
|
||||
MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour::new(identity.clone()),
|
||||
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
||||
|
@ -24,7 +24,7 @@ use libp2p::core::PeerId;
|
||||
use libp2p::ping;
|
||||
use libp2p::swarm::{NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p::Multiaddr;
|
||||
use libp2p::{development_transport, rendezvous};
|
||||
use libp2p::{rendezvous, tokio_development_transport};
|
||||
use libp2p_swarm::AddressScore;
|
||||
use std::time::Duration;
|
||||
|
||||
@ -39,8 +39,8 @@ async fn main() {
|
||||
|
||||
let identity = identity::Keypair::generate_ed25519();
|
||||
|
||||
let mut swarm = Swarm::new(
|
||||
development_transport(identity.clone()).await.unwrap(),
|
||||
let mut swarm = Swarm::with_tokio_executor(
|
||||
tokio_development_transport(identity.clone()).unwrap(),
|
||||
MyBehaviour {
|
||||
rendezvous: rendezvous::client::Behaviour::new(identity.clone()),
|
||||
ping: ping::Behaviour::new(ping::Config::new().with_interval(Duration::from_secs(1))),
|
||||
|
@ -25,7 +25,7 @@ use libp2p::identify;
|
||||
use libp2p::ping;
|
||||
use libp2p::swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p::Multiaddr;
|
||||
use libp2p::{development_transport, rendezvous};
|
||||
use libp2p::{rendezvous, tokio_development_transport};
|
||||
use std::time::Duration;
|
||||
use void::Void;
|
||||
|
||||
@ -40,8 +40,8 @@ async fn main() {
|
||||
|
||||
let identity = identity::Keypair::generate_ed25519();
|
||||
|
||||
let mut swarm = Swarm::new(
|
||||
development_transport(identity.clone()).await.unwrap(),
|
||||
let mut swarm = Swarm::with_tokio_executor(
|
||||
tokio_development_transport(identity.clone()).unwrap(),
|
||||
MyBehaviour {
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"rendezvous-example/1.0.0".to_string(),
|
||||
|
@ -24,7 +24,7 @@ use libp2p::core::PeerId;
|
||||
use libp2p::identify;
|
||||
use libp2p::ping;
|
||||
use libp2p::swarm::{keep_alive, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p::{development_transport, rendezvous};
|
||||
use libp2p::{rendezvous, tokio_development_transport};
|
||||
use void::Void;
|
||||
|
||||
/// Examples for the rendezvous protocol:
|
||||
@ -43,8 +43,8 @@ async fn main() {
|
||||
let key = identity::ed25519::SecretKey::from_bytes(bytes).expect("we always pass 32 bytes");
|
||||
let identity = identity::Keypair::Ed25519(key.into());
|
||||
|
||||
let mut swarm = Swarm::new(
|
||||
development_transport(identity.clone()).await.unwrap(),
|
||||
let mut swarm = Swarm::with_tokio_executor(
|
||||
tokio_development_transport(identity.clone()).unwrap(),
|
||||
MyBehaviour {
|
||||
identify: identify::Behaviour::new(identify::Config::new(
|
||||
"rendezvous-example/1.0.0".to_string(),
|
||||
|
@ -28,7 +28,7 @@ use libp2p::core::upgrade::SelectUpgrade;
|
||||
use libp2p::core::{identity, Multiaddr, PeerId, Transport};
|
||||
use libp2p::mplex::MplexConfig;
|
||||
use libp2p::noise::NoiseAuthenticated;
|
||||
use libp2p::swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmBuilder, SwarmEvent};
|
||||
use libp2p::swarm::{AddressScore, NetworkBehaviour, Swarm, SwarmEvent};
|
||||
use libp2p::yamux::YamuxConfig;
|
||||
use std::fmt::Debug;
|
||||
use std::time::Duration;
|
||||
@ -53,11 +53,7 @@ where
|
||||
.timeout(Duration::from_secs(5))
|
||||
.boxed();
|
||||
|
||||
SwarmBuilder::new(transport, behaviour_fn(peer_id, identity), peer_id)
|
||||
.executor(Box::new(|future| {
|
||||
let _ = tokio::spawn(future);
|
||||
}))
|
||||
.build()
|
||||
Swarm::with_tokio_executor(transport, behaviour_fn(peer_id, identity), peer_id)
|
||||
}
|
||||
|
||||
fn get_rand_memory_address() -> Multiaddr {
|
||||
|
@ -48,7 +48,7 @@ fn is_response_outbound() {
|
||||
|
||||
let (peer1_id, trans) = mk_transport();
|
||||
let ping_proto1 = RequestResponse::new(PingCodec(), protocols, cfg);
|
||||
let mut swarm1 = Swarm::new(trans, ping_proto1, peer1_id);
|
||||
let mut swarm1 = Swarm::without_executor(trans, ping_proto1, peer1_id);
|
||||
|
||||
let request_id1 = swarm1
|
||||
.behaviour_mut()
|
||||
@ -87,11 +87,11 @@ fn ping_protocol() {
|
||||
|
||||
let (peer1_id, trans) = mk_transport();
|
||||
let ping_proto1 = RequestResponse::new(PingCodec(), protocols.clone(), cfg.clone());
|
||||
let mut swarm1 = Swarm::new(trans, ping_proto1, peer1_id);
|
||||
let mut swarm1 = Swarm::without_executor(trans, ping_proto1, peer1_id);
|
||||
|
||||
let (peer2_id, trans) = mk_transport();
|
||||
let ping_proto2 = RequestResponse::new(PingCodec(), protocols, cfg);
|
||||
let mut swarm2 = Swarm::new(trans, ping_proto2, peer2_id);
|
||||
let mut swarm2 = Swarm::without_executor(trans, ping_proto2, peer2_id);
|
||||
|
||||
let (mut tx, mut rx) = mpsc::channel::<Multiaddr>(1);
|
||||
|
||||
@ -176,11 +176,11 @@ fn emits_inbound_connection_closed_failure() {
|
||||
|
||||
let (peer1_id, trans) = mk_transport();
|
||||
let ping_proto1 = RequestResponse::new(PingCodec(), protocols.clone(), cfg.clone());
|
||||
let mut swarm1 = Swarm::new(trans, ping_proto1, peer1_id);
|
||||
let mut swarm1 = Swarm::without_executor(trans, ping_proto1, peer1_id);
|
||||
|
||||
let (peer2_id, trans) = mk_transport();
|
||||
let ping_proto2 = RequestResponse::new(PingCodec(), protocols, cfg);
|
||||
let mut swarm2 = Swarm::new(trans, ping_proto2, peer2_id);
|
||||
let mut swarm2 = Swarm::without_executor(trans, ping_proto2, peer2_id);
|
||||
|
||||
let addr = "/ip4/127.0.0.1/tcp/0".parse().unwrap();
|
||||
swarm1.listen_on(addr).unwrap();
|
||||
@ -245,11 +245,11 @@ fn emits_inbound_connection_closed_if_channel_is_dropped() {
|
||||
|
||||
let (peer1_id, trans) = mk_transport();
|
||||
let ping_proto1 = RequestResponse::new(PingCodec(), protocols.clone(), cfg.clone());
|
||||
let mut swarm1 = Swarm::new(trans, ping_proto1, peer1_id);
|
||||
let mut swarm1 = Swarm::without_executor(trans, ping_proto1, peer1_id);
|
||||
|
||||
let (peer2_id, trans) = mk_transport();
|
||||
let ping_proto2 = RequestResponse::new(PingCodec(), protocols, cfg);
|
||||
let mut swarm2 = Swarm::new(trans, ping_proto2, peer2_id);
|
||||
let mut swarm2 = Swarm::without_executor(trans, ping_proto2, peer2_id);
|
||||
|
||||
let addr = "/ip4/127.0.0.1/tcp/0".parse().unwrap();
|
||||
swarm1.listen_on(addr).unwrap();
|
||||
|
Reference in New Issue
Block a user