only look up hostname once in the beginning

This commit is contained in:
Florian Stecker 2023-02-20 18:51:38 -05:00
parent 702a6ffb33
commit 26cdccee3c

View File

@ -2,12 +2,13 @@
use rustls::{OwnedTrustAnchor, ClientConfig, RootCertStore, ClientConnection}; use rustls::{OwnedTrustAnchor, ClientConfig, RootCertStore, ClientConnection};
use anyhow::{Result as AResult, bail}; use anyhow::{Result as AResult, bail};
use std::net::{TcpStream, ToSocketAddrs}; use std::net::{TcpStream, ToSocketAddrs, SocketAddr};
use std::io::{self, ErrorKind, Read, Write, Error as IOError}; use std::io::{self, ErrorKind, Read, Write, Error as IOError};
use std::process::Command; use std::process::Command;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::cell::RefCell;
use std::thread; use std::thread;
use clap::Parser; use clap::Parser;
@ -22,6 +23,10 @@ pub struct Cli {
#[arg(long, default_value_t = 993)] #[arg(long, default_value_t = 993)]
port: u16, port: u16,
// the resolved address(es)
#[arg(skip)]
addrs: RefCell<Vec<SocketAddr>>,
/// IMAP user name /// IMAP user name
#[arg(short, long)] #[arg(short, long)]
username: String, username: String,
@ -46,7 +51,7 @@ pub struct Cli {
#[derive(Debug)] #[derive(Debug)]
struct Status { struct Status {
connected: bool, connected: bool,
last_run: SystemTime last_run: SystemTime,
} }
const CONNECTION_LOST_ERRORS: &[ErrorKind] = &[ const CONNECTION_LOST_ERRORS: &[ErrorKind] = &[
@ -162,7 +167,11 @@ pub fn run() -> AResult<()> {
time_to_reconnect = u64::min(time_to_reconnect*2, 1800); time_to_reconnect = u64::min(time_to_reconnect*2, 1800);
if cli.verbose > 0 {
println!("Error: {:?}", err);
}
println!("Cannot connect currently, retrying in {time_to_reconnect} seconds"); println!("Cannot connect currently, retrying in {time_to_reconnect} seconds");
thread::sleep(Duration::from_secs(time_to_reconnect)); thread::sleep(Duration::from_secs(time_to_reconnect));
continue; continue;
@ -203,10 +212,16 @@ pub fn connect_and_idle<F: Fn(), G: Fn()>(cli: &Cli, connected_callback: F, mail
let mut tls_client = ClientConnection::new( let mut tls_client = ClientConnection::new(
Arc::new(tls_config), Arc::new(tls_config),
cli.server.as_str().try_into().unwrap())?; cli.server.as_str().try_into().unwrap())?;
let addrs = (cli.server.as_str(), cli.port)
.to_socket_addrs() let mut addrs = cli.addrs.borrow_mut();
.map_err(|e|io::Error::new(ErrorKind::NotConnected, e.to_string()))? if addrs.is_empty() {
.collect::<Vec<_>>(); addrs.extend(
(cli.server.as_str(), cli.port)
.to_socket_addrs()
.map_err(|e|io::Error::new(ErrorKind::NotConnected, e.to_string()))?
);
}
let mut socket = TcpStream::connect(addrs.as_slice())?; let mut socket = TcpStream::connect(addrs.as_slice())?;
let mut state = ImapState::Unauthenticated; let mut state = ImapState::Unauthenticated;