// Copyright 2017-2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see .
//! Metered variant of unbounded mpsc channels to be able to extract metrics.
use futures::{channel::mpsc, task::Poll, task::Context, sink::SinkExt, stream::Stream};
use std::result;
use std::pin::Pin;
use super::Meter;
/// Create a wrapped `mpsc::channel` pair of `MeteredSender` and `MeteredReceiver`.
pub fn unbounded() -> (UnboundedMeteredSender, UnboundedMeteredReceiver) {
let (tx, rx) = mpsc::unbounded();
let shared_meter = Meter::default();
let tx = UnboundedMeteredSender { meter: shared_meter.clone(), inner: tx };
let rx = UnboundedMeteredReceiver { meter: shared_meter, inner: rx };
(tx, rx)
}
/// A receiver tracking the messages consumed by itself.
#[derive(Debug)]
pub struct UnboundedMeteredReceiver {
// count currently contained messages
meter: Meter,
inner: mpsc::UnboundedReceiver,
}
impl std::ops::Deref for UnboundedMeteredReceiver {
type Target = mpsc::UnboundedReceiver;
fn deref(&self) -> &Self::Target {
&self.inner
}
}
impl std::ops::DerefMut for UnboundedMeteredReceiver {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.inner
}
}
impl Stream for UnboundedMeteredReceiver {
type Item = T;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll