Initially suppress "Too many simultaneous connections"

pull/169/head
M66B 6 years ago
parent 19ce2783f1
commit 6a92eb68e4

@ -3199,6 +3199,7 @@ class Core {
} }
static class State { static class State {
private int backoff;
private ConnectionHelper.NetworkState networkState; private ConnectionHelper.NetworkState networkState;
private Thread thread; private Thread thread;
private Semaphore semaphore = new Semaphore(0); private Semaphore semaphore = new Semaphore(0);
@ -3217,6 +3218,14 @@ class Core {
return networkState; return networkState;
} }
void setBackoff(int value) {
this.backoff = value;
}
int getBackoff() {
return backoff;
}
void runnable(Runnable runnable, String name) { void runnable(Runnable runnable, String name) {
thread = new Thread(runnable, name); thread = new Thread(runnable, name);
thread.setPriority(THREAD_PRIORITY_BACKGROUND); thread.setPriority(THREAD_PRIORITY_BACKGROUND);
@ -3227,10 +3236,6 @@ class Core {
yield(); yield();
} }
void acquire() throws InterruptedException {
semaphore.acquire();
}
boolean acquire(long milliseconds) throws InterruptedException { boolean acquire(long milliseconds) throws InterruptedException {
return semaphore.tryAcquire(milliseconds, TimeUnit.MILLISECONDS); return semaphore.tryAcquire(milliseconds, TimeUnit.MILLISECONDS);
} }

@ -51,6 +51,8 @@ import androidx.preference.PreferenceManager;
import com.sun.mail.imap.IMAPFolder; import com.sun.mail.imap.IMAPFolder;
import org.w3c.dom.Text;
import java.net.SocketException; import java.net.SocketException;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -777,7 +779,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
final DB db = DB.getInstance(this); final DB db = DB.getInstance(this);
int backoff = CONNECT_BACKOFF_START; state.setBackoff(CONNECT_BACKOFF_START);
while (state.isRunning()) { while (state.isRunning()) {
state.reset(); state.reset();
Log.i(account.name + " run"); Log.i(account.name + " run");
@ -798,17 +800,24 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
iservice.setListener(new StoreListener() { iservice.setListener(new StoreListener() {
@Override @Override
public void notification(StoreEvent e) { public void notification(StoreEvent e) {
String message = e.getMessage();
if (TextUtils.isEmpty(message))
message = "?";
if (e.getMessageType() == StoreEvent.NOTICE) if (e.getMessageType() == StoreEvent.NOTICE)
EntityLog.log(ServiceSynchronize.this, account.name + " notice: " + e.getMessage()); EntityLog.log(ServiceSynchronize.this, account.name + " notice: " + message);
else else
try { try {
wlFolder.acquire(); wlFolder.acquire();
EntityLog.log(ServiceSynchronize.this, account.name + " " + e.getMessage()); EntityLog.log(ServiceSynchronize.this, account.name + " " + message);
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); if (state.getBackoff() > CONNECT_BACKOFF_MAX ||
nm.notify("alert:" + account.id, 1, !(message.startsWith("Maximum number of connections") /* Dovecot */ ||
getNotificationAlert(account.name, e.getMessage()).build()); message.startsWith("Too many simultaneous connections") /* Gmail */)) {
NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify("alert:" + account.id, 1,
getNotificationAlert(account.name, message).build());
}
} finally { } finally {
wlFolder.release(); wlFolder.release();
} }
@ -842,7 +851,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
long now = new Date().getTime(); long now = new Date().getTime();
long delayed = now - account.last_connected - account.poll_interval * 60 * 1000L; long delayed = now - account.last_connected - account.poll_interval * 60 * 1000L;
if (delayed > ACCOUNT_ERROR_AFTER * 60 * 1000L && backoff > BACKOFF_ERROR_AFTER) { if (delayed > ACCOUNT_ERROR_AFTER * 60 * 1000L && state.getBackoff() > BACKOFF_ERROR_AFTER) {
Log.i("Reporting sync error after=" + delayed); Log.i("Reporting sync error after=" + delayed);
Throwable warning = new Throwable( Throwable warning = new Throwable(
getString(R.string.title_no_sync, getString(R.string.title_no_sync,
@ -1197,7 +1206,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
EntityOperation.sync(this, folder.id, false); EntityOperation.sync(this, folder.id, false);
// Successfully connected: reset back off time // Successfully connected: reset back off time
backoff = CONNECT_BACKOFF_START; state.setBackoff(CONNECT_BACKOFF_START);
// Record successful connection // Record successful connection
account.last_connected = new Date().getTime(); account.last_connected = new Date().getTime();
@ -1286,6 +1295,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
if (state.isRunning()) { if (state.isRunning()) {
int backoff = state.getBackoff();
if (backoff <= CONNECT_BACKOFF_MAX) { if (backoff <= CONNECT_BACKOFF_MAX) {
// Short back-off period, keep device awake // Short back-off period, keep device awake
EntityLog.log(this, account.name + " backoff=" + backoff); EntityLog.log(this, account.name + " backoff=" + backoff);
@ -1319,7 +1329,7 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
} }
if (backoff <= CONNECT_BACKOFF_MAX) if (backoff <= CONNECT_BACKOFF_MAX)
backoff *= 2; state.setBackoff(backoff * 2);
} }
} }
} finally { } finally {

Loading…
Cancel
Save