mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-23 14:51:34 +00:00
Update root crate to use futures-0.3. (#1315)
Mostly mechanical. Creating a `CommonTransport` yields an `io::Result<_>` now since creating the `DnsConfig` may fail with an `io::Error` when creating the `ThreadPool`. The `DnsConfig` `Transport` impl had to be changed slightly: (a) PR [[1311]] requires some `Send` bounds. (b) The async block had to be changed to work around lifetime inference issues which resulted in an "one type is more general than the other" error. [1311]: https://github.com/libp2p/rust-libp2p/pull/1311
This commit is contained in:
110
src/bandwidth.rs
110
src/bandwidth.rs
@ -19,11 +19,11 @@
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use crate::{Multiaddr, core::{Transport, transport::{ListenerEvent, TransportError}}};
|
||||
use futures::{prelude::*, try_ready};
|
||||
use futures::{prelude::*, io::{IoSlice, IoSliceMut}, ready};
|
||||
use lazy_static::lazy_static;
|
||||
use parking_lot::Mutex;
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
use std::{cmp, io, io::Read, io::Write, sync::Arc, time::Duration};
|
||||
use std::{cmp, io, pin::Pin, sync::Arc, task::{Context, Poll}, time::Duration};
|
||||
use wasm_timer::Instant;
|
||||
|
||||
/// Wraps around a `Transport` and logs the bandwidth that goes through all the opened connections.
|
||||
@ -35,7 +35,6 @@ pub struct BandwidthLogging<TInner> {
|
||||
|
||||
impl<TInner> BandwidthLogging<TInner> {
|
||||
/// Creates a new `BandwidthLogging` around the transport.
|
||||
#[inline]
|
||||
pub fn new(inner: TInner, period: Duration) -> (Self, Arc<BandwidthSinks>) {
|
||||
let mut period_seconds = cmp::min(period.as_secs(), 86400) as u32;
|
||||
if period.subsec_nanos() > 0 {
|
||||
@ -58,7 +57,10 @@ impl<TInner> BandwidthLogging<TInner> {
|
||||
|
||||
impl<TInner> Transport for BandwidthLogging<TInner>
|
||||
where
|
||||
TInner: Transport,
|
||||
TInner: Transport + Unpin,
|
||||
TInner::Dial: Unpin,
|
||||
TInner::Listener: Unpin,
|
||||
TInner::ListenerUpgrade: Unpin
|
||||
{
|
||||
type Output = BandwidthConnecLogging<TInner::Output>;
|
||||
type Error = TInner::Error;
|
||||
@ -90,22 +92,23 @@ pub struct BandwidthListener<TInner> {
|
||||
|
||||
impl<TInner, TConn> Stream for BandwidthListener<TInner>
|
||||
where
|
||||
TInner: Stream<Item = ListenerEvent<TConn>>,
|
||||
TInner: TryStream<Ok = ListenerEvent<TConn>> + Unpin
|
||||
{
|
||||
type Item = ListenerEvent<BandwidthFuture<TConn>>;
|
||||
type Error = TInner::Error;
|
||||
type Item = Result<ListenerEvent<BandwidthFuture<TConn>>, TInner::Error>;
|
||||
|
||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
||||
let event = match try_ready!(self.inner.poll()) {
|
||||
Some(v) => v,
|
||||
None => return Ok(Async::Ready(None))
|
||||
};
|
||||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
||||
let event =
|
||||
if let Some(event) = ready!(self.inner.try_poll_next_unpin(cx)?) {
|
||||
event
|
||||
} else {
|
||||
return Poll::Ready(None)
|
||||
};
|
||||
|
||||
let event = event.map(|inner| {
|
||||
BandwidthFuture { inner, sinks: self.sinks.clone() }
|
||||
});
|
||||
|
||||
Ok(Async::Ready(Some(event)))
|
||||
Poll::Ready(Some(Ok(event)))
|
||||
}
|
||||
}
|
||||
|
||||
@ -116,18 +119,13 @@ pub struct BandwidthFuture<TInner> {
|
||||
sinks: Arc<BandwidthSinks>,
|
||||
}
|
||||
|
||||
impl<TInner> Future for BandwidthFuture<TInner>
|
||||
where TInner: Future,
|
||||
{
|
||||
type Item = BandwidthConnecLogging<TInner::Item>;
|
||||
type Error = TInner::Error;
|
||||
impl<TInner: TryFuture + Unpin> Future for BandwidthFuture<TInner> {
|
||||
type Output = Result<BandwidthConnecLogging<TInner::Ok>, TInner::Error>;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
let inner = try_ready!(self.inner.poll());
|
||||
Ok(Async::Ready(BandwidthConnecLogging {
|
||||
inner,
|
||||
sinks: self.sinks.clone(),
|
||||
}))
|
||||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
||||
let inner = ready!(self.inner.try_poll_unpin(cx)?);
|
||||
let logged = BandwidthConnecLogging { inner, sinks: self.sinks.clone() };
|
||||
Poll::Ready(Ok(logged))
|
||||
}
|
||||
}
|
||||
|
||||
@ -139,13 +137,11 @@ pub struct BandwidthSinks {
|
||||
|
||||
impl BandwidthSinks {
|
||||
/// Returns the average number of bytes that have been downloaded in the period.
|
||||
#[inline]
|
||||
pub fn average_download_per_sec(&self) -> u64 {
|
||||
self.download.lock().get()
|
||||
}
|
||||
|
||||
/// Returns the average number of bytes that have been uploaded in the period.
|
||||
#[inline]
|
||||
pub fn average_upload_per_sec(&self) -> u64 {
|
||||
self.upload.lock().get()
|
||||
}
|
||||
@ -157,56 +153,43 @@ pub struct BandwidthConnecLogging<TInner> {
|
||||
sinks: Arc<BandwidthSinks>,
|
||||
}
|
||||
|
||||
impl<TInner> Read for BandwidthConnecLogging<TInner>
|
||||
where TInner: Read
|
||||
{
|
||||
#[inline]
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
let num_bytes = self.inner.read(buf)?;
|
||||
impl<TInner: AsyncRead + Unpin> AsyncRead for BandwidthConnecLogging<TInner> {
|
||||
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context, buf: &mut [u8]) -> Poll<io::Result<usize>> {
|
||||
let num_bytes = ready!(Pin::new(&mut self.inner).poll_read(cx, buf))?;
|
||||
self.sinks.download.lock().inject(num_bytes);
|
||||
Ok(num_bytes)
|
||||
Poll::Ready(Ok(num_bytes))
|
||||
}
|
||||
|
||||
fn poll_read_vectored(mut self: Pin<&mut Self>, cx: &mut Context, bufs: &mut [IoSliceMut]) -> Poll<io::Result<usize>> {
|
||||
let num_bytes = ready!(Pin::new(&mut self.inner).poll_read_vectored(cx, bufs))?;
|
||||
self.sinks.download.lock().inject(num_bytes);
|
||||
Poll::Ready(Ok(num_bytes))
|
||||
}
|
||||
}
|
||||
|
||||
impl<TInner> tokio_io::AsyncRead for BandwidthConnecLogging<TInner>
|
||||
where TInner: tokio_io::AsyncRead
|
||||
{
|
||||
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
|
||||
self.inner.prepare_uninitialized_buffer(buf)
|
||||
}
|
||||
|
||||
fn read_buf<B: bytes::BufMut>(&mut self, buf: &mut B) -> Poll<usize, io::Error> {
|
||||
self.inner.read_buf(buf)
|
||||
}
|
||||
}
|
||||
|
||||
impl<TInner> Write for BandwidthConnecLogging<TInner>
|
||||
where TInner: Write
|
||||
{
|
||||
#[inline]
|
||||
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
|
||||
let num_bytes = self.inner.write(buf)?;
|
||||
impl<TInner: AsyncWrite + Unpin> AsyncWrite for BandwidthConnecLogging<TInner> {
|
||||
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context, buf: &[u8]) -> Poll<io::Result<usize>> {
|
||||
let num_bytes = ready!(Pin::new(&mut self.inner).poll_write(cx, buf))?;
|
||||
self.sinks.upload.lock().inject(num_bytes);
|
||||
Ok(num_bytes)
|
||||
Poll::Ready(Ok(num_bytes))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn flush(&mut self) -> io::Result<()> {
|
||||
self.inner.flush()
|
||||
fn poll_write_vectored(mut self: Pin<&mut Self>, cx: &mut Context, bufs: &[IoSlice]) -> Poll<io::Result<usize>> {
|
||||
let num_bytes = ready!(Pin::new(&mut self.inner).poll_write_vectored(cx, bufs))?;
|
||||
self.sinks.upload.lock().inject(num_bytes);
|
||||
Poll::Ready(Ok(num_bytes))
|
||||
}
|
||||
}
|
||||
|
||||
impl<TInner> tokio_io::AsyncWrite for BandwidthConnecLogging<TInner>
|
||||
where TInner: tokio_io::AsyncWrite
|
||||
{
|
||||
#[inline]
|
||||
fn shutdown(&mut self) -> Poll<(), io::Error> {
|
||||
self.inner.shutdown()
|
||||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
|
||||
Pin::new(&mut self.inner).poll_flush(cx)
|
||||
}
|
||||
|
||||
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<io::Result<()>> {
|
||||
Pin::new(&mut self.inner).poll_close(cx)
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the number of seconds that have elapsed between an arbitrary EPOCH and now.
|
||||
#[inline]
|
||||
fn current_second() -> u32 {
|
||||
lazy_static! {
|
||||
static ref EPOCH: Instant = Instant::now();
|
||||
@ -267,7 +250,6 @@ impl BandwidthSink {
|
||||
self.bytes.remove(0);
|
||||
self.bytes.push(0);
|
||||
}
|
||||
|
||||
self.latest_update = current_second;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user