mirror of
https://github.com/chatmail/core.git
synced 2026-05-01 20:36:31 +03:00
cleanup interrupt and exit of imap idle
This commit is contained in:
@@ -48,11 +48,10 @@ impl Imap {
|
|||||||
|
|
||||||
self.select_folder(context, watch_folder.clone()).await?;
|
self.select_folder(context, watch_folder.clone()).await?;
|
||||||
|
|
||||||
let session = self.session.take();
|
|
||||||
let timeout = Duration::from_secs(23 * 60);
|
let timeout = Duration::from_secs(23 * 60);
|
||||||
let mut info = Default::default();
|
let mut info = Default::default();
|
||||||
|
|
||||||
if let Some(session) = session {
|
if let Some(session) = self.session.take() {
|
||||||
let mut handle = session.idle();
|
let mut handle = session.idle();
|
||||||
if let Err(err) = handle.init().await {
|
if let Err(err) = handle.init().await {
|
||||||
return Err(Error::IdleProtocolFailed(err));
|
return Err(Error::IdleProtocolFailed(err));
|
||||||
@@ -65,29 +64,20 @@ impl Imap {
|
|||||||
Interrupt(InterruptInfo),
|
Interrupt(InterruptInfo),
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.skip_next_idle_wait {
|
info!(context, "Idle entering wait-on-remote state");
|
||||||
// interrupt_idle has happened before we
|
let fut = idle_wait.map(|ev| ev.map(Event::IdleResponse)).race(async {
|
||||||
// provided self.interrupt
|
let probe_network = self.idle_interrupt.recv().await;
|
||||||
self.skip_next_idle_wait = false;
|
|
||||||
drop(idle_wait);
|
// cancel imap idle connection properly
|
||||||
drop(interrupt);
|
drop(interrupt);
|
||||||
|
|
||||||
info!(context, "Idle wait was skipped");
|
|
||||||
} else {
|
|
||||||
info!(context, "Idle entering wait-on-remote state");
|
|
||||||
let fut = idle_wait.map(|ev| ev.map(Event::IdleResponse)).race(
|
|
||||||
self.idle_interrupt.recv().map(|probe_network| {
|
|
||||||
Ok(Event::Interrupt(probe_network.unwrap_or_default()))
|
Ok(Event::Interrupt(probe_network.unwrap_or_default()))
|
||||||
}),
|
});
|
||||||
);
|
|
||||||
|
|
||||||
match fut.await {
|
match fut.await {
|
||||||
Ok(Event::IdleResponse(IdleResponse::NewData(_))) => {
|
Ok(Event::IdleResponse(IdleResponse::NewData(_))) => {
|
||||||
info!(context, "Idle has NewData");
|
info!(context, "Idle has NewData");
|
||||||
}
|
}
|
||||||
// TODO: idle_wait does not distinguish manual interrupts
|
|
||||||
// from Timeouts if we would know it's a Timeout we could bail
|
|
||||||
// directly and reconnect .
|
|
||||||
Ok(Event::IdleResponse(IdleResponse::Timeout)) => {
|
Ok(Event::IdleResponse(IdleResponse::Timeout)) => {
|
||||||
info!(context, "Idle-wait timeout or interruption");
|
info!(context, "Idle-wait timeout or interruption");
|
||||||
}
|
}
|
||||||
@@ -102,31 +92,15 @@ impl Imap {
|
|||||||
warn!(context, "Idle wait errored: {:?}", err);
|
warn!(context, "Idle wait errored: {:?}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// if we can't properly terminate the idle
|
let session = handle
|
||||||
// protocol let's break the connection.
|
|
||||||
let res = handle
|
|
||||||
.done()
|
.done()
|
||||||
.timeout(Duration::from_secs(15))
|
.timeout(Duration::from_secs(15))
|
||||||
.await
|
.await
|
||||||
.map_err(|err| {
|
.map_err(Error::IdleTimeout)??;
|
||||||
self.trigger_reconnect();
|
|
||||||
Error::IdleTimeout(err)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
match res {
|
|
||||||
Ok(session) => {
|
|
||||||
self.session = Some(Session { inner: session });
|
self.session = Some(Session { inner: session });
|
||||||
}
|
} else {
|
||||||
Err(err) => {
|
warn!(context, "Attempted to idle without a session");
|
||||||
// if we cannot terminate IDLE it probably
|
|
||||||
// means that we waited long (with idle_wait)
|
|
||||||
// but the network went away/changed
|
|
||||||
self.trigger_reconnect();
|
|
||||||
return Err(Error::IdleProtocolFailed(err));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(info)
|
Ok(info)
|
||||||
@@ -148,13 +122,6 @@ impl Imap {
|
|||||||
return self.idle_interrupt.recv().await.unwrap_or_default();
|
return self.idle_interrupt.recv().await.unwrap_or_default();
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut info: InterruptInfo = Default::default();
|
|
||||||
if self.skip_next_idle_wait {
|
|
||||||
// interrupt_idle has happened before we
|
|
||||||
// provided self.interrupt
|
|
||||||
self.skip_next_idle_wait = false;
|
|
||||||
info!(context, "fake-idle wait was skipped");
|
|
||||||
} else {
|
|
||||||
// check every minute if there are new messages
|
// check every minute if there are new messages
|
||||||
// TODO: grow sleep durations / make them more flexible
|
// TODO: grow sleep durations / make them more flexible
|
||||||
let mut interval = async_std::stream::interval(Duration::from_secs(60));
|
let mut interval = async_std::stream::interval(Duration::from_secs(60));
|
||||||
@@ -164,15 +131,16 @@ impl Imap {
|
|||||||
Interrupt(InterruptInfo),
|
Interrupt(InterruptInfo),
|
||||||
}
|
}
|
||||||
// loop until we are interrupted or if we fetched something
|
// loop until we are interrupted or if we fetched something
|
||||||
info =
|
let info = loop {
|
||||||
loop {
|
|
||||||
use futures::future::FutureExt;
|
use futures::future::FutureExt;
|
||||||
match interval
|
match interval
|
||||||
.next()
|
.next()
|
||||||
.map(|_| Event::Tick)
|
.map(|_| Event::Tick)
|
||||||
.race(self.idle_interrupt.recv().map(|probe_network| {
|
.race(
|
||||||
Event::Interrupt(probe_network.unwrap_or_default())
|
self.idle_interrupt
|
||||||
}))
|
.recv()
|
||||||
|
.map(|probe_network| Event::Interrupt(probe_network.unwrap_or_default())),
|
||||||
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Event::Tick => {
|
Event::Tick => {
|
||||||
@@ -214,7 +182,6 @@ impl Imap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
|
||||||
|
|
||||||
info!(
|
info!(
|
||||||
context,
|
context,
|
||||||
|
|||||||
@@ -117,7 +117,6 @@ pub struct Imap {
|
|||||||
session: Option<Session>,
|
session: Option<Session>,
|
||||||
connected: bool,
|
connected: bool,
|
||||||
interrupt: Option<stop_token::StopSource>,
|
interrupt: Option<stop_token::StopSource>,
|
||||||
skip_next_idle_wait: bool,
|
|
||||||
should_reconnect: bool,
|
should_reconnect: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,7 +190,6 @@ impl Imap {
|
|||||||
session: Default::default(),
|
session: Default::default(),
|
||||||
connected: Default::default(),
|
connected: Default::default(),
|
||||||
interrupt: Default::default(),
|
interrupt: Default::default(),
|
||||||
skip_next_idle_wait: Default::default(),
|
|
||||||
should_reconnect: Default::default(),
|
should_reconnect: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user