-
Notifications
You must be signed in to change notification settings - Fork 941
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
webrtc: track number of bytes sent / received by all substreams #3162
Closed
melekes
wants to merge
10
commits into
libp2p:3157-bandwidth-logging
from
melekes:3157-bandwidth-logging
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
49da84f
webrtc: track number of bytes sent / received by all substreams
melekes 464b371
count the bytes on UDP muxer level
melekes d3f862e
update docs
melekes ce82d0b
use a map
melekes 03ef33f
write tests
melekes 377c84f
update comments
melekes b02bc50
reset counters after each call
melekes e75c6b8
simplify implementation
melekes d205188
remove fnv
melekes ae56be6
implement `WithBandwidthSinks` for more transports
melekes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2022 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. | ||
|
||
use std::sync::atomic::{AtomicU64, Ordering}; | ||
|
||
/// Holds the counters used when logging bandwidth. | ||
/// | ||
/// Can be safely shared between threads. | ||
pub(crate) struct Bandwidth { | ||
inbound: AtomicU64, | ||
outbound: AtomicU64, | ||
} | ||
impl Bandwidth { | ||
/// Creates a new [`Bandwidth`]. | ||
pub(crate) fn new() -> Self { | ||
Self { | ||
inbound: AtomicU64::new(0), | ||
outbound: AtomicU64::new(0), | ||
} | ||
} | ||
|
||
/// Increases the number of bytes received. | ||
pub(crate) fn add_inbound(&self, n: u64) { | ||
self.inbound.fetch_add(n, Ordering::Relaxed); | ||
} | ||
|
||
/// Increases the number of bytes sent for a given address. | ||
pub(crate) fn add_outbound(&self, n: u64) { | ||
self.outbound.fetch_add(n, Ordering::Relaxed); | ||
} | ||
|
||
/// Gets the number of bytes received & sent. Resets both counters to zero after the call. | ||
pub(crate) fn reset(&self) -> (u64, u64) { | ||
( | ||
self.inbound.swap(0, Ordering::Relaxed), | ||
self.outbound.swap(0, Ordering::Relaxed), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ use std::{ | |
task::{Context, Poll}, | ||
}; | ||
|
||
use crate::tokio::bandwidth::Bandwidth; | ||
use crate::tokio::{error::Error, substream, substream::Substream}; | ||
|
||
/// Maximum number of unprocessed data channels. | ||
|
@@ -65,13 +66,18 @@ pub struct Connection { | |
/// A list of futures, which, once completed, signal that a [`Substream`] has been dropped. | ||
drop_listeners: FuturesUnordered<substream::DropListener>, | ||
no_drop_listeners_waker: Option<Waker>, | ||
|
||
/// Bandwidth logging, which is done in `UDPMuxNewAddr`. | ||
/// | ||
/// Shared between all connections. | ||
bandwidth: Arc<Bandwidth>, | ||
} | ||
|
||
impl Unpin for Connection {} | ||
|
||
impl Connection { | ||
/// Creates a new connection. | ||
pub(crate) async fn new(rtc_conn: RTCPeerConnection) -> Self { | ||
pub(crate) async fn new(rtc_conn: RTCPeerConnection, bandwidth: Arc<Bandwidth>) -> Self { | ||
let (data_channel_tx, data_channel_rx) = mpsc::channel(MAX_DATA_CHANNELS_IN_FLIGHT); | ||
|
||
Connection::register_incoming_data_channels_handler( | ||
|
@@ -87,6 +93,7 @@ impl Connection { | |
close_fut: None, | ||
drop_listeners: FuturesUnordered::default(), | ||
no_drop_listeners_waker: None, | ||
bandwidth, | ||
} | ||
} | ||
|
||
|
@@ -144,6 +151,13 @@ impl Connection { | |
}) | ||
})); | ||
} | ||
|
||
/// Fetches the current bandwidth (for all connections). Counters are reset after each call. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is not perfect from API design, but it's the trade-off. You can browse the commit history. |
||
/// | ||
/// Only use this for updating bandwidth. | ||
pub fn fetch_current_bandwidth(&self) -> (u64, u64) { | ||
self.bandwidth.reset() | ||
} | ||
} | ||
|
||
impl StreamMuxer for Connection { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure what to do with transports who don't have their own connection type. is it okay to import async_std/tokio here? or I should reexport these types?