-
Notifications
You must be signed in to change notification settings - Fork 1.1k
refactor(swarm)!: don't be generic over Transport
#3272
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
Changes from 5 commits
59bcc9f
a1c55b2
7132ff5
01efa8f
b0f2678
df8362c
7de9474
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ use crate::{ | |
Connected, ConnectionError, ConnectionLimit, IncomingInfo, PendingConnectionError, | ||
PendingInboundConnectionError, PendingOutboundConnectionError, | ||
}, | ||
transport::{Transport, TransportError}, | ||
transport::TransportError, | ||
ConnectedPoint, ConnectionHandler, Executor, IntoConnectionHandler, Multiaddr, PeerId, | ||
}; | ||
use concurrent_dial::ConcurrentDial; | ||
|
@@ -79,9 +79,8 @@ impl ExecSwitch { | |
} | ||
|
||
/// A connection `Pool` manages a set of connections for each peer. | ||
pub struct Pool<THandler, TTrans> | ||
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. what do you think of making this 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. In general I think I'd prefer to not use It would be nice if we can assume that:
I am not fully convinced it is a good idea though, might cause a lot of complexity. 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 wish Rust had a way to whitelist what is in the public API of a crate and fail to compile if something is being added. 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. if we decide that every |
||
pub struct Pool<THandler> | ||
where | ||
TTrans: Transport, | ||
THandler: IntoConnectionHandler, | ||
{ | ||
local_id: PeerId, | ||
|
@@ -124,10 +123,10 @@ where | |
|
||
/// Sender distributed to pending tasks for reporting events back | ||
/// to the pool. | ||
pending_connection_events_tx: mpsc::Sender<task::PendingConnectionEvent<TTrans>>, | ||
pending_connection_events_tx: mpsc::Sender<task::PendingConnectionEvent>, | ||
|
||
/// Receiver for events reported from pending tasks. | ||
pending_connection_events_rx: mpsc::Receiver<task::PendingConnectionEvent<TTrans>>, | ||
pending_connection_events_rx: mpsc::Receiver<task::PendingConnectionEvent>, | ||
|
||
/// Sender distributed to established tasks for reporting events back | ||
/// to the pool. | ||
|
@@ -213,7 +212,7 @@ impl<THandler> PendingConnection<THandler> { | |
} | ||
} | ||
|
||
impl<THandler: IntoConnectionHandler, TTrans: Transport> fmt::Debug for Pool<THandler, TTrans> { | ||
impl<THandler: IntoConnectionHandler> fmt::Debug for Pool<THandler> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { | ||
f.debug_struct("Pool") | ||
.field("counters", &self.counters) | ||
|
@@ -223,10 +222,7 @@ impl<THandler: IntoConnectionHandler, TTrans: Transport> fmt::Debug for Pool<THa | |
|
||
/// Event that can happen on the `Pool`. | ||
#[derive(Debug)] | ||
pub enum PoolEvent<THandler: IntoConnectionHandler, TTrans> | ||
where | ||
TTrans: Transport, | ||
{ | ||
pub enum PoolEvent<THandler: IntoConnectionHandler> { | ||
/// A new connection has been established. | ||
ConnectionEstablished { | ||
id: ConnectionId, | ||
|
@@ -239,7 +235,7 @@ where | |
/// [`Some`] when the new connection is an outgoing connection. | ||
/// Addresses are dialed in parallel. Contains the addresses and errors | ||
/// of dial attempts that failed before the one successful dial. | ||
concurrent_dial_errors: Option<Vec<(Multiaddr, TransportError<TTrans::Error>)>>, | ||
concurrent_dial_errors: Option<Vec<(Multiaddr, TransportError<std::io::Error>)>>, | ||
/// How long it took to establish this connection. | ||
established_in: std::time::Duration, | ||
}, | ||
|
@@ -272,7 +268,7 @@ where | |
/// The ID of the failed connection. | ||
id: ConnectionId, | ||
/// The error that occurred. | ||
error: PendingOutboundConnectionError<TTrans::Error>, | ||
error: PendingOutboundConnectionError, | ||
/// The handler that was supposed to handle the connection. | ||
handler: THandler, | ||
/// The (expected) peer of the failed connection. | ||
|
@@ -288,7 +284,7 @@ where | |
/// Local connection address. | ||
local_addr: Multiaddr, | ||
/// The error that occurred. | ||
error: PendingInboundConnectionError<TTrans::Error>, | ||
error: PendingInboundConnectionError, | ||
/// The handler that was supposed to handle the connection. | ||
handler: THandler, | ||
}, | ||
|
@@ -312,10 +308,9 @@ where | |
}, | ||
} | ||
|
||
impl<THandler, TTrans> Pool<THandler, TTrans> | ||
impl<THandler> Pool<THandler> | ||
where | ||
THandler: IntoConnectionHandler, | ||
TTrans: Transport, | ||
{ | ||
/// Creates a new empty `Pool`. | ||
pub fn new(local_id: PeerId, config: PoolConfig, limits: ConnectionLimits) -> Self { | ||
|
@@ -429,12 +424,9 @@ where | |
} | ||
} | ||
|
||
impl<THandler, TTrans> Pool<THandler, TTrans> | ||
impl<THandler> Pool<THandler> | ||
where | ||
THandler: IntoConnectionHandler, | ||
TTrans: Transport + 'static, | ||
TTrans::Output: Send + 'static, | ||
TTrans::Error: Send + 'static, | ||
{ | ||
/// Adds a pending outgoing connection to the pool in the form of a `Future` | ||
/// that establishes and negotiates the connection. | ||
|
@@ -448,22 +440,15 @@ where | |
'static, | ||
( | ||
Multiaddr, | ||
Result< | ||
<TTrans as Transport>::Output, | ||
TransportError<<TTrans as Transport>::Error>, | ||
>, | ||
Result<(PeerId, StreamMuxerBox), TransportError<std::io::Error>>, | ||
), | ||
>, | ||
>, | ||
peer: Option<PeerId>, | ||
handler: THandler, | ||
role_override: Endpoint, | ||
dial_concurrency_factor_override: Option<NonZeroU8>, | ||
) -> Result<ConnectionId, (ConnectionLimit, THandler)> | ||
where | ||
TTrans: Send, | ||
TTrans::Dial: Send + 'static, | ||
{ | ||
) -> Result<ConnectionId, (ConnectionLimit, THandler)> { | ||
if let Err(limit) = self.counters.check_max_pending_outgoing() { | ||
return Err((limit, handler)); | ||
}; | ||
|
@@ -515,7 +500,7 @@ where | |
info: IncomingInfo<'_>, | ||
) -> Result<ConnectionId, (ConnectionLimit, THandler)> | ||
where | ||
TFut: Future<Output = Result<TTrans::Output, TTrans::Error>> + Send + 'static, | ||
TFut: Future<Output = Result<(PeerId, StreamMuxerBox), std::io::Error>> + Send + 'static, | ||
{ | ||
let endpoint = info.create_connected_point(); | ||
|
||
|
@@ -552,9 +537,8 @@ where | |
} | ||
|
||
/// Polls the connection pool for events. | ||
pub fn poll(&mut self, cx: &mut Context<'_>) -> Poll<PoolEvent<THandler, TTrans>> | ||
pub fn poll(&mut self, cx: &mut Context<'_>) -> Poll<PoolEvent<THandler>> | ||
where | ||
TTrans: Transport<Output = (PeerId, StreamMuxerBox)>, | ||
THandler: IntoConnectionHandler + 'static, | ||
THandler::Handler: ConnectionHandler + Send, | ||
<THandler::Handler as ConnectionHandler>::OutboundOpenInfo: Send, | ||
|
@@ -677,7 +661,7 @@ where | |
), | ||
}; | ||
|
||
let error: Result<(), PendingInboundConnectionError<_>> = self | ||
let error = self | ||
.counters | ||
// Check general established connection limit. | ||
.check_max_established(&endpoint) | ||
|
Uh oh!
There was an error while loading. Please reload this page.