From 28659d161b0c0bbdd82ccdab1c10c3b543192b16 Mon Sep 17 00:00:00 2001 From: link2xt Date: Mon, 26 Aug 2024 19:25:33 +0000 Subject: [PATCH] Put connection delays into constant --- src/imap/client.rs | 12 ++++-------- src/net.rs | 9 +++++++++ src/smtp/connect.rs | 12 ++++-------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/imap/client.rs b/src/imap/client.rs index 7c819ed50d..7c334912fd 100644 --- a/src/imap/client.rs +++ b/src/imap/client.rs @@ -1,6 +1,5 @@ use std::net::SocketAddr; use std::ops::{Deref, DerefMut}; -use std::time::Duration; use anyhow::{format_err, Context as _, Result}; use async_imap::Client as ImapClient; @@ -16,7 +15,9 @@ use crate::login_param::{ConnectionCandidate, ConnectionSecurity}; use crate::net::dns::{lookup_host_with_cache, update_connect_timestamp}; use crate::net::session::SessionStream; use crate::net::tls::wrap_tls; -use crate::net::{connect_tcp_inner, connect_tls_inner, update_connection_history}; +use crate::net::{ + connect_tcp_inner, connect_tls_inner, update_connection_history, CONNECTION_DELAYS, +}; use crate::socks::Socks5Config; use crate::tools::time; @@ -206,12 +207,7 @@ impl Client { connection_attempt_set.spawn(fut); } - // Start second, third and fourth connection attempt 300 ms, 10 s and 15 s after the first. - for delay in [ - Duration::from_millis(300), - Duration::from_secs(10), - Duration::from_secs(15), - ] { + for delay in CONNECTION_DELAYS { delay_set.spawn(tokio::time::sleep(delay)); } diff --git a/src/net.rs b/src/net.rs index 38ef5cf121..dad174a2eb 100644 --- a/src/net.rs +++ b/src/net.rs @@ -37,6 +37,15 @@ pub(crate) const TRANSACTION_TIMEOUT: Duration = Duration::from_secs(300); /// TTL for caches in seconds. pub(crate) const CACHE_TTL: u64 = 30 * 24 * 60 * 60; +/// Start additional connection attempts after 300 ms, 5 s, 10 s and 15 s. +/// This way we can have up to 5 parallel connection attempts at the same time. +pub(crate) const CONNECTION_DELAYS: [Duration; 4] = [ + Duration::from_millis(300), + Duration::from_secs(5), + Duration::from_secs(10), + Duration::from_secs(15), +]; + /// Removes connection history entries after `CACHE_TTL`. pub(crate) async fn prune_connection_history(context: &Context) -> Result<()> { let now = time(); diff --git a/src/smtp/connect.rs b/src/smtp/connect.rs index 7082d87f27..a9c6378a5e 100644 --- a/src/smtp/connect.rs +++ b/src/smtp/connect.rs @@ -1,7 +1,6 @@ //! SMTP connection establishment. use std::net::SocketAddr; -use std::time::Duration; use anyhow::{bail, format_err, Context as _, Result}; use async_smtp::{SmtpClient, SmtpTransport}; @@ -13,7 +12,9 @@ use crate::login_param::{ConnectionCandidate, ConnectionSecurity}; use crate::net::dns::{lookup_host_with_cache, update_connect_timestamp}; use crate::net::session::SessionBufStream; use crate::net::tls::wrap_tls; -use crate::net::{connect_tcp_inner, connect_tls_inner, update_connection_history}; +use crate::net::{ + connect_tcp_inner, connect_tls_inner, update_connection_history, CONNECTION_DELAYS, +}; use crate::oauth2::get_oauth2_access_token; use crate::socks::Socks5Config; use crate::tools::time; @@ -177,12 +178,7 @@ async fn connect_stream( connection_attempt_set.spawn(fut); } - // Start second, third and fourth connection attempt 300 ms, 10 s and 15 s after the first. - for delay in [ - Duration::from_millis(300), - Duration::from_secs(10), - Duration::from_secs(15), - ] { + for delay in CONNECTION_DELAYS { delay_set.spawn(tokio::time::sleep(delay)); }