melib/imap: use SystemTime for IMAP server timeout
parent
d862e7bf53
commit
e518b3f16d
|
@ -56,7 +56,7 @@ use std::hash::Hasher;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::{Arc, Mutex, RwLock};
|
use std::sync::{Arc, Mutex, RwLock};
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, SystemTime};
|
||||||
|
|
||||||
pub type ImapNum = usize;
|
pub type ImapNum = usize;
|
||||||
pub type UID = ImapNum;
|
pub type UID = ImapNum;
|
||||||
|
@ -167,7 +167,7 @@ pub struct UIDStore {
|
||||||
reverse_modseq: Arc<Mutex<HashMap<MailboxHash, BTreeMap<ModSequence, EnvelopeHash>>>>,
|
reverse_modseq: Arc<Mutex<HashMap<MailboxHash, BTreeMap<ModSequence, EnvelopeHash>>>>,
|
||||||
highestmodseqs: Arc<Mutex<HashMap<MailboxHash, std::result::Result<ModSequence, ()>>>>,
|
highestmodseqs: Arc<Mutex<HashMap<MailboxHash, std::result::Result<ModSequence, ()>>>>,
|
||||||
mailboxes: Arc<FutureMutex<HashMap<MailboxHash, ImapMailbox>>>,
|
mailboxes: Arc<FutureMutex<HashMap<MailboxHash, ImapMailbox>>>,
|
||||||
is_online: Arc<Mutex<(Instant, Result<()>)>>,
|
is_online: Arc<Mutex<(SystemTime, Result<()>)>>,
|
||||||
event_consumer: BackendEventConsumer,
|
event_consumer: BackendEventConsumer,
|
||||||
timeout: Option<Duration>,
|
timeout: Option<Duration>,
|
||||||
}
|
}
|
||||||
|
@ -197,7 +197,7 @@ impl UIDStore {
|
||||||
mailboxes: Arc::new(FutureMutex::new(Default::default())),
|
mailboxes: Arc::new(FutureMutex::new(Default::default())),
|
||||||
tag_index: Arc::new(RwLock::new(Default::default())),
|
tag_index: Arc::new(RwLock::new(Default::default())),
|
||||||
is_online: Arc::new(Mutex::new((
|
is_online: Arc::new(Mutex::new((
|
||||||
Instant::now(),
|
SystemTime::now(),
|
||||||
Err(MeliError::new("Account is uninitialised.")),
|
Err(MeliError::new("Account is uninitialised.")),
|
||||||
))),
|
))),
|
||||||
event_consumer,
|
event_consumer,
|
||||||
|
|
|
@ -34,7 +34,9 @@ use std::future::Future;
|
||||||
use std::iter::FromIterator;
|
use std::iter::FromIterator;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant, SystemTime};
|
||||||
|
|
||||||
|
const IMAP_PROTOCOL_TIMEOUT: Duration = Duration::from_secs(60 * 28);
|
||||||
|
|
||||||
use super::protocol_parser;
|
use super::protocol_parser;
|
||||||
use super::{Capabilities, ImapServerConf, UIDStore};
|
use super::{Capabilities, ImapServerConf, UIDStore};
|
||||||
|
@ -540,8 +542,8 @@ impl ImapConnection {
|
||||||
|
|
||||||
pub fn connect<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
|
pub fn connect<'a>(&'a mut self) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
|
||||||
Box::pin(async move {
|
Box::pin(async move {
|
||||||
if let (instant, ref mut status @ Ok(())) = *self.uid_store.is_online.lock().unwrap() {
|
if let (time, ref mut status @ Ok(())) = *self.uid_store.is_online.lock().unwrap() {
|
||||||
if Instant::now().duration_since(instant) >= Duration::new(60 * 30, 0) {
|
if SystemTime::now().duration_since(time).unwrap_or_default() >= IMAP_PROTOCOL_TIMEOUT {
|
||||||
let err = MeliError::new("Connection timed out").set_kind(ErrorKind::Timeout);
|
let err = MeliError::new("Connection timed out").set_kind(ErrorKind::Timeout);
|
||||||
*status = Err(err.clone());
|
*status = Err(err.clone());
|
||||||
self.stream = Err(err);
|
self.stream = Err(err);
|
||||||
|
@ -569,7 +571,7 @@ impl ImapConnection {
|
||||||
if let Err(err) = new_stream.as_ref() {
|
if let Err(err) = new_stream.as_ref() {
|
||||||
self.uid_store.is_online.lock().unwrap().1 = Err(err.clone());
|
self.uid_store.is_online.lock().unwrap().1 = Err(err.clone());
|
||||||
} else {
|
} else {
|
||||||
*self.uid_store.is_online.lock().unwrap() = (Instant::now(), Ok(()));
|
*self.uid_store.is_online.lock().unwrap() = (SystemTime::now(), Ok(()));
|
||||||
}
|
}
|
||||||
let (capabilities, stream) = new_stream?;
|
let (capabilities, stream) = new_stream?;
|
||||||
self.stream = Ok(stream);
|
self.stream = Ok(stream);
|
||||||
|
@ -654,7 +656,7 @@ impl ImapConnection {
|
||||||
let mut response = Vec::new();
|
let mut response = Vec::new();
|
||||||
ret.clear();
|
ret.clear();
|
||||||
self.stream.as_mut()?.read_response(&mut response).await?;
|
self.stream.as_mut()?.read_response(&mut response).await?;
|
||||||
*self.uid_store.is_online.lock().unwrap() = (Instant::now(), Ok(()));
|
*self.uid_store.is_online.lock().unwrap() = (SystemTime::now(), Ok(()));
|
||||||
|
|
||||||
match self.server_conf.protocol {
|
match self.server_conf.protocol {
|
||||||
ImapProtocol::IMAP { .. } => {
|
ImapProtocol::IMAP { .. } => {
|
||||||
|
@ -756,7 +758,7 @@ impl ImapConnection {
|
||||||
}
|
}
|
||||||
Err(err)
|
Err(err)
|
||||||
} else {
|
} else {
|
||||||
*self.uid_store.is_online.lock().unwrap() = (Instant::now(), Ok(()));
|
*self.uid_store.is_online.lock().unwrap() = (SystemTime::now(), Ok(()));
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1052,7 +1054,7 @@ impl ImapBlockingConnection {
|
||||||
async fn read(
|
async fn read(
|
||||||
conn: &mut ImapBlockingConnection,
|
conn: &mut ImapBlockingConnection,
|
||||||
break_flag: &mut bool,
|
break_flag: &mut bool,
|
||||||
prev_failure: &mut Option<Instant>,
|
prev_failure: &mut Option<SystemTime>,
|
||||||
) -> Option<Vec<u8>> {
|
) -> Option<Vec<u8>> {
|
||||||
let ImapBlockingConnection {
|
let ImapBlockingConnection {
|
||||||
ref mut prev_res_length,
|
ref mut prev_res_length,
|
||||||
|
@ -1080,7 +1082,7 @@ async fn read(
|
||||||
debug!(&_err);
|
debug!(&_err);
|
||||||
*err = Some(Into::<MeliError>::into(_err).set_kind(crate::error::ErrorKind::Network));
|
*err = Some(Into::<MeliError>::into(_err).set_kind(crate::error::ErrorKind::Network));
|
||||||
*break_flag = true;
|
*break_flag = true;
|
||||||
*prev_failure = Some(Instant::now());
|
*prev_failure = Some(SystemTime::now());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
|
|
|
@ -29,7 +29,7 @@ use nom::{
|
||||||
};
|
};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::time::Instant;
|
use std::time::SystemTime;
|
||||||
|
|
||||||
pub fn managesieve_capabilities(input: &[u8]) -> Result<Vec<(&[u8], &[u8])>> {
|
pub fn managesieve_capabilities(input: &[u8]) -> Result<Vec<(&[u8], &[u8])>> {
|
||||||
let (_, ret) = separated_nonempty_list(
|
let (_, ret) = separated_nonempty_list(
|
||||||
|
@ -115,7 +115,7 @@ pub fn new_managesieve_connection(
|
||||||
};
|
};
|
||||||
let uid_store = Arc::new(UIDStore {
|
let uid_store = Arc::new(UIDStore {
|
||||||
is_online: Arc::new(Mutex::new((
|
is_online: Arc::new(Mutex::new((
|
||||||
Instant::now(),
|
SystemTime::now(),
|
||||||
Err(MeliError::new("Account is uninitialised.")),
|
Err(MeliError::new("Account is uninitialised.")),
|
||||||
))),
|
))),
|
||||||
..UIDStore::new(
|
..UIDStore::new(
|
||||||
|
|
Loading…
Reference in New Issue