Skip to content

Commit

Permalink
Put connection delays into constant
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Aug 26, 2024
1 parent 870715f commit 28659d1
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
12 changes: 4 additions & 8 deletions src/imap/client.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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));
}

Expand Down
9 changes: 9 additions & 0 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 4 additions & 8 deletions src/smtp/connect.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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;
Expand Down Expand Up @@ -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));
}

Expand Down

0 comments on commit 28659d1

Please sign in to comment.