Merge pull request #189 from udoprog/fix-sync-resolve

Avoid synchronously resolving server address
This commit is contained in:
Aaron Weiss 2019-12-30 19:28:34 -05:00 committed by GitHub
commit a086e0913c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 11 deletions

View file

@ -103,7 +103,7 @@ impl Connection {
} }
let connector: tokio_tls::TlsConnector = builder.build()?.into(); let connector: tokio_tls::TlsConnector = builder.build()?.into();
let socket = TcpStream::connect(&config.socket_addr()?).await?; let socket = TcpStream::connect(config.to_socket_addrs()?).await?;
let stream = connector.connect(&domain, socket).await?; let stream = connector.connect(&domain, socket).await?;
let framed = IrcCodec::new(config.encoding())?.framed(stream); let framed = IrcCodec::new(config.encoding())?.framed(stream);
let transport = Transport::new(&config, framed, tx); let transport = Transport::new(&config, framed, tx);
@ -111,8 +111,7 @@ impl Connection {
Ok(Connection::Secured(transport)) Ok(Connection::Secured(transport))
} else { } else {
log::info!("Connecting to {}.", config.server()?); log::info!("Connecting to {}.", config.server()?);
let addr = config.socket_addr()?; let stream = TcpStream::connect(config.to_socket_addrs()?).await?;
let stream = TcpStream::connect(&addr).await?;
let framed = IrcCodec::new(config.encoding())?.framed(stream); let framed = IrcCodec::new(config.encoding())?.framed(stream);
let transport = Transport::new(&config, framed, tx); let transport = Transport::new(&config, framed, tx);

View file

@ -4,9 +4,9 @@ use std::{
collections::HashMap, collections::HashMap,
fs::File, fs::File,
io::prelude::*, io::prelude::*,
net::{SocketAddr, ToSocketAddrs},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
use tokio::net::ToSocketAddrs;
#[cfg(feature = "json")] #[cfg(feature = "json")]
use serde_json; use serde_json;
@ -420,13 +420,11 @@ impl Config {
.unwrap_or(if self.use_ssl() { 6697 } else { 6667 }) .unwrap_or(if self.use_ssl() { 6697 } else { 6667 })
} }
/// Gets the server and port as a `SocketAddr`. /// Return something that can be converted into a socket address by tokio.
/// This panics when server is not specified or the address is malformed. pub(crate) fn to_socket_addrs(&self) -> Result<impl ToSocketAddrs + '_> {
pub fn socket_addr(&self) -> Result<SocketAddr> { let server = self.server()?;
format!("{}:{}", self.server()?, self.port()) let port = self.port();
.to_socket_addrs() Ok((server, port))
.map(|mut i| i.next().unwrap())
.map_err(|e| e.into())
} }
/// Gets the server password specified in the configuration. /// Gets the server password specified in the configuration.