From 2cf41581085d740e7fdc354f9ca72a91bf6549b7 Mon Sep 17 00:00:00 2001 From: M66B Date: Sat, 26 Dec 2020 13:09:47 +0100 Subject: [PATCH] Empty connection pool after 1 min idle --- .../java/com/sun/mail/imap/IMAPStore.java | 2 +- .../eu/faircode/email/ServiceSynchronize.java | 24 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/sun/mail/imap/IMAPStore.java b/app/src/main/java/com/sun/mail/imap/IMAPStore.java index 5fa720f3d2..078c1b9645 100644 --- a/app/src/main/java/com/sun/mail/imap/IMAPStore.java +++ b/app/src/main/java/com/sun/mail/imap/IMAPStore.java @@ -1395,7 +1395,7 @@ public class IMAPStore extends Store /** * Empty the connection pool. */ - private void emptyConnectionPool(boolean force) { + public void emptyConnectionPool(boolean force) { synchronized (pool) { for (int index = pool.authenticatedConnections.size() - 1; diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 5180d77a82..29717f0afe 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -111,6 +111,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences private MutableLiveData> liveAccountState = new MutableLiveData<>(); private MediatorState liveAccountNetworkState = new MediatorState(); + private static final long PURGE_DELAY = 60 * 1000L; // milliseconds private static final long QUIT_DELAY = 5 * 1000L; // milliseconds private static final long STILL_THERE_THRESHOLD = 3 * 60 * 1000L; // milliseconds static final int DEFAULT_POLL_INTERVAL = 0; // minutes @@ -1363,6 +1364,23 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences forced = true; + Runnable purge = new Runnable() { + @Override + public void run() { + executor.submit(new Runnable() { + @Override + public void run() { + try { + Log.i(account.name + " Empty connection pool"); + ((IMAPStore) istore).emptyConnectionPool(false); + } catch (Throwable ex) { + Log.e(ex); + } + } + }); + } + }; + Log.i(account.name + " observing operations"); getMainHandler().post(new Runnable() { @Override @@ -1392,6 +1410,12 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences } handling = all; + if (istore instanceof IMAPStore) { + getMainHandler().removeCallbacks(purge); + if (handling.size() == 0) + getMainHandler().postDelayed(purge, PURGE_DELAY); + } + for (Long fid : added.keySet()) { EntityFolder found = null; for (EntityFolder f : mapFolders.keySet())