Skip to content

Commit

Permalink
[android] Fix Native suspension, #2858
Browse files Browse the repository at this point in the history
  • Loading branch information
charlag committed May 21, 2021
1 parent 8b66994 commit 3611cc5
Showing 1 changed file with 42 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,14 @@ private MissedNotification downloadMissedNotification(@NonNull SseInfo sseInfo)
} catch (InterruptedException ignored) {
}
// tries are not decremented and we don't return, we just wait and try again.
} catch (TooManyRequestsException e) {
Log.d(TAG, "TooManyRequestsException when downloading missed notification, waiting " +
e.getRetryAfterSeconds() + "s");
try {
Thread.sleep(TimeUnit.SECONDS.toMillis(e.getRetryAfterSeconds()));
} catch (InterruptedException ignored) {
}
// tries are not decremented and we don't return, we just wait and try again.
} catch (ServerResponseException e) {
triesLeft--;
Log.w(TAG, e);
Expand Down Expand Up @@ -155,28 +163,37 @@ private MissedNotification executeMissedNotificationDownload(@NonNull SseInfo ss
}

private void handleResponseCode(HttpURLConnection urlConnection, int responseCode)
throws FileNotFoundException, ServerResponseException, ClientRequestException, ServiceUnavailableException {
throws FileNotFoundException, ServerResponseException, ClientRequestException, ServiceUnavailableException,
TooManyRequestsException {
if (responseCode == 404) {
throw new FileNotFoundException("Missed notification not found: " + 404);
} else if (responseCode == ServiceUnavailableException.CODE) {
String retryAfterHeader = urlConnection.getHeaderField("Retry-After");
if (retryAfterHeader == null) {
retryAfterHeader = urlConnection.getHeaderField("Suspension-Time");
}
int suspensionTime;
try {
suspensionTime = Integer.parseInt(retryAfterHeader);
} catch (NumberFormatException e) {
suspensionTime = 0;
}
int suspensionTime = extractSuspectionTime(urlConnection);
throw new ServiceUnavailableException(suspensionTime);
} else if (responseCode == TooManyRequestsException.CODE) {
int suspensionTime = extractSuspectionTime(urlConnection);
throw new TooManyRequestsException(suspensionTime);
} else if (400 <= responseCode && responseCode < 500) {
throw new ClientRequestException(responseCode);
} else if (500 <= responseCode && responseCode <= 600) {
throw new ServerResponseException(responseCode);
}
}

private int extractSuspectionTime(HttpURLConnection urlConnection) {
String retryAfterHeader = urlConnection.getHeaderField("Retry-After");
if (retryAfterHeader == null) {
retryAfterHeader = urlConnection.getHeaderField("Suspension-Time");
}
int suspensionTime;
try {
suspensionTime = Integer.parseInt(retryAfterHeader);
} catch (NumberFormatException e) {
suspensionTime = 0;
}
return suspensionTime;
}

private URL makeAlarmNotificationUrl(SseInfo sseInfo) throws MalformedURLException {
String customId = Utils.base64ToBase64Url(Utils.bytesToBase64(sseInfo.getPushIdentifier().getBytes(StandardCharsets.UTF_8)));
return new URL(sseInfo.getSseOrigin() + "/rest/sys/missednotification/" + customId);
Expand Down Expand Up @@ -227,6 +244,20 @@ static class ServerResponseException extends HttpException {
}
}

static class TooManyRequestsException extends HttpException {
static final int CODE = 429;
private final int retryAfterSeconds;

public TooManyRequestsException(int retryAfterSeconds) {
super(CODE);
this.retryAfterSeconds = retryAfterSeconds;
}

public int getRetryAfterSeconds() {
return retryAfterSeconds;
}
}

static class ServiceUnavailableException extends HttpException {
static final int CODE = 503;
private final int suspensionSeconds;
Expand Down

0 comments on commit 3611cc5

Please sign in to comment.