litep2p/
bandwidth.rs

1// Copyright 2023 litep2p developers
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21//! Bandwidth sinks for metering inbound/outbound bytes.
22
23use std::sync::{
24    atomic::{AtomicUsize, Ordering},
25    Arc,
26};
27
28/// Inner bandwidth sink
29#[derive(Debug)]
30struct InnerBandwidthSink {
31    /// Number of inbound bytes.
32    inbound: AtomicUsize,
33
34    /// Number of outbound bytes.
35    outbound: AtomicUsize,
36}
37
38/// Bandwidth sink which provides metering for inbound/outbound byte usage.
39///
40/// The reported values are not necessarily up to date with the latest information
41/// and should not be used for metrics that require high precision but they do provide
42/// an overall view of the data usage of `litep2p`.
43#[derive(Debug, Clone)]
44pub struct BandwidthSink(Arc<InnerBandwidthSink>);
45
46impl BandwidthSink {
47    /// Create new [`BandwidthSink`].
48    pub(crate) fn new() -> Self {
49        Self(Arc::new(InnerBandwidthSink {
50            inbound: AtomicUsize::new(0usize),
51            outbound: AtomicUsize::new(0usize),
52        }))
53    }
54
55    /// Increase the amount of inbound bytes.
56    pub(crate) fn increase_inbound(&self, bytes: usize) {
57        let _ = self.0.inbound.fetch_add(bytes, Ordering::Relaxed);
58    }
59
60    /// Increse the amount of outbound bytes.
61    pub(crate) fn increase_outbound(&self, bytes: usize) {
62        let _ = self.0.outbound.fetch_add(bytes, Ordering::Relaxed);
63    }
64
65    /// Get total the number of bytes received.
66    pub fn inbound(&self) -> usize {
67        self.0.inbound.load(Ordering::Relaxed)
68    }
69
70    /// Get total the nubmer of bytes sent.
71    pub fn outbound(&self) -> usize {
72        self.0.outbound.load(Ordering::Relaxed)
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn verify_bandwidth() {
82        let sink = BandwidthSink::new();
83
84        sink.increase_inbound(1337usize);
85        sink.increase_outbound(1338usize);
86
87        assert_eq!(sink.inbound(), 1337usize);
88        assert_eq!(sink.outbound(), 1338usize);
89    }
90}