mirror of
https://github.com/chatmail/core.git
synced 2026-05-22 08:16:32 +03:00
introduce a trigger_reconnect helper
This commit is contained in:
31
src/imap.rs
31
src/imap.rs
@@ -132,6 +132,10 @@ impl Imap {
|
|||||||
self.should_reconnect.load(Ordering::Relaxed)
|
self.should_reconnect.load(Ordering::Relaxed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn trigger_reconnect(&self) {
|
||||||
|
self.should_reconnect.store(true, Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
|
||||||
fn setup_handle_if_needed(&self, context: &Context) -> bool {
|
fn setup_handle_if_needed(&self, context: &Context) -> bool {
|
||||||
task::block_on(async move {
|
task::block_on(async move {
|
||||||
if self.config.read().await.imap_server.is_empty() {
|
if self.config.read().await.imap_server.is_empty() {
|
||||||
@@ -140,10 +144,8 @@ impl Imap {
|
|||||||
|
|
||||||
if self.should_reconnect() {
|
if self.should_reconnect() {
|
||||||
self.unsetup_handle(context).await;
|
self.unsetup_handle(context).await;
|
||||||
}
|
|
||||||
|
|
||||||
if self.is_connected().await {
|
|
||||||
self.should_reconnect.store(false, Ordering::Relaxed);
|
self.should_reconnect.store(false, Ordering::Relaxed);
|
||||||
|
} else if self.is_connected().await {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -373,7 +375,8 @@ impl Imap {
|
|||||||
|
|
||||||
pub fn fetch(&self, context: &Context) -> bool {
|
pub fn fetch(&self, context: &Context) -> bool {
|
||||||
task::block_on(async move {
|
task::block_on(async move {
|
||||||
if !self.is_connected().await || !context.sql.is_open() {
|
if !context.sql.is_open() {
|
||||||
|
// probably shutdown
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -462,7 +465,7 @@ impl Imap {
|
|||||||
);
|
);
|
||||||
|
|
||||||
self.config.write().await.selected_folder = None;
|
self.config.write().await.selected_folder = None;
|
||||||
self.should_reconnect.store(true, Ordering::Relaxed);
|
self.trigger_reconnect();
|
||||||
return ImapActionResult::Failed;
|
return ImapActionResult::Failed;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -550,7 +553,7 @@ impl Imap {
|
|||||||
match session.fetch(set, PREFETCH_FLAGS).await {
|
match session.fetch(set, PREFETCH_FLAGS).await {
|
||||||
Ok(list) => list,
|
Ok(list) => list,
|
||||||
Err(_err) => {
|
Err(_err) => {
|
||||||
self.should_reconnect.store(true, Ordering::Relaxed);
|
self.trigger_reconnect();
|
||||||
info!(
|
info!(
|
||||||
context,
|
context,
|
||||||
"No result returned for folder \"{}\".",
|
"No result returned for folder \"{}\".",
|
||||||
@@ -694,7 +697,7 @@ impl Imap {
|
|||||||
match session.uid_fetch(set, BODY_FLAGS).await {
|
match session.uid_fetch(set, BODY_FLAGS).await {
|
||||||
Ok(msgs) => msgs,
|
Ok(msgs) => msgs,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
self.should_reconnect.store(true, Ordering::Relaxed);
|
self.trigger_reconnect();
|
||||||
warn!(
|
warn!(
|
||||||
context,
|
context,
|
||||||
"Error on fetching message #{} from folder \"{}\"; retry={}; error={}.",
|
"Error on fetching message #{} from folder \"{}\"; retry={}; error={}.",
|
||||||
@@ -777,6 +780,10 @@ impl Imap {
|
|||||||
let timeout = Duration::from_secs(23 * 60);
|
let timeout = Duration::from_secs(23 * 60);
|
||||||
if let Some(session) = session {
|
if let Some(session) = session {
|
||||||
match session.idle() {
|
match session.idle() {
|
||||||
|
|
||||||
|
// BEWARE: If you change the Secure branch you
|
||||||
|
// typically also need to change the Insecure branch.
|
||||||
|
|
||||||
IdleHandle::Secure(mut handle) => {
|
IdleHandle::Secure(mut handle) => {
|
||||||
if let Err(err) = handle.init().await {
|
if let Err(err) = handle.init().await {
|
||||||
warn!(context, "Failed to establish IDLE connection: {:?}", err);
|
warn!(context, "Failed to establish IDLE connection: {:?}", err);
|
||||||
@@ -801,7 +808,11 @@ impl Imap {
|
|||||||
*self.session.lock().await = Some(Session::Secure(session));
|
*self.session.lock().await = Some(Session::Secure(session));
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
warn!(context, "Failed to close IMAP IDLE connection: {:?}", err);
|
// if we cannot terminate IDLE it probably
|
||||||
|
// means that we waited long (with idle_wait)
|
||||||
|
// but the network went away/changed
|
||||||
|
self.trigger_reconnect();
|
||||||
|
warn!(context, "Failed to terminate IMAP IDLE connection: {:?}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -830,6 +841,10 @@ impl Imap {
|
|||||||
*self.session.lock().await = Some(Session::Insecure(session));
|
*self.session.lock().await = Some(Session::Insecure(session));
|
||||||
}
|
}
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
// if we cannot terminate IDLE it probably
|
||||||
|
// means that we waited long (with idle_wait)
|
||||||
|
// but the network went away/changed
|
||||||
|
self.trigger_reconnect();
|
||||||
warn!(context, "Failed to close IMAP IDLE connection: {:?}", err);
|
warn!(context, "Failed to close IMAP IDLE connection: {:?}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user