Skip to content

Commit

Permalink
refactor!: replace Endpoint addr/id getters
Browse files Browse the repository at this point in the history
Rather than `Endpoint` having getters for `addr`
(`get_socket_addr_by_id`) and `id` (`get_connection_id`) there are now
only getters for `Connection`, either by `addr`
(`get_connection_by_addr`) or by `id` (`get_connection_by_id`).

For now this mostly adds indirection for callers, who can call
`Connection::id` or `Connection::remote_address` on the result to
achieve the same outcome as before.

BREAKING CHANGE: `Endpoint::get_connection_id` and
`Endpoint::get_socket_addr_by_id` have been removed.
`Endpoint::get_connection_by_addr` and `Endpoint::get_connection_by_id`
can be used instead to get a `Connection`, from which the `id` or
`remote_address` can be retrieved.
  • Loading branch information
Chris Connelly authored and connec committed Sep 21, 2021
1 parent 8d68439 commit c2ab8a1
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/connection_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,12 +148,11 @@ struct Store<I: ConnId> {
id_gen: IdGen,
}

/// Unique key identifying a connection. Two connections will always have distict keys even if they
/// have the same socket address.
/// An application-defined identifier for a connection.
pub trait ConnId:
Clone + Copy + Eq + PartialEq + Ord + PartialOrd + Default + Send + Sync + 'static
{
/// Generate
/// Generate an ID for the given `SocketAddr`.
fn generate(socket_addr: &SocketAddr) -> Self;
}

Expand Down
5 changes: 5 additions & 0 deletions src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ impl<I: ConnId> Connection<I> {
Self { quic_conn, remover }
}

/// Get the ID under which the connection is stored in the pool.
pub fn id(&self) -> I {
self.remover.id()
}

/// Get the address of the connected peer.
pub fn remote_address(&self) -> SocketAddr {
self.quic_conn.remote_address()
Expand Down
12 changes: 6 additions & 6 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,20 +360,20 @@ impl<I: ConnId> Endpoint<I> {
}
}

/// Get the connection ID of an existing pooled connection with the provided socket address.
pub async fn get_connection_id(&self, addr: &SocketAddr) -> Option<I> {
/// Get the existing `Connection` for a `SocketAddr`.
pub async fn get_connection_by_addr(&self, addr: &SocketAddr) -> Option<Connection<I>> {
self.connection_pool
.get_by_addr(addr)
.await
.map(|(_, remover)| remover.id())
.map(|(connection, remover)| Connection::new(connection, remover))
}

/// Get the `SocketAddr` of an existing pooled connection with the provided connection ID.
pub async fn get_socket_addr_by_id(&self, id: &I) -> Option<SocketAddr> {
/// Get the existing `Connection` for the given ID.
pub async fn get_connection_by_id(&self, id: &I) -> Option<Connection<I>> {
self.connection_pool
.get_by_id(id)
.await
.map(|(_, remover)| *remover.remote_addr())
.map(|(connection, remover)| Connection::new(connection, remover))
}

/// Open a bi-directional stream with a given peer.
Expand Down

0 comments on commit c2ab8a1

Please sign in to comment.