Better handle service unavailable.

pull/2/head
Tom Hacohen 7 years ago
parent 3e5f3f5429
commit a460a52079

@ -37,5 +37,5 @@ public class Constants {
public static final Uri serviceUrl = Uri.parse((DEBUG_REMOTE_URL == null) ? "https://api.etesync.com/" : DEBUG_REMOTE_URL);
public static final int DEFAULT_SYNC_INTERVAL = 4 * 3600; // 4 hours
public static final long DEFAULT_RETRY_DELAY = 30 * 60; // 30 minutes
}

@ -37,6 +37,8 @@ abstract class BaseManager {
if (!response.isSuccessful()) {
switch (response.code()) {
case HttpURLConnection.HTTP_UNAVAILABLE:
throw new Exceptions.ServiceUnavailableException("Service unavailable", Long.valueOf(response.header("Retry-After", "0")));
case HttpURLConnection.HTTP_UNAUTHORIZED:
throw new Exceptions.UnauthorizedException("Failed to connect");
default:

@ -19,8 +19,13 @@ public class Exceptions {
}
public static class ServiceUnavailableException extends HttpException {
public long retryAfter;
public ServiceUnavailableException(String message) {
this(message, 0);
}
public ServiceUnavailableException(String message, long retryAfter) {
super(message);
this.retryAfter = retryAfter;
}
}

@ -29,6 +29,7 @@ import java.util.logging.Level;
import at.bitfire.davdroid.AccountSettings;
import at.bitfire.davdroid.App;
import at.bitfire.davdroid.Constants;
import at.bitfire.davdroid.GsonHelper;
import at.bitfire.davdroid.HttpClient;
import at.bitfire.davdroid.InvalidAccountException;
@ -183,11 +184,8 @@ abstract public class SyncManager {
syncResult.stats.numIoExceptions++;
} catch (Exceptions.ServiceUnavailableException e) {
Date retryAfter = null; // ((Exceptions.ServiceUnavailableException) e).retryAfter;
if (retryAfter != null) {
// how many seconds to wait? getTime() returns ms, so divide by 1000
// syncResult.delayUntil = (retryAfter.getTime() - new Date().getTime()) / 1000;
}
long retryAfter = (e.retryAfter > 0) ? e.retryAfter : Constants.DEFAULT_RETRY_DELAY;
syncResult.delayUntil = retryAfter;
} catch (InterruptedException e) {
return;
} catch (Exception | OutOfMemoryError e) {

Loading…
Cancel
Save