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
Uh oh!
There was an error while loading. Please reload this page.
-
The problem
At the moment, opening a new stream and sending data on it from within a
ConnectionHandleris 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.awaited and nothing withinConnectionHandleris markedasync(on purpose).The actual execution of the protocol using the stream is however
asyncso 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_*_connectionsuch that it can be passed on to theConnectionHandlerimplementation. That is somewhat tedious and a breaking change.The proposal
Instead of accessing the
Connectionexplicitly, we could access it implicitly via thread-local storage. Let's remember that:Connectionruns within a task that combines the channel from the behaviour.Connectionitself interacts with theConnectionHandlerand the muxer within itspollfunction.tracingspan as part of theConnection, we could set a thread-local storage variable at the beginning ofpollthat provides access to a channel within theConnectionitself:open_streamwould access this TLS and thereby implicitly communicate with "its"Connectioninstance, being able to e.g. queue aoneshot::Senderinto a list for pending streams. The correspondingoneshot::Receiverwould be returned fromopen_stream.Connectionwould 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 newFuturethat 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