-
Notifications
You must be signed in to change notification settings - Fork 999
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
swarm: Make API for creating a new Swarm
executor aware
#3068
Comments
I'm currently working on a PR to solve this issue. However, I have a small question: Solving this could remove the need for local spawns since FuturesUnordered could also be treated as an executor. I don't see any usages of local spawns besides being a fallback. |
It doesn't quite work unfortunately. |
I see, however, if there is an executor, there is no need for local spawns, right? So I could just construct an enum encapsulating the different cases. |
That is correct. It is either one or the other :) |
This issue is closed by #3097. Consider linking it. |
Done! |
Previously, the executor for connection tasks silently defaulted to a `futures::executor::ThreadPool`. This causes issues such as #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.
Previously, the executor for connection tasks silently defaulted to a `futures::executor::ThreadPool`. This causes issues such as libp2p#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 libp2p#3068.
Description
This issue is extracted out of #2173 and proposes a solution to issues like #2230.
A
Swarm
needs to execute background tasks. Currently, each physical connection (i.e. a TCP connection) is executed on its own background task. This improves latency because it means the main event loop of theSwarm
which also callsNetworkBehaviour::poll
does not get blocked by IO. Also see #2885.Today, the way a
Swarm
achieves this is very subtle.By default,
Swarm::new
will try to create afutures::executor::Threadpool
to execute these connection background tasks. For async-IO types from thetokio
runtime, this does not work because they require a tokio reactor to run on the same thread which by default is only active on a worker thread of a tokioRuntime
. To achieve this, a user has to use theSwarmBuilder
instead and callexecutor(Box::new(|f| { tokio::spawn(f) }))
. See for example here:rust-libp2p/examples/chat-tokio.rs
Lines 109 to 115 in b42f286
In case creation of the
futures::executor::Threadpool
fails, the connection tasks are polled on the current thread instead:rust-libp2p/swarm/src/connection/pool.rs
Lines 398 to 404 in b42f286
rust-libp2p/swarm/src/connection/pool.rs
Lines 101 to 103 in b42f286
I am proposed to change the API for
Swarm
andSwarmBuilder
to force the user to make a decision on which executor they'd like their tasks to be executed on. There are several options and it is not yet clear, what the best one is:Swarm
that has a trait-bound for spawning new tasks. This would allow us to re-export type-aliases likelibp2p::swarm::tokio::Swarm
which would point to something likelibp2p::swarm::Swarm<TokioRuntime>
. A type-parameter-only solution is likely the easiest to migrate too because it doesn't require any signature changes. We can deprecate the current type in favor oflibp2p::swarm::futures_threadpool::Swarm
.libp2p::swarm::Swarm::new
that requires to pass an executor-specific type:There are likely other solutions too. Whatever we choose, it must play nicely with cargo features, i.e. be completely additive in terms of APIs and not change any behaviour.
Motivation
rust-libp2p
correctly.Open questions
Are you planning to do it yourself in a pull request?
Maybe.
The text was updated successfully, but these errors were encountered: