imap: modify connection timeouts

memfd
Manos Pitsidianakis 2020-06-11 11:46:15 +03:00
parent 34d782f16f
commit 40f66f3333
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
2 changed files with 25 additions and 9 deletions

View File

@ -174,7 +174,7 @@ pub(self) fn try_lock<T>(
dur: Option<std::time::Duration>, dur: Option<std::time::Duration>,
) -> Result<std::sync::MutexGuard<T>> { ) -> Result<std::sync::MutexGuard<T>> {
let now = Instant::now(); let now = Instant::now();
while Instant::now().duration_since(now) <= dur.unwrap_or(std::time::Duration::from_millis(150)) while Instant::now().duration_since(now) <= dur.unwrap_or(std::time::Duration::from_millis(100))
{ {
if let Ok(guard) = connection.try_lock() { if let Ok(guard) = connection.try_lock() {
return Ok(guard); return Ok(guard);
@ -188,19 +188,23 @@ impl MailBackend for ImapType {
//if let Ok(mut g) = try_lock(&self.connection, None) { //if let Ok(mut g) = try_lock(&self.connection, None) {
// let _ = g.connect(); // let _ = g.connect();
//} //}
try_lock(&self.uid_store.is_online, None)?.1.clone() try_lock(
&self.uid_store.is_online,
Some(std::time::Duration::from_millis(12)),
)?
.1
.clone()
} }
fn connect(&mut self) { fn connect(&mut self) {
if self.is_online().is_err() { let connection = self.connection.clone();
if Instant::now().duration_since(self.uid_store.is_online.lock().unwrap().0) let _ = std::thread::Builder::new()
>= std::time::Duration::new(2, 0) .name(format!("{} connecting", self.account_name.as_str(),))
{ .spawn(move || {
if let Ok(mut g) = try_lock(&self.connection, None) { if let Ok(mut g) = try_lock(&connection, None) {
let _ = g.connect(); let _ = g.connect();
} }
} });
}
} }
fn get(&mut self, mailbox: &Mailbox) -> Async<Result<Vec<Envelope>>> { fn get(&mut self, mailbox: &Mailbox) -> Async<Result<Vec<Envelope>>> {

View File

@ -639,6 +639,7 @@ impl Iterator for ImapBlockingConnection {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
self.result.drain(0..self.prev_res_length); self.result.drain(0..self.prev_res_length);
self.prev_res_length = 0; self.prev_res_length = 0;
let mut prev_failure = None;
let ImapBlockingConnection { let ImapBlockingConnection {
ref mut prev_res_length, ref mut prev_res_length,
ref mut result, ref mut result,
@ -660,12 +661,23 @@ impl Iterator for ImapBlockingConnection {
*prev_res_length = pos + b"\r\n".len(); *prev_res_length = pos + b"\r\n".len();
return Some(result[0..*prev_res_length].to_vec()); return Some(result[0..*prev_res_length].to_vec());
} }
prev_failure = None;
} }
Err(e) Err(e)
if e.kind() == std::io::ErrorKind::WouldBlock if e.kind() == std::io::ErrorKind::WouldBlock
|| e.kind() == std::io::ErrorKind::Interrupted => || e.kind() == std::io::ErrorKind::Interrupted =>
{ {
debug!(&e); debug!(&e);
if let Some(prev_failure) = prev_failure.as_ref() {
if Instant::now().duration_since(*prev_failure)
>= std::time::Duration::new(60 * 5, 0)
{
*err = Some(e.to_string());
return None;
}
} else {
prev_failure = Some(Instant::now());
}
continue; continue;
} }
Err(e) => { Err(e) => {