refactor: replace {IMAP,SMTP,HTTP}_TIMEOUT with a single constant

This change also increases HTTP timeout from 30 seconds to 60 seconds.
This commit is contained in:
link2xt
2024-07-29 01:28:55 +00:00
parent 40d355209b
commit 8ec4a8ad46
7 changed files with 43 additions and 85 deletions

View File

@@ -3,19 +3,14 @@
use anyhow::{Context as _, Result};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::str::FromStr;
use std::time::Duration;
use tokio::net::lookup_host;
use tokio::time::timeout;
use crate::context::Context;
use crate::tools::time;
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)))
async fn lookup_host_with_timeout(hostname: &str, port: u16) -> Result<Vec<SocketAddr>> {
let res = timeout(super::TIMEOUT, lookup_host((hostname, port)))
.await
.context("DNS lookup timeout")?
.context("DNS lookup failure")?;
@@ -66,11 +61,10 @@ pub(crate) async fn lookup_host_with_cache(
context: &Context,
hostname: &str,
port: u16,
timeout_val: Duration,
load_cache: bool,
) -> Result<Vec<SocketAddr>> {
let now = time();
let mut resolved_addrs = match lookup_host_with_timeout(hostname, port, timeout_val).await {
let mut resolved_addrs = match lookup_host_with_timeout(hostname, port).await {
Ok(res) => res,
Err(err) => {
warn!(

View File

@@ -1,7 +1,6 @@
//! # HTTP module.
use std::sync::Arc;
use std::time::Duration;
use anyhow::{anyhow, Result};
use mime::Mime;
@@ -11,8 +10,6 @@ use crate::context::Context;
use crate::net::lookup_host_with_cache;
use crate::socks::Socks5Config;
const HTTP_TIMEOUT: Duration = Duration::from_secs(30);
static LETSENCRYPT_ROOT: Lazy<reqwest::tls::Certificate> = Lazy::new(|| {
reqwest::tls::Certificate::from_der(include_bytes!(
"../../assets/root-certificates/letsencrypt/isrgrootx1.der"
@@ -122,8 +119,7 @@ impl reqwest::dns::Resolve for CustomResolver {
let port = 443; // Actual port does not matter.
let socket_addrs =
lookup_host_with_cache(&context, hostname.as_str(), port, HTTP_TIMEOUT, load_cache)
.await;
lookup_host_with_cache(&context, hostname.as_str(), port, load_cache).await;
match socket_addrs {
Ok(socket_addrs) => {
let addrs: reqwest::dns::Addrs = Box::new(socket_addrs.into_iter());
@@ -141,7 +137,7 @@ pub(crate) async fn get_client(context: &Context, load_cache: bool) -> Result<re
let resolver = Arc::new(CustomResolver::new(context.clone(), load_cache));
let builder = reqwest::ClientBuilder::new()
.timeout(HTTP_TIMEOUT)
.timeout(super::TIMEOUT)
.add_root_certificate(LETSENCRYPT_ROOT.clone())
.dns_resolver(resolver);