Add libp2p-request-response protocol. (#1596)

* Add the libp2p-request-response protocol.

This crate provides a generic implementation for request/response
protocols, whereby each request is sent on a new substream.

* Fix OneShotHandler usage in floodsub.

* Custom ProtocolsHandler and multiple protocols.

  1. Implement a custom ProtocolsHandler instead of using
     the OneShotHandler for better control and error handling.
     In particular, all request/response sending/receiving is
     kept in the substreams upgrades and thus the background
     task of a connection.
  2. Support multiple protocols (usually protocol versions)
     with a single `RequestResponse` instance, with
     configurable inbound/outbound support.

* Small doc clarification.

* Remove unnecessary Sync bounds.

* Remove redundant Clone constraint.

* Update protocols/request-response/Cargo.toml

Co-authored-by: Toralf Wittner <tw@dtex.org>

* Update dev-dependencies.

* Update Cargo.tomls.

* Add changelog.

* Remove Sync bound from RequestResponseCodec::Protocol.

Apparently the compiler just needs some help with the scope
of borrows, which is unfortunate.

* Try async-trait.

* Allow checking whether a ResponseChannel is still open.

Also expand the commentary on `send_response` to indicate that
responses may be discard if they come in too late.

* Add `RequestResponse::is_pending`.

As an analogue of `ResponseChannel::is_open` for outbound requests.

* Revert now unnecessary changes to the OneShotHandler.

Since `libp2p-request-response` is no longer using it.

* Update CHANGELOG for libp2p-swarm.

Co-authored-by: Toralf Wittner <tw@dtex.org>
This commit is contained in:
Roman Borschel
2020-06-29 17:08:40 +02:00
committed by GitHub
parent 7270ed8721
commit eb8cb43508
15 changed files with 1481 additions and 72 deletions

View File

@ -140,7 +140,7 @@ pub trait ProtocolsHandler: Send + 'static {
/// Injects an event coming from the outside in the handler.
fn inject_event(&mut self, event: Self::InEvent);
/// Indicates to the handler that upgrading a substream to the given protocol has failed.
/// Indicates to the handler that upgrading an outbound substream to the given protocol has failed.
fn inject_dial_upgrade_error(
&mut self,
info: Self::OutboundOpenInfo,
@ -149,6 +149,14 @@ pub trait ProtocolsHandler: Send + 'static {
>
);
/// Indicates to the handler that upgrading an inbound substream to the given protocol has failed.
fn inject_listen_upgrade_error(
&mut self,
_: ProtocolsHandlerUpgrErr<
<Self::InboundProtocol as InboundUpgradeSend>::Error
>
) {}
/// Returns until when the connection should be kept alive.
///
/// This method is called by the `Swarm` after each invocation of
@ -236,7 +244,7 @@ pub struct SubstreamProtocol<TUpgrade> {
}
impl<TUpgrade> SubstreamProtocol<TUpgrade> {
/// Create a new `ListenProtocol` from the given upgrade.
/// Create a new `SubstreamProtocol` from the given upgrade.
///
/// The default timeout for applying the given upgrade on a substream is
/// 10 seconds.