RFC: Opening new streams via a context-aware, global API #4836
Closed
thomaseizinger
started this conversation in
Ideas
Replies: 1 comment
-
Documenting an out-of-band discussion here. I don't think this is a good idea for the following reason:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The problem
At the moment, opening a new stream and sending data on it from within a
ConnectionHandler
is somewhat tedious:ConnectionHandlerEvent::OutboundSubstreamRequest
.ConnectionHandler::poll
.ConnectionEvent
.Futures{Set,Map}
where it is polled to completion.This is a lot of state tracking that one needs to get right.
Given the linear execution flow of the above, it would be much easier (and in line with the coding guidelines) to offer a
async
-based API for requesting a new stream. The problem with such an API is that it needs to be.await
ed and nothing withinConnectionHandler
is markedasync
(on purpose).The actual execution of the protocol using the stream is however
async
so it could be added to the beginning of that. Think something like:The question is, what is
open_stream
? The function somehow needs a reference tolibp2p_swarm::Connection
, which could be achieved using a channel. Said channel would have to be passed intoNetworkBehaviour::handle_established_*_connection
such that it can be passed on to theConnectionHandler
implementation. That is somewhat tedious and a breaking change.The proposal
Instead of accessing the
Connection
explicitly, we could access it implicitly via thread-local storage. Let's remember that:Connection
runs within a task that combines the channel from the behaviour.Connection
itself interacts with theConnectionHandler
and the muxer within itspoll
function.tracing
span as part of theConnection
, we could set a thread-local storage variable at the beginning ofpoll
that provides access to a channel within theConnection
itself:open_stream
would access this TLS and thereby implicitly communicate with "its"Connection
instance, being able to e.g. queue aoneshot::Sender
into a list for pending streams. The correspondingoneshot::Receiver
would be returned fromopen_stream
.Connection
would then proceed to open a new stream via the muxer, negotiate the protocol and - if successful - send the stream into the channel, back to theConnectionHandler
.Advantages
ConnectionHandler
, instead, any function can directly create a newFuture
that opens a stream, runs the protocols and returns the result.ConnectionHandler
.#[doc(hidden)]
as we explore it within the codebase first.Disadvantages
Beta Was this translation helpful? Give feedback.
All reactions