mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-05-29 02:31:20 +00:00
fix(libp2p): add shortcut with_tcp(...).with_bandwidth_logging()
Add the shortcut method `.with_bandwidth_logging` to `SwarmBuilder<_, QuicPhase<_>>`, thus allowing `with_tcp(...).with_bandwidth_logging()`. Pull-Request: #4626.
This commit is contained in:
parent
56cb08a9b0
commit
74c087dcef
@ -66,6 +66,8 @@ pub struct SwarmBuilder<Provider, Phase> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::SwarmBuilder;
|
use crate::SwarmBuilder;
|
||||||
|
use libp2p_core::{muxing::StreamMuxerBox, transport::dummy::DummyTransport};
|
||||||
|
use libp2p_identity::PeerId;
|
||||||
use libp2p_swarm::{NetworkBehaviour, Swarm};
|
use libp2p_swarm::{NetworkBehaviour, Swarm};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -228,9 +230,6 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
#[cfg(feature = "tokio")]
|
#[cfg(feature = "tokio")]
|
||||||
fn other_transport() -> Result<(), Box<dyn std::error::Error>> {
|
fn other_transport() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
use libp2p_core::{muxing::StreamMuxerBox, transport::dummy::DummyTransport};
|
|
||||||
use libp2p_identity::PeerId;
|
|
||||||
|
|
||||||
let _ = SwarmBuilder::with_new_identity()
|
let _ = SwarmBuilder::with_new_identity()
|
||||||
.with_tokio()
|
.with_tokio()
|
||||||
// Closure can either return a Transport directly.
|
// Closure can either return a Transport directly.
|
||||||
@ -319,4 +318,56 @@ mod tests {
|
|||||||
.unwrap()
|
.unwrap()
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(all(feature = "tokio", feature = "tcp", feature = "tls", feature = "yamux"))]
|
||||||
|
fn tcp_bandwidth_logging() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let (builder, _logging) = SwarmBuilder::with_new_identity()
|
||||||
|
.with_tokio()
|
||||||
|
.with_tcp(
|
||||||
|
Default::default(),
|
||||||
|
libp2p_tls::Config::new,
|
||||||
|
libp2p_yamux::Config::default,
|
||||||
|
)?
|
||||||
|
.with_bandwidth_logging();
|
||||||
|
|
||||||
|
builder
|
||||||
|
.with_behaviour(|_| libp2p_swarm::dummy::Behaviour)
|
||||||
|
.unwrap()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(all(feature = "tokio", feature = "quic"))]
|
||||||
|
fn quic_bandwidth_logging() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let (builder, _logging) = SwarmBuilder::with_new_identity()
|
||||||
|
.with_tokio()
|
||||||
|
.with_quic()
|
||||||
|
.with_bandwidth_logging();
|
||||||
|
|
||||||
|
builder
|
||||||
|
.with_behaviour(|_| libp2p_swarm::dummy::Behaviour)
|
||||||
|
.unwrap()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(feature = "tokio")]
|
||||||
|
fn other_transport_bandwidth_logging() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
let (builder, _logging) = SwarmBuilder::with_new_identity()
|
||||||
|
.with_tokio()
|
||||||
|
.with_other_transport(|_| DummyTransport::<(PeerId, StreamMuxerBox)>::new())?
|
||||||
|
.with_bandwidth_logging();
|
||||||
|
|
||||||
|
builder
|
||||||
|
.with_behaviour(|_| libp2p_swarm::dummy::Behaviour)
|
||||||
|
.unwrap()
|
||||||
|
.build();
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ use libp2p_core::muxing::StreamMuxer;
|
|||||||
all(not(target_arch = "wasm32"), feature = "websocket")
|
all(not(target_arch = "wasm32"), feature = "websocket")
|
||||||
))]
|
))]
|
||||||
use libp2p_core::{InboundUpgrade, Negotiated, OutboundUpgrade, UpgradeInfo};
|
use libp2p_core::{InboundUpgrade, Negotiated, OutboundUpgrade, UpgradeInfo};
|
||||||
use std::marker::PhantomData;
|
use std::{marker::PhantomData, sync::Arc};
|
||||||
|
|
||||||
pub struct QuicPhase<T> {
|
pub struct QuicPhase<T> {
|
||||||
pub(crate) transport: T,
|
pub(crate) transport: T,
|
||||||
@ -237,3 +237,21 @@ impl_quic_phase_with_websocket!(
|
|||||||
super::provider::Tokio,
|
super::provider::Tokio,
|
||||||
rw_stream_sink::RwStreamSink<libp2p_websocket::BytesConnection<libp2p_tcp::tokio::TcpStream>>
|
rw_stream_sink::RwStreamSink<libp2p_websocket::BytesConnection<libp2p_tcp::tokio::TcpStream>>
|
||||||
);
|
);
|
||||||
|
impl<Provider, T: AuthenticatedMultiplexedTransport> SwarmBuilder<Provider, QuicPhase<T>> {
|
||||||
|
pub fn with_bandwidth_logging(
|
||||||
|
self,
|
||||||
|
) -> (
|
||||||
|
SwarmBuilder<
|
||||||
|
Provider,
|
||||||
|
BehaviourPhase<impl AuthenticatedMultiplexedTransport, NoRelayBehaviour>,
|
||||||
|
>,
|
||||||
|
Arc<crate::bandwidth::BandwidthSinks>,
|
||||||
|
) {
|
||||||
|
self.without_quic()
|
||||||
|
.without_any_other_transports()
|
||||||
|
.without_dns()
|
||||||
|
.without_relay()
|
||||||
|
.without_websocket()
|
||||||
|
.with_bandwidth_logging()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user