parse ip eagerly

This commit is contained in:
Conrad Ludgate
2024-04-23 18:18:03 +01:00
parent f34134faaf
commit a12b338aac

View File

@@ -11,7 +11,12 @@ use crate::{
use futures::TryFutureExt;
use itertools::Itertools;
use pq_proto::StartupMessageParams;
use std::{io, net::SocketAddr, time::Duration};
use std::{
io,
net::{IpAddr, SocketAddr},
str::FromStr,
time::Duration,
};
use thiserror::Error;
use tokio::net::TcpStream;
use tokio_postgres::{
@@ -182,11 +187,15 @@ impl ConnCfg {
// wrap TcpStream::connect with timeout
let connect_with_timeout = |host, port| async move {
let addrs = dns
.resolve(host)
.await
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
.collect::<Vec<_>>();
let addrs = if let Ok(ip) = IpAddr::from_str(host) {
vec![ip]
} else {
dns.resolve(host)
.await
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?
.collect()
};
let timeout = timeout / addrs.len() as u32;
let mut last_err = None;