Skip to content

Commit

Permalink
address: don't block async runtime for dns lookup
Browse files Browse the repository at this point in the history
Stop blocking the async runtime for dns lookup and resolution by moving
this blocking work to a blocking task via `tokio::task::spawn_blocking`.
  • Loading branch information
bmwill committed Aug 28, 2024
1 parent 98c0d74 commit 4f0bc2a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
20 changes: 9 additions & 11 deletions crates/anemo/src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
config::EndpointConfig, connection::Connection, types::Address, ConnectionOrigin, PeerId,
Result,
};
use crate::{config::EndpointConfig, connection::Connection, ConnectionOrigin, PeerId, Result};
use std::sync::Arc;
use std::time::Duration;
use std::{
Expand Down Expand Up @@ -47,18 +44,21 @@ impl Endpoint {
}

#[cfg(test)]
fn new_with_address<A: Into<Address>>(config: EndpointConfig, addr: A) -> Result<Self> {
fn new_with_address<A: Into<crate::types::Address>>(
config: EndpointConfig,
addr: A,
) -> Result<Self> {
let socket = std::net::UdpSocket::bind(addr.into())?;
Self::new(config, socket)
}

pub fn connect(&self, address: Address) -> Result<Connecting> {
pub fn connect(&self, address: SocketAddr) -> Result<Connecting> {
self.connect_with_client_config(self.config.client_config().clone(), address)
}

pub fn connect_with_expected_peer_id(
&self,
address: Address,
address: SocketAddr,
peer_id: PeerId,
) -> Result<Connecting> {
let config = self
Expand All @@ -70,12 +70,10 @@ impl Endpoint {
fn connect_with_client_config(
&self,
config: quinn::ClientConfig,
address: Address,
address: SocketAddr,
) -> Result<Connecting> {
let addr = address.resolve()?;

self.inner
.connect_with(config, addr, self.config.server_name())
.connect_with(config, address, self.config.server_name())
.map_err(Into::into)
.map(Connecting::new_outbound)
}
Expand Down
25 changes: 12 additions & 13 deletions crates/anemo/src/network/connection_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,33 +429,32 @@ impl ConnectionManager {
peer_id: Option<PeerId>,
oneshot: oneshot::Sender<Result<PeerId>>,
) {
let target_address = address.clone();
let maybe_connecting = if let Some(peer_id) = peer_id {
self.endpoint
.connect_with_expected_peer_id(address, peer_id)
} else {
self.endpoint.connect(address)
};
self.pending_connections.spawn(Self::dial_peer_task(
maybe_connecting,
target_address,
self.endpoint.clone(),
address,
peer_id,
oneshot,
self.config.clone(),
));
}

// TODO maybe look at cloning the endpoint so that we can try multiple addresses in the event
// Address resolves to multiple ips.
// TODO maybe look at trying all addresses that are resolved vs just the first one.
async fn dial_peer_task(
maybe_connecting: Result<Connecting>,
endpoint: Arc<Endpoint>,
target_address: Address,
peer_id: Option<PeerId>,
oneshot: oneshot::Sender<Result<PeerId>>,
config: Arc<Config>,
) -> ConnectingOutput {
let fut = async {
let connection = maybe_connecting?.await?;
let socket_addr = target_address.resolve().await?;

let connection = if let Some(peer_id) = peer_id {
endpoint.connect_with_expected_peer_id(socket_addr, peer_id)
} else {
endpoint.connect(socket_addr)
}?
.await?;

super::wire::handshake(connection).await
};
Expand Down
10 changes: 9 additions & 1 deletion crates/anemo/src/types/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ pub enum Address {
}

impl Address {
pub(crate) fn resolve(&self) -> std::io::Result<std::net::SocketAddr> {
pub(crate) async fn resolve(&self) -> std::io::Result<std::net::SocketAddr> {
let address = self.to_owned();

tokio::task::spawn_blocking(move || address.resolve_blocking())
.await
.unwrap()
}

fn resolve_blocking(&self) -> std::io::Result<std::net::SocketAddr> {
std::net::ToSocketAddrs::to_socket_addrs(self).and_then(|mut iter| {
iter.next().ok_or_else(|| {
std::io::Error::new(std::io::ErrorKind::NotFound, "unable to resolve host")
Expand Down

0 comments on commit 4f0bc2a

Please sign in to comment.