Skip to content

Add a request-response NetworkBehaviour helper #1562

@tomaka

Description

@tomaka

Writing a simple "request-response" protocol (where each request opens a new substream) is way too complicated at the moment, despite the OneShotHandler.

Instead, in my opinion there should be some sort of helper structs.

Here is an API sketch:

impl<T: OutboundUpgrade> Request<T> {
    pub fn new(protocol: T) -> Self { ... }
    pub fn start_request(&mut self, destination: &PeerId) -> Result<..., ...>;
    pub fn in_progress(&self) -> impl Iterator<Item = InProgress> { ... }
}
impl<T: OutboundUpgrade> NetworkBehaviour for Request<T> { ... }

impl<T: InboundUpgrade> Respond<T> {
    pub fn pending(&self) -> impl Iterator<Item = Pending> { ... }
}
impl<T: InboundUpgrade> NetworkBehaviour for Respond<T> { ... }

/// `Pending` borrows an entry within the `Respond`
impl<'a> Pending<'a> {
    pub fn source(&self) -> &PeerId { ... }
    pub fn request(&self) -> &... { ... }
    pub fn respond(self, response: ...) { ... }
}

Thanks to this, it would be possible to write just an implementation of OutboundUpgrade and InboundUpgrade.

This could also be combined with libp2p_core::upgrade::from_fn to create protocols in one line of code, provided Rust gets existential types.

Related: #1530

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions