Avoid synchronously resolving server address

This commit is contained in:
John-John Tedro 2019-12-29 19:08:31 +01:00
parent 71a703c762
commit b08e2a89ae
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 socket = TcpStream::connect(&config.socket_addr()?).await?;
let socket = TcpStream::connect(config.to_socket_addrs()?).await?;
let stream = connector.connect(&domain, socket).await?;
let framed = IrcCodec::new(config.encoding())?.framed(stream);
let transport = Transport::new(&config, framed, tx);
@ -111,8 +111,7 @@ impl Connection {
Ok(Connection::Secured(transport))
} else {
log::info!("Connecting to {}.", config.server()?);
let addr = config.socket_addr()?;
let stream = TcpStream::connect(&addr).await?;
let stream = TcpStream::connect(config.to_socket_addrs()?).await?;
let framed = IrcCodec::new(config.encoding())?.framed(stream);
let transport = Transport::new(&config, framed, tx);

View file

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