mirror of
https://github.com/chatmail/core.git
synced 2026-05-05 14:26:30 +03:00
Add DNS lookup timeout
This commit is contained in:
21
src/net.rs
21
src/net.rs
@@ -20,6 +20,18 @@ async fn connect_tcp_inner(addr: SocketAddr, timeout_val: Duration) -> Result<Tc
|
|||||||
Ok(tcp_stream)
|
Ok(tcp_stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn lookup_host_with_timeout(
|
||||||
|
hostname: &str,
|
||||||
|
port: u16,
|
||||||
|
timeout_val: Duration,
|
||||||
|
) -> Result<Vec<SocketAddr>> {
|
||||||
|
let res = timeout(timeout_val, lookup_host((hostname, port)))
|
||||||
|
.await
|
||||||
|
.context("DNS lookup timeout")?
|
||||||
|
.context("DNS lookup failure")?;
|
||||||
|
Ok(res.collect())
|
||||||
|
}
|
||||||
|
|
||||||
/// Looks up hostname and port using DNS and updates the address resolution cache.
|
/// Looks up hostname and port using DNS and updates the address resolution cache.
|
||||||
///
|
///
|
||||||
/// If `load_cache` is true, appends cached results not older than 30 days to the end.
|
/// If `load_cache` is true, appends cached results not older than 30 days to the end.
|
||||||
@@ -27,11 +39,12 @@ async fn lookup_host_with_cache(
|
|||||||
context: &Context,
|
context: &Context,
|
||||||
hostname: &str,
|
hostname: &str,
|
||||||
port: u16,
|
port: u16,
|
||||||
|
timeout_val: Duration,
|
||||||
load_cache: bool,
|
load_cache: bool,
|
||||||
) -> Result<Vec<SocketAddr>> {
|
) -> Result<Vec<SocketAddr>> {
|
||||||
let now = time();
|
let now = time();
|
||||||
let mut resolved_addrs: Vec<SocketAddr> = match lookup_host((hostname, port)).await {
|
let mut resolved_addrs = match lookup_host_with_timeout(hostname, port, timeout_val).await {
|
||||||
Ok(res) => res.collect(),
|
Ok(res) => res,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!(
|
warn!(
|
||||||
context,
|
context,
|
||||||
@@ -126,7 +139,9 @@ pub(crate) async fn connect_tcp(
|
|||||||
let mut tcp_stream = None;
|
let mut tcp_stream = None;
|
||||||
let mut last_error = None;
|
let mut last_error = None;
|
||||||
|
|
||||||
for resolved_addr in lookup_host_with_cache(context, host, port, load_cache).await? {
|
for resolved_addr in
|
||||||
|
lookup_host_with_cache(context, host, port, timeout_val, load_cache).await?
|
||||||
|
{
|
||||||
match connect_tcp_inner(resolved_addr, timeout_val).await {
|
match connect_tcp_inner(resolved_addr, timeout_val).await {
|
||||||
Ok(stream) => {
|
Ok(stream) => {
|
||||||
tcp_stream = Some(stream);
|
tcp_stream = Some(stream);
|
||||||
|
|||||||
Reference in New Issue
Block a user