2018-06-25 15:12:39 +02:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2019-03-19 12:45:57 +01:00
|
|
|
//! Transports with timeouts on the connection setup.
|
2018-06-25 15:12:39 +02:00
|
|
|
//!
|
2019-03-19 12:45:57 +01:00
|
|
|
//! The connection setup includes all protocol upgrades applied on the
|
|
|
|
//! underlying `Transport`.
|
2018-06-25 15:12:39 +02:00
|
|
|
// TODO: add example
|
|
|
|
|
2019-04-10 10:29:21 +02:00
|
|
|
use crate::{Multiaddr, Transport, transport::{TransportError, ListenerEvent}};
|
2019-09-16 11:08:44 +02:00
|
|
|
use futures::prelude::*;
|
|
|
|
use futures_timer::Delay;
|
|
|
|
use std::{error, fmt, io, pin::Pin, task::Context, task::Poll, time::Duration};
|
2018-06-25 15:12:39 +02:00
|
|
|
|
2019-03-19 12:45:57 +01:00
|
|
|
/// A `TransportTimeout` is a `Transport` that wraps another `Transport` and adds
|
|
|
|
/// timeouts to all inbound and outbound connection attempts.
|
2018-06-25 15:12:39 +02:00
|
|
|
///
|
2019-03-19 12:45:57 +01:00
|
|
|
/// **Note**: `listen_on` is never subject to a timeout, only the setup of each
|
|
|
|
/// individual accepted connection.
|
2018-06-25 15:12:39 +02:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub struct TransportTimeout<InnerTrans> {
|
|
|
|
inner: InnerTrans,
|
|
|
|
outgoing_timeout: Duration,
|
|
|
|
incoming_timeout: Duration,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<InnerTrans> TransportTimeout<InnerTrans> {
|
|
|
|
/// Wraps around a `Transport` to add timeouts to all the sockets created by it.
|
|
|
|
pub fn new(trans: InnerTrans, timeout: Duration) -> Self {
|
|
|
|
TransportTimeout {
|
|
|
|
inner: trans,
|
|
|
|
outgoing_timeout: timeout,
|
|
|
|
incoming_timeout: timeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps around a `Transport` to add timeouts to the outgoing connections.
|
|
|
|
pub fn with_outgoing_timeout(trans: InnerTrans, timeout: Duration) -> Self {
|
|
|
|
TransportTimeout {
|
|
|
|
inner: trans,
|
|
|
|
outgoing_timeout: timeout,
|
2018-07-11 11:14:40 +02:00
|
|
|
incoming_timeout: Duration::from_secs(100 * 365 * 24 * 3600), // 100 years
|
2018-06-25 15:12:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wraps around a `Transport` to add timeouts to the ingoing connections.
|
|
|
|
pub fn with_ingoing_timeout(trans: InnerTrans, timeout: Duration) -> Self {
|
|
|
|
TransportTimeout {
|
|
|
|
inner: trans,
|
2018-07-11 11:14:40 +02:00
|
|
|
outgoing_timeout: Duration::from_secs(100 * 365 * 24 * 3600), // 100 years
|
2018-06-25 15:12:39 +02:00
|
|
|
incoming_timeout: timeout,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<InnerTrans> Transport for TransportTimeout<InnerTrans>
|
2018-07-11 11:14:40 +02:00
|
|
|
where
|
|
|
|
InnerTrans: Transport,
|
2019-01-10 11:27:06 +01:00
|
|
|
InnerTrans::Error: 'static,
|
2018-06-25 15:12:39 +02:00
|
|
|
{
|
|
|
|
type Output = InnerTrans::Output;
|
2019-01-10 11:27:06 +01:00
|
|
|
type Error = TransportTimeoutError<InnerTrans::Error>;
|
2018-06-25 15:12:39 +02:00
|
|
|
type Listener = TimeoutListener<InnerTrans::Listener>;
|
2019-09-16 11:08:44 +02:00
|
|
|
type ListenerUpgrade = Timeout<InnerTrans::ListenerUpgrade>;
|
|
|
|
type Dial = Timeout<InnerTrans::Dial>;
|
2018-06-25 15:12:39 +02:00
|
|
|
|
2019-04-10 10:29:21 +02:00
|
|
|
fn listen_on(self, addr: Multiaddr) -> Result<Self::Listener, TransportError<Self::Error>> {
|
|
|
|
let listener = self.inner.listen_on(addr)
|
2019-01-10 11:27:06 +01:00
|
|
|
.map_err(|err| err.map(TransportTimeoutError::Other))?;
|
2018-06-25 15:12:39 +02:00
|
|
|
|
2019-01-10 11:27:06 +01:00
|
|
|
let listener = TimeoutListener {
|
|
|
|
inner: listener,
|
|
|
|
timeout: self.incoming_timeout,
|
|
|
|
};
|
|
|
|
|
2019-04-10 10:29:21 +02:00
|
|
|
Ok(listener)
|
2018-06-25 15:12:39 +02:00
|
|
|
}
|
|
|
|
|
2019-01-10 11:27:06 +01:00
|
|
|
fn dial(self, addr: Multiaddr) -> Result<Self::Dial, TransportError<Self::Error>> {
|
|
|
|
let dial = self.inner.dial(addr)
|
|
|
|
.map_err(|err| err.map(TransportTimeoutError::Other))?;
|
2019-09-16 11:08:44 +02:00
|
|
|
Ok(Timeout {
|
|
|
|
inner: dial,
|
|
|
|
timer: Delay::new(self.outgoing_timeout),
|
2019-01-10 11:27:06 +01:00
|
|
|
})
|
2018-06-25 15:12:39 +02:00
|
|
|
}
|
2021-01-12 13:35:11 +01:00
|
|
|
|
|
|
|
fn address_translation(&self, server: &Multiaddr, observed: &Multiaddr) -> Option<Multiaddr> {
|
|
|
|
self.inner.address_translation(server, observed)
|
|
|
|
}
|
2018-06-25 15:12:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: can be removed and replaced with an `impl Stream` once impl Trait is fully stable
|
|
|
|
// in Rust (https://github.com/rust-lang/rust/issues/34511)
|
2019-12-10 11:46:30 +01:00
|
|
|
#[pin_project::pin_project]
|
2018-06-25 15:12:39 +02:00
|
|
|
pub struct TimeoutListener<InnerStream> {
|
2019-12-10 11:46:30 +01:00
|
|
|
#[pin]
|
2018-06-25 15:12:39 +02:00
|
|
|
inner: InnerStream,
|
|
|
|
timeout: Duration,
|
|
|
|
}
|
|
|
|
|
2020-02-13 12:05:45 +01:00
|
|
|
impl<InnerStream, O, E> Stream for TimeoutListener<InnerStream>
|
2018-07-11 11:14:40 +02:00
|
|
|
where
|
2020-02-13 12:05:45 +01:00
|
|
|
InnerStream: TryStream<Ok = ListenerEvent<O, E>, Error = E>,
|
2018-06-25 15:12:39 +02:00
|
|
|
{
|
2020-02-13 12:05:45 +01:00
|
|
|
type Item = Result<ListenerEvent<Timeout<O>, TransportTimeoutError<E>>, TransportTimeoutError<E>>;
|
2019-09-16 11:08:44 +02:00
|
|
|
|
2020-07-27 20:27:33 +00:00
|
|
|
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
|
2019-12-10 11:46:30 +01:00
|
|
|
let this = self.project();
|
|
|
|
|
|
|
|
let poll_out = match TryStream::try_poll_next(this.inner, cx) {
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Ready(Some(Err(err))) => return Poll::Ready(Some(Err(TransportTimeoutError::Other(err)))),
|
|
|
|
Poll::Ready(Some(Ok(v))) => v,
|
|
|
|
Poll::Ready(None) => return Poll::Ready(None),
|
|
|
|
Poll::Pending => return Poll::Pending,
|
|
|
|
};
|
|
|
|
|
2019-12-10 11:46:30 +01:00
|
|
|
let timeout = *this.timeout;
|
2020-02-13 12:05:45 +01:00
|
|
|
let event = poll_out
|
|
|
|
.map(move |inner_fut| {
|
|
|
|
Timeout {
|
|
|
|
inner: inner_fut,
|
|
|
|
timer: Delay::new(timeout),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.map_err(TransportTimeoutError::Other);
|
2019-09-16 11:08:44 +02:00
|
|
|
|
|
|
|
Poll::Ready(Some(Ok(event)))
|
2018-06-25 15:12:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-10 11:27:06 +01:00
|
|
|
/// Wraps around a `Future`. Turns the error type from `TimeoutError<Err>` to
|
|
|
|
/// `TransportTimeoutError<Err>`.
|
2018-06-25 15:12:39 +02:00
|
|
|
// TODO: can be replaced with `impl Future` once `impl Trait` are fully stable in Rust
|
|
|
|
// (https://github.com/rust-lang/rust/issues/34511)
|
2019-12-10 11:46:30 +01:00
|
|
|
#[pin_project::pin_project]
|
2018-09-04 18:30:57 +08:00
|
|
|
#[must_use = "futures do nothing unless polled"]
|
2019-09-16 11:08:44 +02:00
|
|
|
pub struct Timeout<InnerFut> {
|
2019-12-10 11:46:30 +01:00
|
|
|
#[pin]
|
2018-06-25 15:12:39 +02:00
|
|
|
inner: InnerFut,
|
2019-09-16 11:08:44 +02:00
|
|
|
timer: Delay,
|
2018-06-25 15:12:39 +02:00
|
|
|
}
|
|
|
|
|
2019-09-16 11:08:44 +02:00
|
|
|
impl<InnerFut> Future for Timeout<InnerFut>
|
2018-07-11 11:14:40 +02:00
|
|
|
where
|
2019-12-10 11:46:30 +01:00
|
|
|
InnerFut: TryFuture,
|
2018-06-25 15:12:39 +02:00
|
|
|
{
|
2019-09-16 11:08:44 +02:00
|
|
|
type Output = Result<InnerFut::Ok, TransportTimeoutError<InnerFut::Error>>;
|
|
|
|
|
2020-07-27 20:27:33 +00:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
2019-09-16 11:08:44 +02:00
|
|
|
// It is debatable whether we should poll the inner future first or the timer first.
|
|
|
|
// For example, if you start dialing with a timeout of 10 seconds, then after 15 seconds
|
|
|
|
// the dialing succeeds on the wire, then after 20 seconds you poll, then depending on
|
|
|
|
// which gets polled first, the outcome will be success or failure.
|
|
|
|
|
2019-12-10 11:46:30 +01:00
|
|
|
let mut this = self.project();
|
|
|
|
|
|
|
|
match TryFuture::try_poll(this.inner, cx) {
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Pending => {},
|
|
|
|
Poll::Ready(Ok(v)) => return Poll::Ready(Ok(v)),
|
|
|
|
Poll::Ready(Err(err)) => return Poll::Ready(Err(TransportTimeoutError::Other(err))),
|
|
|
|
}
|
|
|
|
|
2019-12-18 16:50:07 +01:00
|
|
|
match Pin::new(&mut this.timer).poll(cx) {
|
2019-09-16 11:08:44 +02:00
|
|
|
Poll::Pending => Poll::Pending,
|
2019-12-18 16:50:07 +01:00
|
|
|
Poll::Ready(()) => Poll::Ready(Err(TransportTimeoutError::Timeout))
|
2019-09-16 11:08:44 +02:00
|
|
|
}
|
2018-06-25 15:12:39 +02:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 11:27:06 +01:00
|
|
|
|
|
|
|
/// Error that can be produced by the `TransportTimeout` layer.
|
2019-09-16 11:08:44 +02:00
|
|
|
#[derive(Debug)]
|
2019-01-10 11:27:06 +01:00
|
|
|
pub enum TransportTimeoutError<TErr> {
|
|
|
|
/// The transport timed out.
|
|
|
|
Timeout,
|
|
|
|
/// An error happened in the timer.
|
2019-09-16 11:08:44 +02:00
|
|
|
TimerError(io::Error),
|
2019-01-10 11:27:06 +01:00
|
|
|
/// Other kind of error.
|
|
|
|
Other(TErr),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TErr> fmt::Display for TransportTimeoutError<TErr>
|
|
|
|
where TErr: fmt::Display,
|
|
|
|
{
|
2019-02-11 14:58:15 +01:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2019-01-10 11:27:06 +01:00
|
|
|
match self {
|
|
|
|
TransportTimeoutError::Timeout => write!(f, "Timeout has been reached"),
|
2019-09-16 11:08:44 +02:00
|
|
|
TransportTimeoutError::TimerError(err) => write!(f, "Error in the timer: {}", err),
|
2019-01-10 11:27:06 +01:00
|
|
|
TransportTimeoutError::Other(err) => write!(f, "{}", err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<TErr> error::Error for TransportTimeoutError<TErr>
|
|
|
|
where TErr: error::Error + 'static,
|
|
|
|
{
|
|
|
|
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
|
|
|
match self {
|
|
|
|
TransportTimeoutError::Timeout => None,
|
2019-09-16 11:08:44 +02:00
|
|
|
TransportTimeoutError::TimerError(err) => Some(err),
|
2019-01-10 11:27:06 +01:00
|
|
|
TransportTimeoutError::Other(err) => Some(err),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|