2020-06-29 17:08:40 +02:00
|
|
|
// Copyright 2020 Parity Technologies (UK) Ltd.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
mod protocol;
|
|
|
|
|
|
|
|
use crate::codec::RequestResponseCodec;
|
2021-08-11 13:12:12 +02:00
|
|
|
use crate::{RequestId, EMPTY_QUEUE_SHRINK_THRESHOLD};
|
2020-06-29 17:08:40 +02:00
|
|
|
|
2021-08-11 13:12:12 +02:00
|
|
|
pub use protocol::{ProtocolSupport, RequestProtocol, ResponseProtocol};
|
2020-06-29 17:08:40 +02:00
|
|
|
|
2021-08-11 13:12:12 +02:00
|
|
|
use futures::{channel::oneshot, future::BoxFuture, prelude::*, stream::FuturesUnordered};
|
|
|
|
use libp2p_core::upgrade::{NegotiationError, UpgradeError};
|
2020-06-29 17:08:40 +02:00
|
|
|
use libp2p_swarm::{
|
|
|
|
protocols_handler::{
|
2021-08-11 13:12:12 +02:00
|
|
|
KeepAlive, ProtocolsHandler, ProtocolsHandlerEvent, ProtocolsHandlerUpgrErr,
|
|
|
|
},
|
|
|
|
SubstreamProtocol,
|
2020-06-29 17:08:40 +02:00
|
|
|
};
|
|
|
|
use smallvec::SmallVec;
|
|
|
|
use std::{
|
|
|
|
collections::VecDeque,
|
2021-08-11 13:12:12 +02:00
|
|
|
fmt, io,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicU64, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
task::{Context, Poll},
|
2020-06-29 17:08:40 +02:00
|
|
|
time::Duration,
|
|
|
|
};
|
|
|
|
use wasm_timer::Instant;
|
|
|
|
|
|
|
|
/// A connection handler of a `RequestResponse` protocol.
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub struct RequestResponseHandler<TCodec>
|
|
|
|
where
|
|
|
|
TCodec: RequestResponseCodec,
|
|
|
|
{
|
|
|
|
/// The supported inbound protocols.
|
|
|
|
inbound_protocols: SmallVec<[TCodec::Protocol; 2]>,
|
|
|
|
/// The request/response message codec.
|
|
|
|
codec: TCodec,
|
|
|
|
/// The keep-alive timeout of idle connections. A connection is considered
|
|
|
|
/// idle if there are no outbound substreams.
|
|
|
|
keep_alive_timeout: Duration,
|
|
|
|
/// The timeout for inbound and outbound substreams (i.e. request
|
|
|
|
/// and response processing).
|
|
|
|
substream_timeout: Duration,
|
|
|
|
/// The current connection keep-alive.
|
|
|
|
keep_alive: KeepAlive,
|
|
|
|
/// A pending fatal error that results in the connection being closed.
|
|
|
|
pending_error: Option<ProtocolsHandlerUpgrErr<io::Error>>,
|
|
|
|
/// Queue of events to emit in `poll()`.
|
|
|
|
pending_events: VecDeque<RequestResponseHandlerEvent<TCodec>>,
|
|
|
|
/// Outbound upgrades waiting to be emitted as an `OutboundSubstreamRequest`.
|
|
|
|
outbound: VecDeque<RequestProtocol<TCodec>>,
|
|
|
|
/// Inbound upgrades waiting for the incoming request.
|
2021-08-11 13:12:12 +02:00
|
|
|
inbound: FuturesUnordered<
|
|
|
|
BoxFuture<
|
|
|
|
'static,
|
|
|
|
Result<
|
|
|
|
(
|
|
|
|
(RequestId, TCodec::Request),
|
|
|
|
oneshot::Sender<TCodec::Response>,
|
|
|
|
),
|
|
|
|
oneshot::Canceled,
|
|
|
|
>,
|
|
|
|
>,
|
|
|
|
>,
|
|
|
|
inbound_request_id: Arc<AtomicU64>,
|
2020-06-29 17:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<TCodec> RequestResponseHandler<TCodec>
|
|
|
|
where
|
|
|
|
TCodec: RequestResponseCodec,
|
|
|
|
{
|
|
|
|
pub(super) fn new(
|
|
|
|
inbound_protocols: SmallVec<[TCodec::Protocol; 2]>,
|
|
|
|
codec: TCodec,
|
|
|
|
keep_alive_timeout: Duration,
|
|
|
|
substream_timeout: Duration,
|
2021-08-11 13:12:12 +02:00
|
|
|
inbound_request_id: Arc<AtomicU64>,
|
2020-06-29 17:08:40 +02:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
inbound_protocols,
|
|
|
|
codec,
|
|
|
|
keep_alive: KeepAlive::Yes,
|
|
|
|
keep_alive_timeout,
|
|
|
|
substream_timeout,
|
|
|
|
outbound: VecDeque::new(),
|
|
|
|
inbound: FuturesUnordered::new(),
|
|
|
|
pending_events: VecDeque::new(),
|
|
|
|
pending_error: None,
|
2021-08-11 13:12:12 +02:00
|
|
|
inbound_request_id,
|
2020-06-29 17:08:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The events emitted by the [`RequestResponseHandler`].
|
|
|
|
#[doc(hidden)]
|
|
|
|
pub enum RequestResponseHandlerEvent<TCodec>
|
|
|
|
where
|
2021-08-11 13:12:12 +02:00
|
|
|
TCodec: RequestResponseCodec,
|
2020-06-29 17:08:40 +02:00
|
|
|
{
|
[request-response] Refine success & error reporting for inbound requests. (#1867)
* Refine error reporting for inbound request handling.
At the moment one can neither get confirmation when a
response has been sent on the underlying transport, nor
is one aware of response omissions. The latter was
originally intended as a feature for support of
one-way protocols, which seems like a bad idea in
hindsight. The lack of notification for sent
responses may prohibit implementation of some
request-response protocols that need to ensure
a happens-before relation between sending a
response and a subsequent request, besides uses
for collecting statistics.
Even with these changes, there is no active notification
for failed inbound requests as a result of connections
unexpectedly closing, as is the case for outbound requests.
Instead, for pending inbound requests this scenario
can be identified if necessary by the absense of both
`InboundFailure` and `ResponseSent` events for a particular
previously received request. Interest in this situation is
not expected to be common and would otherwise require
explicitly tracking all inbound requests in the `RequestResponse`
behaviour, which would be a pity. `RequestResponse::send_response`
now also synchronously returns an error if the inbound upgrade
handling the request has been aborted, due to timeout or
closing of the connection, giving more options for graceful
error handling for inbound requests.
As an aside, the `Throttled` wrapper now no longer emits
inbound or outbound error events occurring in the context
of sending credit requests or responses. This is in addition
to not emitting `ResponseSent` events for ACK responses of
credit grants.
* Update protocols/request-response/src/lib.rs
Co-authored-by: Max Inden <mail@max-inden.de>
* Address some minor clippy warnings. (#1868)
* Track pending credit request IDs.
In order to avoid emitting events relating to credit grants or acks
on the public API. The public API should only emit events relating
to the actual requests and responses sent by client code.
* Small cleanup
* Cleanup
* Update versions and changelogs.
* Unreleased
Co-authored-by: Max Inden <mail@max-inden.de>
2020-12-07 13:07:47 +01:00
|
|
|
/// A request has been received.
|
2020-06-29 17:08:40 +02:00
|
|
|
Request {
|
2020-09-07 17:22:40 +02:00
|
|
|
request_id: RequestId,
|
2020-06-29 17:08:40 +02:00
|
|
|
request: TCodec::Request,
|
2021-08-11 13:12:12 +02:00
|
|
|
sender: oneshot::Sender<TCodec::Response>,
|
2020-06-29 17:08:40 +02:00
|
|
|
},
|
[request-response] Refine success & error reporting for inbound requests. (#1867)
* Refine error reporting for inbound request handling.
At the moment one can neither get confirmation when a
response has been sent on the underlying transport, nor
is one aware of response omissions. The latter was
originally intended as a feature for support of
one-way protocols, which seems like a bad idea in
hindsight. The lack of notification for sent
responses may prohibit implementation of some
request-response protocols that need to ensure
a happens-before relation between sending a
response and a subsequent request, besides uses
for collecting statistics.
Even with these changes, there is no active notification
for failed inbound requests as a result of connections
unexpectedly closing, as is the case for outbound requests.
Instead, for pending inbound requests this scenario
can be identified if necessary by the absense of both
`InboundFailure` and `ResponseSent` events for a particular
previously received request. Interest in this situation is
not expected to be common and would otherwise require
explicitly tracking all inbound requests in the `RequestResponse`
behaviour, which would be a pity. `RequestResponse::send_response`
now also synchronously returns an error if the inbound upgrade
handling the request has been aborted, due to timeout or
closing of the connection, giving more options for graceful
error handling for inbound requests.
As an aside, the `Throttled` wrapper now no longer emits
inbound or outbound error events occurring in the context
of sending credit requests or responses. This is in addition
to not emitting `ResponseSent` events for ACK responses of
credit grants.
* Update protocols/request-response/src/lib.rs
Co-authored-by: Max Inden <mail@max-inden.de>
* Address some minor clippy warnings. (#1868)
* Track pending credit request IDs.
In order to avoid emitting events relating to credit grants or acks
on the public API. The public API should only emit events relating
to the actual requests and responses sent by client code.
* Small cleanup
* Cleanup
* Update versions and changelogs.
* Unreleased
Co-authored-by: Max Inden <mail@max-inden.de>
2020-12-07 13:07:47 +01:00
|
|
|
/// A response has been received.
|
2020-06-29 17:08:40 +02:00
|
|
|
Response {
|
|
|
|
request_id: RequestId,
|
2021-08-11 13:12:12 +02:00
|
|
|
response: TCodec::Response,
|
2020-06-29 17:08:40 +02:00
|
|
|
},
|
[request-response] Refine success & error reporting for inbound requests. (#1867)
* Refine error reporting for inbound request handling.
At the moment one can neither get confirmation when a
response has been sent on the underlying transport, nor
is one aware of response omissions. The latter was
originally intended as a feature for support of
one-way protocols, which seems like a bad idea in
hindsight. The lack of notification for sent
responses may prohibit implementation of some
request-response protocols that need to ensure
a happens-before relation between sending a
response and a subsequent request, besides uses
for collecting statistics.
Even with these changes, there is no active notification
for failed inbound requests as a result of connections
unexpectedly closing, as is the case for outbound requests.
Instead, for pending inbound requests this scenario
can be identified if necessary by the absense of both
`InboundFailure` and `ResponseSent` events for a particular
previously received request. Interest in this situation is
not expected to be common and would otherwise require
explicitly tracking all inbound requests in the `RequestResponse`
behaviour, which would be a pity. `RequestResponse::send_response`
now also synchronously returns an error if the inbound upgrade
handling the request has been aborted, due to timeout or
closing of the connection, giving more options for graceful
error handling for inbound requests.
As an aside, the `Throttled` wrapper now no longer emits
inbound or outbound error events occurring in the context
of sending credit requests or responses. This is in addition
to not emitting `ResponseSent` events for ACK responses of
credit grants.
* Update protocols/request-response/src/lib.rs
Co-authored-by: Max Inden <mail@max-inden.de>
* Address some minor clippy warnings. (#1868)
* Track pending credit request IDs.
In order to avoid emitting events relating to credit grants or acks
on the public API. The public API should only emit events relating
to the actual requests and responses sent by client code.
* Small cleanup
* Cleanup
* Update versions and changelogs.
* Unreleased
Co-authored-by: Max Inden <mail@max-inden.de>
2020-12-07 13:07:47 +01:00
|
|
|
/// A response to an inbound request has been sent.
|
|
|
|
ResponseSent(RequestId),
|
|
|
|
/// A response to an inbound request was omitted as a result
|
|
|
|
/// of dropping the response `sender` of an inbound `Request`.
|
|
|
|
ResponseOmission(RequestId),
|
|
|
|
/// An outbound request timed out while sending the request
|
|
|
|
/// or waiting for the response.
|
2020-06-29 17:08:40 +02:00
|
|
|
OutboundTimeout(RequestId),
|
|
|
|
/// An outbound request failed to negotiate a mutually supported protocol.
|
|
|
|
OutboundUnsupportedProtocols(RequestId),
|
[request-response] Refine success & error reporting for inbound requests. (#1867)
* Refine error reporting for inbound request handling.
At the moment one can neither get confirmation when a
response has been sent on the underlying transport, nor
is one aware of response omissions. The latter was
originally intended as a feature for support of
one-way protocols, which seems like a bad idea in
hindsight. The lack of notification for sent
responses may prohibit implementation of some
request-response protocols that need to ensure
a happens-before relation between sending a
response and a subsequent request, besides uses
for collecting statistics.
Even with these changes, there is no active notification
for failed inbound requests as a result of connections
unexpectedly closing, as is the case for outbound requests.
Instead, for pending inbound requests this scenario
can be identified if necessary by the absense of both
`InboundFailure` and `ResponseSent` events for a particular
previously received request. Interest in this situation is
not expected to be common and would otherwise require
explicitly tracking all inbound requests in the `RequestResponse`
behaviour, which would be a pity. `RequestResponse::send_response`
now also synchronously returns an error if the inbound upgrade
handling the request has been aborted, due to timeout or
closing of the connection, giving more options for graceful
error handling for inbound requests.
As an aside, the `Throttled` wrapper now no longer emits
inbound or outbound error events occurring in the context
of sending credit requests or responses. This is in addition
to not emitting `ResponseSent` events for ACK responses of
credit grants.
* Update protocols/request-response/src/lib.rs
Co-authored-by: Max Inden <mail@max-inden.de>
* Address some minor clippy warnings. (#1868)
* Track pending credit request IDs.
In order to avoid emitting events relating to credit grants or acks
on the public API. The public API should only emit events relating
to the actual requests and responses sent by client code.
* Small cleanup
* Cleanup
* Update versions and changelogs.
* Unreleased
Co-authored-by: Max Inden <mail@max-inden.de>
2020-12-07 13:07:47 +01:00
|
|
|
/// An inbound request timed out while waiting for the request
|
|
|
|
/// or sending the response.
|
2020-09-07 17:22:40 +02:00
|
|
|
InboundTimeout(RequestId),
|
2020-06-29 17:08:40 +02:00
|
|
|
/// An inbound request failed to negotiate a mutually supported protocol.
|
2020-09-07 17:22:40 +02:00
|
|
|
InboundUnsupportedProtocols(RequestId),
|
2020-06-29 17:08:40 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 12:41:28 +02:00
|
|
|
impl<TCodec: RequestResponseCodec> fmt::Debug for RequestResponseHandlerEvent<TCodec> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
2021-08-11 13:12:12 +02:00
|
|
|
RequestResponseHandlerEvent::Request {
|
|
|
|
request_id,
|
|
|
|
request: _,
|
|
|
|
sender: _,
|
|
|
|
} => f
|
|
|
|
.debug_struct("RequestResponseHandlerEvent::Request")
|
2021-08-11 12:41:28 +02:00
|
|
|
.field("request_id", request_id)
|
|
|
|
.finish(),
|
2021-08-11 13:12:12 +02:00
|
|
|
RequestResponseHandlerEvent::Response {
|
|
|
|
request_id,
|
|
|
|
response: _,
|
|
|
|
} => f
|
|
|
|
.debug_struct("RequestResponseHandlerEvent::Response")
|
2021-08-11 12:41:28 +02:00
|
|
|
.field("request_id", request_id)
|
|
|
|
.finish(),
|
2021-08-11 13:12:12 +02:00
|
|
|
RequestResponseHandlerEvent::ResponseSent(request_id) => f
|
|
|
|
.debug_tuple("RequestResponseHandlerEvent::ResponseSent")
|
2021-08-11 12:41:28 +02:00
|
|
|
.field(request_id)
|
|
|
|
.finish(),
|
2021-08-11 13:12:12 +02:00
|
|
|
RequestResponseHandlerEvent::ResponseOmission(request_id) => f
|
|
|
|
.debug_tuple("RequestResponseHandlerEvent::ResponseOmission")
|
2021-08-11 12:41:28 +02:00
|
|
|
.field(request_id)
|
|
|
|
.finish(),
|
2021-08-11 13:12:12 +02:00
|
|
|
RequestResponseHandlerEvent::OutboundTimeout(request_id) => f
|
|
|
|
.debug_tuple("RequestResponseHandlerEvent::OutboundTimeout")
|
2021-08-11 12:41:28 +02:00
|
|
|
.field(request_id)
|
|
|
|
.finish(),
|
2021-08-11 13:12:12 +02:00
|
|
|
RequestResponseHandlerEvent::OutboundUnsupportedProtocols(request_id) => f
|
|
|
|
.debug_tuple("RequestResponseHandlerEvent::OutboundUnsupportedProtocols")
|
2021-08-11 12:41:28 +02:00
|
|
|
.field(request_id)
|
|
|
|
.finish(),
|
2021-08-11 13:12:12 +02:00
|
|
|
RequestResponseHandlerEvent::InboundTimeout(request_id) => f
|
|
|
|
.debug_tuple("RequestResponseHandlerEvent::InboundTimeout")
|
2021-08-11 12:41:28 +02:00
|
|
|
.field(request_id)
|
|
|
|
.finish(),
|
2021-08-11 13:12:12 +02:00
|
|
|
RequestResponseHandlerEvent::InboundUnsupportedProtocols(request_id) => f
|
|
|
|
.debug_tuple("RequestResponseHandlerEvent::InboundUnsupportedProtocols")
|
2021-08-11 12:41:28 +02:00
|
|
|
.field(request_id)
|
|
|
|
.finish(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:08:40 +02:00
|
|
|
impl<TCodec> ProtocolsHandler for RequestResponseHandler<TCodec>
|
|
|
|
where
|
|
|
|
TCodec: RequestResponseCodec + Send + Clone + 'static,
|
|
|
|
{
|
|
|
|
type InEvent = RequestProtocol<TCodec>;
|
|
|
|
type OutEvent = RequestResponseHandlerEvent<TCodec>;
|
|
|
|
type Error = ProtocolsHandlerUpgrErr<io::Error>;
|
|
|
|
type InboundProtocol = ResponseProtocol<TCodec>;
|
|
|
|
type OutboundProtocol = RequestProtocol<TCodec>;
|
|
|
|
type OutboundOpenInfo = RequestId;
|
2020-09-07 17:22:40 +02:00
|
|
|
type InboundOpenInfo = RequestId;
|
2020-06-29 17:08:40 +02:00
|
|
|
|
2020-08-23 16:57:20 +02:00
|
|
|
fn listen_protocol(&self) -> SubstreamProtocol<Self::InboundProtocol, Self::InboundOpenInfo> {
|
2020-06-29 17:08:40 +02:00
|
|
|
// A channel for notifying the handler when the inbound
|
|
|
|
// upgrade received the request.
|
|
|
|
let (rq_send, rq_recv) = oneshot::channel();
|
|
|
|
|
|
|
|
// A channel for notifying the inbound upgrade when the
|
|
|
|
// response is sent.
|
|
|
|
let (rs_send, rs_recv) = oneshot::channel();
|
|
|
|
|
2020-09-07 17:22:40 +02:00
|
|
|
let request_id = RequestId(self.inbound_request_id.fetch_add(1, Ordering::Relaxed));
|
|
|
|
|
2020-06-29 17:08:40 +02:00
|
|
|
// By keeping all I/O inside the `ResponseProtocol` and thus the
|
|
|
|
// inbound substream upgrade via above channels, we ensure that it
|
|
|
|
// is all subject to the configured timeout without extra bookkeeping
|
|
|
|
// for inbound substreams as well as their timeouts and also make the
|
|
|
|
// implementation of inbound and outbound upgrades symmetric in
|
|
|
|
// this sense.
|
|
|
|
let proto = ResponseProtocol {
|
|
|
|
protocols: self.inbound_protocols.clone(),
|
|
|
|
codec: self.codec.clone(),
|
|
|
|
request_sender: rq_send,
|
|
|
|
response_receiver: rs_recv,
|
2021-08-11 13:12:12 +02:00
|
|
|
request_id,
|
2020-06-29 17:08:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// The handler waits for the request to come in. It then emits
|
|
|
|
// `RequestResponseHandlerEvent::Request` together with a
|
|
|
|
// `ResponseChannel`.
|
2021-08-11 13:12:12 +02:00
|
|
|
self.inbound
|
|
|
|
.push(rq_recv.map_ok(move |rq| (rq, rs_send)).boxed());
|
2020-06-29 17:08:40 +02:00
|
|
|
|
2020-09-07 17:22:40 +02:00
|
|
|
SubstreamProtocol::new(proto, request_id).with_timeout(self.substream_timeout)
|
2020-06-29 17:08:40 +02:00
|
|
|
}
|
|
|
|
|
2021-08-11 13:12:12 +02:00
|
|
|
fn inject_fully_negotiated_inbound(&mut self, sent: bool, request_id: RequestId) {
|
[request-response] Refine success & error reporting for inbound requests. (#1867)
* Refine error reporting for inbound request handling.
At the moment one can neither get confirmation when a
response has been sent on the underlying transport, nor
is one aware of response omissions. The latter was
originally intended as a feature for support of
one-way protocols, which seems like a bad idea in
hindsight. The lack of notification for sent
responses may prohibit implementation of some
request-response protocols that need to ensure
a happens-before relation between sending a
response and a subsequent request, besides uses
for collecting statistics.
Even with these changes, there is no active notification
for failed inbound requests as a result of connections
unexpectedly closing, as is the case for outbound requests.
Instead, for pending inbound requests this scenario
can be identified if necessary by the absense of both
`InboundFailure` and `ResponseSent` events for a particular
previously received request. Interest in this situation is
not expected to be common and would otherwise require
explicitly tracking all inbound requests in the `RequestResponse`
behaviour, which would be a pity. `RequestResponse::send_response`
now also synchronously returns an error if the inbound upgrade
handling the request has been aborted, due to timeout or
closing of the connection, giving more options for graceful
error handling for inbound requests.
As an aside, the `Throttled` wrapper now no longer emits
inbound or outbound error events occurring in the context
of sending credit requests or responses. This is in addition
to not emitting `ResponseSent` events for ACK responses of
credit grants.
* Update protocols/request-response/src/lib.rs
Co-authored-by: Max Inden <mail@max-inden.de>
* Address some minor clippy warnings. (#1868)
* Track pending credit request IDs.
In order to avoid emitting events relating to credit grants or acks
on the public API. The public API should only emit events relating
to the actual requests and responses sent by client code.
* Small cleanup
* Cleanup
* Update versions and changelogs.
* Unreleased
Co-authored-by: Max Inden <mail@max-inden.de>
2020-12-07 13:07:47 +01:00
|
|
|
if sent {
|
2021-08-11 13:12:12 +02:00
|
|
|
self.pending_events
|
|
|
|
.push_back(RequestResponseHandlerEvent::ResponseSent(request_id))
|
[request-response] Refine success & error reporting for inbound requests. (#1867)
* Refine error reporting for inbound request handling.
At the moment one can neither get confirmation when a
response has been sent on the underlying transport, nor
is one aware of response omissions. The latter was
originally intended as a feature for support of
one-way protocols, which seems like a bad idea in
hindsight. The lack of notification for sent
responses may prohibit implementation of some
request-response protocols that need to ensure
a happens-before relation between sending a
response and a subsequent request, besides uses
for collecting statistics.
Even with these changes, there is no active notification
for failed inbound requests as a result of connections
unexpectedly closing, as is the case for outbound requests.
Instead, for pending inbound requests this scenario
can be identified if necessary by the absense of both
`InboundFailure` and `ResponseSent` events for a particular
previously received request. Interest in this situation is
not expected to be common and would otherwise require
explicitly tracking all inbound requests in the `RequestResponse`
behaviour, which would be a pity. `RequestResponse::send_response`
now also synchronously returns an error if the inbound upgrade
handling the request has been aborted, due to timeout or
closing of the connection, giving more options for graceful
error handling for inbound requests.
As an aside, the `Throttled` wrapper now no longer emits
inbound or outbound error events occurring in the context
of sending credit requests or responses. This is in addition
to not emitting `ResponseSent` events for ACK responses of
credit grants.
* Update protocols/request-response/src/lib.rs
Co-authored-by: Max Inden <mail@max-inden.de>
* Address some minor clippy warnings. (#1868)
* Track pending credit request IDs.
In order to avoid emitting events relating to credit grants or acks
on the public API. The public API should only emit events relating
to the actual requests and responses sent by client code.
* Small cleanup
* Cleanup
* Update versions and changelogs.
* Unreleased
Co-authored-by: Max Inden <mail@max-inden.de>
2020-12-07 13:07:47 +01:00
|
|
|
} else {
|
2021-08-11 13:12:12 +02:00
|
|
|
self.pending_events
|
|
|
|
.push_back(RequestResponseHandlerEvent::ResponseOmission(request_id))
|
[request-response] Refine success & error reporting for inbound requests. (#1867)
* Refine error reporting for inbound request handling.
At the moment one can neither get confirmation when a
response has been sent on the underlying transport, nor
is one aware of response omissions. The latter was
originally intended as a feature for support of
one-way protocols, which seems like a bad idea in
hindsight. The lack of notification for sent
responses may prohibit implementation of some
request-response protocols that need to ensure
a happens-before relation between sending a
response and a subsequent request, besides uses
for collecting statistics.
Even with these changes, there is no active notification
for failed inbound requests as a result of connections
unexpectedly closing, as is the case for outbound requests.
Instead, for pending inbound requests this scenario
can be identified if necessary by the absense of both
`InboundFailure` and `ResponseSent` events for a particular
previously received request. Interest in this situation is
not expected to be common and would otherwise require
explicitly tracking all inbound requests in the `RequestResponse`
behaviour, which would be a pity. `RequestResponse::send_response`
now also synchronously returns an error if the inbound upgrade
handling the request has been aborted, due to timeout or
closing of the connection, giving more options for graceful
error handling for inbound requests.
As an aside, the `Throttled` wrapper now no longer emits
inbound or outbound error events occurring in the context
of sending credit requests or responses. This is in addition
to not emitting `ResponseSent` events for ACK responses of
credit grants.
* Update protocols/request-response/src/lib.rs
Co-authored-by: Max Inden <mail@max-inden.de>
* Address some minor clippy warnings. (#1868)
* Track pending credit request IDs.
In order to avoid emitting events relating to credit grants or acks
on the public API. The public API should only emit events relating
to the actual requests and responses sent by client code.
* Small cleanup
* Cleanup
* Update versions and changelogs.
* Unreleased
Co-authored-by: Max Inden <mail@max-inden.de>
2020-12-07 13:07:47 +01:00
|
|
|
}
|
2020-06-29 17:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_fully_negotiated_outbound(
|
|
|
|
&mut self,
|
|
|
|
response: TCodec::Response,
|
|
|
|
request_id: RequestId,
|
|
|
|
) {
|
2021-08-11 13:12:12 +02:00
|
|
|
self.pending_events
|
|
|
|
.push_back(RequestResponseHandlerEvent::Response {
|
|
|
|
request_id,
|
|
|
|
response,
|
2020-06-29 17:08:40 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_event(&mut self, request: Self::InEvent) {
|
|
|
|
self.keep_alive = KeepAlive::Yes;
|
|
|
|
self.outbound.push_back(request);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_dial_upgrade_error(
|
|
|
|
&mut self,
|
|
|
|
info: RequestId,
|
|
|
|
error: ProtocolsHandlerUpgrErr<io::Error>,
|
|
|
|
) {
|
|
|
|
match error {
|
|
|
|
ProtocolsHandlerUpgrErr::Timeout => {
|
2021-08-11 13:12:12 +02:00
|
|
|
self.pending_events
|
|
|
|
.push_back(RequestResponseHandlerEvent::OutboundTimeout(info));
|
2020-06-29 17:08:40 +02:00
|
|
|
}
|
|
|
|
ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => {
|
|
|
|
// The remote merely doesn't support the protocol(s) we requested.
|
|
|
|
// This is no reason to close the connection, which may
|
|
|
|
// successfully communicate with other protocols already.
|
|
|
|
// An event is reported to permit user code to react to the fact that
|
|
|
|
// the remote peer does not support the requested protocol(s).
|
|
|
|
self.pending_events.push_back(
|
2021-08-11 13:12:12 +02:00
|
|
|
RequestResponseHandlerEvent::OutboundUnsupportedProtocols(info),
|
|
|
|
);
|
2020-06-29 17:08:40 +02:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Anything else is considered a fatal error or misbehaviour of
|
|
|
|
// the remote peer and results in closing the connection.
|
|
|
|
self.pending_error = Some(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn inject_listen_upgrade_error(
|
|
|
|
&mut self,
|
2020-09-07 17:22:40 +02:00
|
|
|
info: RequestId,
|
2021-08-11 13:12:12 +02:00
|
|
|
error: ProtocolsHandlerUpgrErr<io::Error>,
|
2020-06-29 17:08:40 +02:00
|
|
|
) {
|
|
|
|
match error {
|
2021-08-11 13:12:12 +02:00
|
|
|
ProtocolsHandlerUpgrErr::Timeout => self
|
|
|
|
.pending_events
|
|
|
|
.push_back(RequestResponseHandlerEvent::InboundTimeout(info)),
|
2020-06-29 17:08:40 +02:00
|
|
|
ProtocolsHandlerUpgrErr::Upgrade(UpgradeError::Select(NegotiationError::Failed)) => {
|
|
|
|
// The local peer merely doesn't support the protocol(s) requested.
|
|
|
|
// This is no reason to close the connection, which may
|
|
|
|
// successfully communicate with other protocols already.
|
|
|
|
// An event is reported to permit user code to react to the fact that
|
|
|
|
// the local peer does not support the requested protocol(s).
|
|
|
|
self.pending_events.push_back(
|
2021-08-11 13:12:12 +02:00
|
|
|
RequestResponseHandlerEvent::InboundUnsupportedProtocols(info),
|
|
|
|
);
|
2020-06-29 17:08:40 +02:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
// Anything else is considered a fatal error or misbehaviour of
|
|
|
|
// the remote peer and results in closing the connection.
|
|
|
|
self.pending_error = Some(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn connection_keep_alive(&self) -> KeepAlive {
|
|
|
|
self.keep_alive
|
|
|
|
}
|
|
|
|
|
|
|
|
fn poll(
|
|
|
|
&mut self,
|
2020-07-27 20:27:33 +00:00
|
|
|
cx: &mut Context<'_>,
|
2021-08-11 13:12:12 +02:00
|
|
|
) -> Poll<ProtocolsHandlerEvent<RequestProtocol<TCodec>, RequestId, Self::OutEvent, Self::Error>>
|
|
|
|
{
|
2020-06-29 17:08:40 +02:00
|
|
|
// Check for a pending (fatal) error.
|
|
|
|
if let Some(err) = self.pending_error.take() {
|
|
|
|
// The handler will not be polled again by the `Swarm`.
|
2021-08-11 13:12:12 +02:00
|
|
|
return Poll::Ready(ProtocolsHandlerEvent::Close(err));
|
2020-06-29 17:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Drain pending events.
|
|
|
|
if let Some(event) = self.pending_events.pop_front() {
|
2021-08-11 13:12:12 +02:00
|
|
|
return Poll::Ready(ProtocolsHandlerEvent::Custom(event));
|
2020-06-29 17:08:40 +02:00
|
|
|
} else if self.pending_events.capacity() > EMPTY_QUEUE_SHRINK_THRESHOLD {
|
|
|
|
self.pending_events.shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for inbound requests.
|
|
|
|
while let Poll::Ready(Some(result)) = self.inbound.poll_next_unpin(cx) {
|
|
|
|
match result {
|
2020-09-07 17:22:40 +02:00
|
|
|
Ok(((id, rq), rs_sender)) => {
|
2020-06-29 17:08:40 +02:00
|
|
|
// We received an inbound request.
|
|
|
|
self.keep_alive = KeepAlive::Yes;
|
|
|
|
return Poll::Ready(ProtocolsHandlerEvent::Custom(
|
|
|
|
RequestResponseHandlerEvent::Request {
|
2021-08-11 13:12:12 +02:00
|
|
|
request_id: id,
|
|
|
|
request: rq,
|
|
|
|
sender: rs_sender,
|
|
|
|
},
|
|
|
|
));
|
2020-06-29 17:08:40 +02:00
|
|
|
}
|
|
|
|
Err(oneshot::Canceled) => {
|
|
|
|
// The inbound upgrade has errored or timed out reading
|
|
|
|
// or waiting for the request. The handler is informed
|
|
|
|
// via `inject_listen_upgrade_error`.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit outbound requests.
|
|
|
|
if let Some(request) = self.outbound.pop_front() {
|
|
|
|
let info = request.request_id;
|
2021-08-11 13:12:12 +02:00
|
|
|
return Poll::Ready(ProtocolsHandlerEvent::OutboundSubstreamRequest {
|
|
|
|
protocol: SubstreamProtocol::new(request, info)
|
|
|
|
.with_timeout(self.substream_timeout),
|
|
|
|
});
|
2020-06-29 17:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
debug_assert!(self.outbound.is_empty());
|
|
|
|
|
|
|
|
if self.outbound.capacity() > EMPTY_QUEUE_SHRINK_THRESHOLD {
|
|
|
|
self.outbound.shrink_to_fit();
|
|
|
|
}
|
|
|
|
|
2020-08-13 13:13:23 +02:00
|
|
|
if self.inbound.is_empty() && self.keep_alive.is_yes() {
|
2020-06-29 17:08:40 +02:00
|
|
|
// No new inbound or outbound requests. However, we may just have
|
|
|
|
// started the latest inbound or outbound upgrade(s), so make sure
|
|
|
|
// the keep-alive timeout is preceded by the substream timeout.
|
|
|
|
let until = Instant::now() + self.substream_timeout + self.keep_alive_timeout;
|
|
|
|
self.keep_alive = KeepAlive::Until(until);
|
|
|
|
}
|
|
|
|
|
|
|
|
Poll::Pending
|
|
|
|
}
|
|
|
|
}
|