forked from microsoft/react-native-code-push
-
Notifications
You must be signed in to change notification settings - Fork 15
Add retry logic to bundle download [Android] #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ofalvai
wants to merge
3
commits into
CodePushNext:master
Choose a base branch
from
ofalvai:push-tmpmromkllpp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
Examples/CodePushDemoAppNewArch/android/gradle/wrapper/gradle-wrapper.properties
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
android/app/src/main/java/com/microsoft/codepush/react/CodePushNetworkException.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package com.microsoft.codepush.react; | ||
|
|
||
| import java.io.EOFException; | ||
| import java.io.IOException; | ||
| import java.net.ConnectException; | ||
| import java.net.SocketException; | ||
| import java.net.SocketTimeoutException; | ||
| import java.net.UnknownHostException; | ||
| import java.util.zip.ZipException; | ||
|
|
||
| public class CodePushNetworkException extends IOException { | ||
| private final int httpStatusCode; | ||
|
|
||
| public CodePushNetworkException(String message, int httpStatusCode) { | ||
| super(message + " (HTTP " + httpStatusCode + ")"); | ||
| this.httpStatusCode = httpStatusCode; | ||
| } | ||
|
|
||
| public int getHttpStatusCode() { | ||
| return httpStatusCode; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
android/app/src/main/java/com/microsoft/codepush/react/RetryHelper.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package com.microsoft.codepush.react; | ||
|
|
||
| import java.io.EOFException; | ||
| import java.io.IOException; | ||
| import java.net.ConnectException; | ||
| import java.net.HttpURLConnection; | ||
| import java.net.SocketException; | ||
| import java.net.SocketTimeoutException; | ||
| import java.net.UnknownHostException; | ||
| import java.util.zip.ZipException; | ||
|
|
||
| public class RetryHelper { | ||
| private static final int MAX_RETRY_ATTEMPTS = 3; | ||
| private static final long BASE_RETRY_DELAY_MS = 1000; | ||
|
|
||
| public interface RetryableOperation { | ||
| void execute() throws IOException; | ||
| } | ||
|
|
||
| public static void executeWithRetry(RetryableOperation operation) throws IOException { | ||
| IOException lastException = null; | ||
|
|
||
| for (int attempt = 1; attempt <= MAX_RETRY_ATTEMPTS; attempt++) { | ||
| try { | ||
| operation.execute(); | ||
| return; | ||
| } catch (IOException e) { | ||
| lastException = e; | ||
|
|
||
| boolean shouldRetry = isRetryableException(e) && attempt < MAX_RETRY_ATTEMPTS; | ||
|
|
||
| if (shouldRetry) { | ||
| CodePushUtils.log("Download attempt " + attempt + " failed, retrying: " + e.getMessage()); | ||
|
|
||
| try { | ||
| // Exponential backoff | ||
| long delay = BASE_RETRY_DELAY_MS * (1L << (attempt - 1)); | ||
| Thread.sleep(delay); | ||
| } catch (InterruptedException ie) { | ||
| Thread.currentThread().interrupt(); | ||
| throw new IOException("Download interrupted", ie); | ||
| } | ||
| } else { | ||
| throw lastException; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| throw lastException; | ||
| } | ||
|
|
||
| private static boolean isRetryableException(IOException e) { | ||
| if (e instanceof CodePushNetworkException) { | ||
| CodePushNetworkException ne = (CodePushNetworkException) e; | ||
| int statusCode = ne.getHttpStatusCode(); | ||
| if (statusCode > 0) { | ||
| return statusCode >= 500 || statusCode == 408 || statusCode == 429; | ||
| } | ||
| } | ||
|
|
||
| return isRetryableError(e, 0); | ||
| } | ||
|
|
||
| private static boolean isRetryableError(Throwable e, int depth) { | ||
| // Prevent stack overflow in recursive calls | ||
| if (e == null || depth > 10) { | ||
| return false; | ||
| } | ||
|
|
||
| if (e instanceof SocketTimeoutException || | ||
| e instanceof ConnectException || | ||
| e instanceof UnknownHostException || | ||
| e instanceof SocketException || | ||
| e instanceof EOFException) { | ||
| return true; | ||
| } | ||
|
|
||
| return isRetryableError(e.getCause(), depth + 1); | ||
| } | ||
|
|
||
| public static void checkHttpResponse(HttpURLConnection connection) throws IOException { | ||
| int responseCode = connection.getResponseCode(); | ||
| if (responseCode >= 400) { | ||
| throw new CodePushNetworkException("HTTP error during download", responseCode); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| #Sun Aug 24 14:51:00 KST 2025 | ||
| distributionBase=GRADLE_USER_HOME | ||
| distributionPath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip | ||
| zipStoreBase=GRADLE_USER_HOME | ||
| zipStorePath=wrapper/dists | ||
| distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| var CodePushWrapper = require("../codePushWrapper.js"); | ||
|
|
||
| module.exports = { | ||
| startTest: function (testApp) { | ||
| CodePushWrapper.checkForUpdate(testApp, | ||
| function(remotePackage) { | ||
| if (remotePackage) { | ||
| // The download URL will be set to trigger retries (unreachable host, HTTP 500, etc.) | ||
| // RetryHelper will log retry attempts before eventually failing | ||
| CodePushWrapper.download(testApp, | ||
| function() { | ||
| }, | ||
| function(error) { | ||
| }, | ||
| remotePackage | ||
| ); | ||
| } else { | ||
| testApp.sendTestMessage("No update available for retry test"); | ||
| } | ||
| }, | ||
| function(error) { | ||
| testApp.sendTestMessage("Check for update failed: " + error.message); | ||
| } | ||
| ); | ||
| }, | ||
|
|
||
| getScenarioName: function () { | ||
| return "Retry Behavior Test"; | ||
| } | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason to set timeout as 30s and 60s? It seems to be too long for me 🤔