From 9a2b0d167fefa1949a19dc5a75330f1b3e1e9f6f Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 2 Sep 2020 10:12:43 +0200 Subject: [PATCH] Added sync/download yield --- app/src/main/java/eu/faircode/email/Core.java | 34 ++++++++++++++++--- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/Core.java b/app/src/main/java/eu/faircode/email/Core.java index 4fd579c749..56f3c32dc3 100644 --- a/app/src/main/java/eu/faircode/email/Core.java +++ b/app/src/main/java/eu/faircode/email/Core.java @@ -135,6 +135,10 @@ class Core { private static final int SYNC_CHUNCK_SIZE = 200; private static final int SYNC_BATCH_SIZE = 20; private static final int DOWNLOAD_BATCH_SIZE = 20; + private static final int SYNC_YIELD_COUNT = 100; + private static final long SYNC_YIELD_DURATION = 1000; // milliseconds + private static final int DOWNLOAD_YIELD_COUNT = 25; + private static final long DOWNLOAD_YIELD_DURATION = 1000; // milliseconds private static final long YIELD_DURATION = 200L; // milliseconds private static final long JOIN_WAIT = 180 * 1000L; // milliseconds private static final long FUTURE_RECEIVED = 30 * 24 * 3600 * 1000L; // milliseconds @@ -2344,6 +2348,7 @@ class Core { fp.add(GmailFolder.FetchProfileItem.THRID); // Add/update local messages + int synced = 0; Long[] ids = new Long[imessages.length]; Log.i(folder.name + " add=" + imessages.length); for (int i = imessages.length - 1; i >= 0 && state.isRunning() && state.isRecoverable(); i -= SYNC_BATCH_SIZE) { @@ -2399,6 +2404,15 @@ class Core { false, download && initialize == 0, rules, state, stats); ids[from + j] = (message == null || message.ui_hide ? null : message.id); + + if (message != null && full.contains(isub[j])) + if ((++synced % SYNC_YIELD_COUNT) == 0) + try { + Log.i(folder.name + " yield synced=" + synced); + Thread.sleep(SYNC_YIELD_DURATION); + } catch (InterruptedException ex) { + Log.w(ex); + } } catch (MessageRemovedException ex) { Log.w(folder.name, ex); } catch (FolderClosedException ex) { @@ -2442,6 +2456,7 @@ class Core { db.folder().setFolderSyncState(folder.id, "downloading"); // Download messages/attachments + int downloaded = 0; Log.i(folder.name + " download=" + imessages.length); for (int i = imessages.length - 1; i >= 0 && state.isRunning() && state.isRecoverable(); i -= DOWNLOAD_BATCH_SIZE) { int from = Math.max(0, i - DOWNLOAD_BATCH_SIZE + 1); @@ -2461,12 +2476,19 @@ class Core { for (int j = isub.length - 1; j >= 0 && state.isRunning() && state.isRecoverable(); j--) try { if (ids[from + j] != null) - downloadMessage( + if (downloadMessage( context, account, folder, istore, ifolder, (MimeMessage) isub[j], ids[from + j], - state, stats); + state, stats)) + if ((++downloaded % DOWNLOAD_YIELD_COUNT) == 0) + try { + Log.i(folder.name + " yield downloaded=" + downloaded); + Thread.sleep(DOWNLOAD_YIELD_DURATION); + } catch (InterruptedException ex) { + Log.w(ex); + } } catch (FolderClosedException ex) { throw ex; } catch (Throwable ex) { @@ -3163,18 +3185,18 @@ class Core { } } - private static void downloadMessage( + private static boolean downloadMessage( Context context, EntityAccount account, EntityFolder folder, IMAPStore istore, IMAPFolder ifolder, MimeMessage imessage, long id, State state, SyncStats stats) throws MessagingException, IOException { if (state.getNetworkState().isRoaming()) - return; + return false; DB db = DB.getInstance(context); EntityMessage message = db.message().getMessage(id); if (message == null || message.ui_hide) - return; + return false; SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); long maxSize = prefs.getInt("download", MessageHelper.DEFAULT_DOWNLOAD_SIZE); @@ -3254,6 +3276,8 @@ class Core { db.attachment().setError(attachment.id, Log.formatThrowable(ex, false)); } } + + return fetch; } private static void reportEmptyMessage(Context context, State state, EntityAccount account, IMAPStore istore) {