From ba8f1bfcfd60621ed90487f9584d306108914b0a Mon Sep 17 00:00:00 2001 From: iequidoo Date: Sat, 5 Aug 2023 23:01:14 -0300 Subject: [PATCH] feat: Grow sleep durations on errors in Imap::fake_idle() (#4424) --- src/imap/idle.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/imap/idle.rs b/src/imap/idle.rs index db7debe0d..c89aabe19 100644 --- a/src/imap/idle.rs +++ b/src/imap/idle.rs @@ -135,10 +135,8 @@ impl Imap { }; info!(context, "IMAP-fake-IDLEing folder={:?}", watch_folder); - // check every minute if there are new messages - // TODO: grow sleep durations / make them more flexible - let mut interval = tokio::time::interval(Duration::from_secs(60)); - + const TIMEOUT_INIT_MS: u64 = 60_000; + let mut timeout_ms: u64 = TIMEOUT_INIT_MS; enum Event { Tick, Interrupt, @@ -146,6 +144,12 @@ impl Imap { // loop until we are interrupted or if we fetched something loop { use futures::future::FutureExt; + use rand::Rng; + + let mut interval = tokio::time::interval(Duration::from_millis(timeout_ms)); + timeout_ms = timeout_ms + .saturating_add(rand::thread_rng().gen_range((timeout_ms / 2)..=timeout_ms)); + interval.tick().await; // The first tick completes immediately. match interval .tick() .map(|_| Event::Tick) @@ -188,6 +192,7 @@ impl Imap { { Ok(res) => { info!(context, "fetch_new_messages returned {:?}", res); + timeout_ms = TIMEOUT_INIT_MS; if res { break; }