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

feat(swarm): Make executor for connection tasks explicit #3097

Merged
merged 40 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
ce2cb5c
Initial prototype commit
umgefahren Nov 8, 2022
6cb302f
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 8, 2022
b69dc35
Update swarm/src/connection/pool.rs
umgefahren Nov 9, 2022
12e0a32
Moving tokio and async std executors
umgefahren Nov 9, 2022
ad2ad9b
Implement other changes
umgefahren Nov 9, 2022
92da6a2
Formatted
umgefahren Nov 9, 2022
e7253d1
Update swarm/src/connection/pool.rs
umgefahren Nov 10, 2022
3a24540
Update swarm/src/lib.rs
umgefahren Nov 10, 2022
98dc7ce
Implemented changes
umgefahren Nov 10, 2022
38b21d7
Merge branch 'executor-aware-swarm' of github.com:umgefahren/rust-lib…
umgefahren Nov 10, 2022
804b7d6
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 10, 2022
8fd5e56
Fix tests
umgefahren Nov 10, 2022
ee9354c
Modified CI
umgefahren Nov 10, 2022
5381a2c
Corrected some clippy issues
umgefahren Nov 10, 2022
dc5f1b7
Update core/Cargo.toml
umgefahren Nov 10, 2022
b82db0b
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 11, 2022
5365389
Implemented suggested changes
umgefahren Nov 11, 2022
50ee927
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 12, 2022
e055d2a
Apply suggestions from code review
umgefahren Nov 12, 2022
5feb2e5
Implemented requested changes
umgefahren Nov 12, 2022
e6761c6
Use new api
umgefahren Nov 13, 2022
20c5a09
Update swarm/src/lib.rs
umgefahren Nov 13, 2022
eb7836d
Update swarm/src/lib.rs
umgefahren Nov 13, 2022
361ec51
Update swarm/src/lib.rs
umgefahren Nov 13, 2022
da955ba
Implemented more ergonomic api
umgefahren Nov 13, 2022
7d8cbe3
Move executor
umgefahren Nov 13, 2022
15607ba
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 13, 2022
f0493ed
Correct bad merge
umgefahren Nov 13, 2022
91676a9
Do fmt
umgefahren Nov 13, 2022
d99b802
Added changelog entries
umgefahren Nov 13, 2022
05deb81
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 13, 2022
69fa671
Apply suggestions from code review
umgefahren Nov 13, 2022
f27fdf8
Implement small changes
umgefahren Nov 13, 2022
742f555
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 13, 2022
f61686d
Apply suggestions from code review
umgefahren Nov 14, 2022
d193da3
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 14, 2022
30c5020
Addressed Max's concerns.
umgefahren Nov 14, 2022
f4ddadd
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 15, 2022
51e98fb
Removed unnecessary inlines
umgefahren Nov 15, 2022
1edb41d
Merge branch 'master' into executor-aware-swarm
umgefahren Nov 15, 2022
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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ secp256k1 = ["libp2p-core/secp256k1"]
rsa = ["libp2p-core/rsa"]
ecdsa = ["libp2p-core/ecdsa"]
serde = ["libp2p-core/serde", "libp2p-kad?/serde", "libp2p-gossipsub?/serde"]
tokio = ["libp2p-mdns?/tokio", "libp2p-tcp?/tokio", "libp2p-dns?/tokio"]
async-std = ["libp2p-mdns?/async-io", "libp2p-tcp?/async-io", "libp2p-dns?/async-std"]
tokio = ["libp2p-mdns?/tokio", "libp2p-tcp?/tokio", "libp2p-dns?/tokio", "libp2p-swarm/tokio", "libp2p-core/tokio"]
async-std = ["libp2p-mdns?/async-io", "libp2p-tcp?/async-io", "libp2p-dns?/async-std", "libp2p-swarm/async-std", "libp2p-core/async-std"]

[dependencies]
bytes = "1"
Expand Down
11 changes: 8 additions & 3 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ unsigned-varint = "0.7"
void = "1"
zeroize = "1"
serde = { version = "1", optional = true, features = ["derive"] }
# TODO make optional before commit
tokio = { version = "1.15", features = ["rt"], optional = true }
async-std = { version = "1.6.2", optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ring = { version = "0.16.9", features = ["alloc", "std"], default-features = false, optional = true}
Expand All @@ -56,10 +59,12 @@ serde_json = "1.0"
prost-build = "0.11"

[features]
secp256k1 = [ "libsecp256k1" ]
ecdsa = [ "p256" ]
rsa = [ "dep:ring" ]
secp256k1 = ["libsecp256k1"]
ecdsa = ["p256"]
rsa = ["dep:ring"]
umgefahren marked this conversation as resolved.
Show resolved Hide resolved
serde = ["multihash/serde-codec", "dep:serde"]
tokio = ["dep:tokio"]
async-std = ["dep:async-std"]

[[bench]]
name = "peer_id"
Expand Down
40 changes: 40 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ mod peer_record_proto {
include!(concat!(env!("OUT_DIR"), "/peer_record_proto.rs"));
}

use futures::executor::ThreadPool;
/// Multi-address re-export.
pub use multiaddr;
pub type Negotiated<T> = multistream_select::Negotiated<T>;
Expand Down Expand Up @@ -102,3 +103,42 @@ impl<F: Fn(Pin<Box<dyn Future<Output = ()> + Send>>)> Executor for F {
self(f)
}
}

impl Executor for ThreadPool {
fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
self.spawn_ok(future)
}
}
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved

#[cfg(feature = "tokio")]
#[derive(Clone, Copy, Debug, Default)]
pub enum TokioExecutor<'a> {
#[default]
Empty,
Given(&'a tokio::runtime::Runtime),
umgefahren marked this conversation as resolved.
Show resolved Hide resolved
}

#[cfg(feature = "tokio")]
impl<'a> Executor for TokioExecutor<'a> {
fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
match self {
Self::Given(runtime) => {
let _ = runtime.spawn(future);
}
Self::Empty => {
let _ = tokio::spawn(future);
}
}
}
}

#[cfg(feature = "async-std")]
#[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct AsyncStdExecutor;

#[cfg(feature = "async-std")]
impl Executor for AsyncStdExecutor {
fn exec(&self, future: Pin<Box<dyn Future<Output = ()> + Send>>) {
let _ = async_std::task::spawn(future);
}
}
23 changes: 6 additions & 17 deletions examples/chat-tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use libp2p::{
TokioMdns,
},
mplex, noise,
swarm::{SwarmBuilder, SwarmEvent},
swarm::SwarmEvent,
tcp, Multiaddr, NetworkBehaviour, PeerId, Transport,
};
use std::error::Error;
Expand Down Expand Up @@ -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::TokioSwarm::new(transport, behaviour, peer_id);

// Reach out to another node if specified
if let Some(to_dial) = std::env::args().nth(1) {
Expand Down
2 changes: 1 addition & 1 deletion examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
};

behaviour.floodsub.subscribe(floodsub_topic.clone());
Swarm::new(transport, behaviour, local_peer_id)
Swarm::<_, libp2p_core::AsyncStdExecutor>::new(transport, behaviour, local_peer_id)
};

// Reach out to another node if specified
Expand Down
4 changes: 4 additions & 0 deletions swarm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ env_logger = "0.9"
libp2p = { path = "..", features = ["full"] }
quickcheck = { package = "quickcheck-ext", path = "../misc/quickcheck-ext" }

[features]
tokio = ["libp2p-core/tokio"]
async-std = ["libp2p-core/async-std"]

# Passing arguments to the docsrs builder in order to properly document cfg's.
# More information: https://docs.rs/about/builds#cross-compiling
[package.metadata.docs.rs]
Expand Down
Loading