Added operation yield

This commit is contained in:
M66B 2022-02-01 12:58:42 +01:00
parent e86436898b
commit 12be9e6596
1 changed files with 30 additions and 5 deletions

View File

@ -137,6 +137,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
private static final int FAST_FAIL_COUNT = 3;
private static final int FETCH_YIELD_DURATION = 50; // milliseconds
private static final long WATCHDOG_INTERVAL = 60 * 60 * 1000L; // milliseconds
private static final long OPS_YIELD_INTERVAL = 15 * 1000L; // milliseconds
private static final long OPS_YIELD_DURATION = 1000L; // milliseconds
private static final String ACTION_NEW_MESSAGE_COUNT = BuildConfig.APPLICATION_ID + ".NEW_MESSAGE_COUNT";
@ -1817,6 +1819,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
cowner.value.start();
db.operation().liveOperations(account.id).observe(cowner.value, new Observer<List<TupleOperationEx>>() {
private Long last = null;
private long idle = 0, busy = 0;
private List<Long> handling = new ArrayList<>();
private final Map<TupleOperationEx.PartitionKey, List<TupleOperationEx>> partitions = new HashMap<>();
@ -1983,11 +1987,32 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
Log.i(account.name + " folder " + folder.name + " flags=" + ifolder.getPermanentFlags());
}
Core.processOperations(ServiceSynchronize.this,
account, folder,
partition,
iservice, ifolder,
state, serial);
long start = new Date().getTime();
try {
Core.processOperations(ServiceSynchronize.this,
account, folder,
partition,
iservice, ifolder,
state, serial);
} finally {
long end = new Date().getTime();
if (last != null)
idle += (start - last);
last = end;
busy += (end - start);
if (busy + idle > OPS_YIELD_INTERVAL) {
long wait = (OPS_YIELD_DURATION - idle);
Log.i(account.name + " ops idle wait=" + wait);
if (wait > 0)
try {
Thread.sleep(wait);
} catch (InterruptedException ex) {
Log.w(ex);
}
idle = 0;
busy = 0;
}
}
} catch (Throwable ex) {
Log.e(folder.name, ex);