Skip to content

Commit 937fc85

Browse files
authored
Allow setting of max retries (#782)
* Remove ParsePlugins abstraction to Android, since that is our only target * Remove obsolete setting of system properties * Remove references to enabling/disable retry on command since all are retry enabled * Use max retries from configuration * Use the same default max retries as ParseRequest
1 parent c1fc653 commit 937fc85

17 files changed

+96
-168
lines changed

Parse/src/main/java/com/parse/CacheQueryController.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public <T extends ParseObject> Task<List<T>> findAsync(
3333
final String sessionToken = user != null ? user.getSessionToken() : null;
3434
CommandDelegate<List<T>> callbacks = new CommandDelegate<List<T>>() {
3535
@Override
36-
public Task<List<T>> runOnNetworkAsync(boolean retry) {
37-
return networkController.findAsync(state, sessionToken, retry, cancellationToken);
36+
public Task<List<T>> runOnNetworkAsync() {
37+
return networkController.findAsync(state, sessionToken, cancellationToken);
3838
}
3939

4040
@Override
@@ -53,8 +53,8 @@ public <T extends ParseObject> Task<Integer> countAsync(
5353
final String sessionToken = user != null ? user.getSessionToken() : null;
5454
CommandDelegate<Integer> callbacks = new CommandDelegate<Integer>() {
5555
@Override
56-
public Task<Integer> runOnNetworkAsync(boolean retry) {
57-
return networkController.countAsync(state, sessionToken, retry, cancellationToken);
56+
public Task<Integer> runOnNetworkAsync() {
57+
return networkController.countAsync(state, sessionToken, cancellationToken);
5858
}
5959

6060
@Override
@@ -124,7 +124,7 @@ private <TResult> Task<TResult> runCommandWithPolicyAsync(final CommandDelegate<
124124
switch (policy) {
125125
case IGNORE_CACHE:
126126
case NETWORK_ONLY:
127-
return c.runOnNetworkAsync(true);
127+
return c.runOnNetworkAsync();
128128
case CACHE_ONLY:
129129
return c.runFromCacheAsync();
130130
case CACHE_ELSE_NETWORK:
@@ -133,13 +133,13 @@ private <TResult> Task<TResult> runCommandWithPolicyAsync(final CommandDelegate<
133133
@Override
134134
public Task<TResult> then(Task<TResult> task) throws Exception {
135135
if (task.getError() instanceof ParseException) {
136-
return c.runOnNetworkAsync(true);
136+
return c.runOnNetworkAsync();
137137
}
138138
return task;
139139
}
140140
});
141141
case NETWORK_ELSE_CACHE:
142-
return c.runOnNetworkAsync(false).continueWithTask(new Continuation<TResult, Task<TResult>>() {
142+
return c.runOnNetworkAsync().continueWithTask(new Continuation<TResult, Task<TResult>>() {
143143
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
144144
@Override
145145
public Task<TResult> then(Task<TResult> task) throws Exception {
@@ -167,7 +167,7 @@ public Task<TResult> then(Task<TResult> task) throws Exception {
167167
*/
168168
private interface CommandDelegate<T> {
169169
// Fetches data from the network.
170-
Task<T> runOnNetworkAsync(boolean retry);
170+
Task<T> runOnNetworkAsync();
171171

172172
// Fetches data from the cache.
173173
Task<T> runFromCacheAsync();

Parse/src/main/java/com/parse/NetworkObjectController.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public Task<ParseObject.State> fetchAsync(
3333
state.objectId(),
3434
state.className(),
3535
sessionToken);
36-
command.enableRetrying();
3736

3837
return command.executeAsync(client).onSuccess(new Continuation<JSONObject, ParseObject.State>() {
3938
@Override
@@ -64,7 +63,6 @@ public Task<ParseObject.State> saveAsync(
6463
state,
6564
objectJSON,
6665
sessionToken);
67-
command.enableRetrying();
6866
return command.executeAsync(client).onSuccess(new Continuation<JSONObject, ParseObject.State>() {
6967
@Override
7068
public ParseObject.State then(Task<JSONObject> task) throws Exception {
@@ -124,7 +122,6 @@ public ParseObject.State then(Task<JSONObject> task) throws Exception {
124122
public Task<Void> deleteAsync(ParseObject.State state, String sessionToken) {
125123
ParseRESTObjectCommand command = ParseRESTObjectCommand.deleteObjectCommand(
126124
state, sessionToken);
127-
command.enableRetrying();
128125

129126
return command.executeAsync(client).makeVoid();
130127
}
@@ -139,7 +136,6 @@ public List<Task<Void>> deleteAllAsync(
139136
ParseObject.State state = states.get(i);
140137
ParseRESTObjectCommand command = ParseRESTObjectCommand.deleteObjectCommand(
141138
state, sessionToken);
142-
command.enableRetrying();
143139
commands.add(command);
144140
}
145141

Parse/src/main/java/com/parse/NetworkQueryController.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@ public NetworkQueryController(ParseHttpClient restClient) {
3232
public <T extends ParseObject> Task<List<T>> findAsync(
3333
ParseQuery.State<T> state, ParseUser user, Task<Void> cancellationToken) {
3434
String sessionToken = user != null ? user.getSessionToken() : null;
35-
return findAsync(state, sessionToken, true, cancellationToken);
35+
return findAsync(state, sessionToken, cancellationToken);
3636
}
3737

3838
@Override
3939
public <T extends ParseObject> Task<Integer> countAsync(
4040
ParseQuery.State<T> state, ParseUser user, Task<Void> cancellationToken) {
4141
String sessionToken = user != null ? user.getSessionToken() : null;
42-
return countAsync(state, sessionToken, true, cancellationToken);
42+
return countAsync(state, sessionToken, cancellationToken);
4343
}
4444

4545
/**
@@ -50,14 +50,10 @@ public <T extends ParseObject> Task<Integer> countAsync(
5050
/* package */ <T extends ParseObject> Task<List<T>> findAsync(
5151
final ParseQuery.State<T> state,
5252
String sessionToken,
53-
boolean shouldRetry,
5453
Task<Void> ct) {
5554
final long queryStart = System.nanoTime();
5655

5756
final ParseRESTCommand command = ParseRESTQueryCommand.findCommand(state, sessionToken);
58-
if (shouldRetry) {
59-
command.enableRetrying();
60-
}
6157

6258
final long querySent = System.nanoTime();
6359
return command.executeAsync(restClient, ct).onSuccess(new Continuation<JSONObject, List<T>>() {
@@ -94,12 +90,8 @@ public List<T> then(Task<JSONObject> task) throws Exception {
9490
/* package */ <T extends ParseObject> Task<Integer> countAsync(
9591
final ParseQuery.State<T> state,
9692
String sessionToken,
97-
boolean shouldRetry,
9893
Task<Void> ct) {
9994
final ParseRESTCommand command = ParseRESTQueryCommand.countCommand(state, sessionToken);
100-
if (shouldRetry) {
101-
command.enableRetrying();
102-
}
10395

10496
return command.executeAsync(restClient, ct).onSuccessTask(new Continuation<JSONObject, Task<JSONObject>>() {
10597
@Override

Parse/src/main/java/com/parse/Parse.java

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
public class Parse {
3737
private static final String TAG = "com.parse.Parse";
3838

39+
private static final int DEFAULT_MAX_RETRIES = ParseRequest.DEFAULT_MAX_RETRIES;
40+
3941
/**
4042
* Represents an opaque configuration for the {@code Parse} SDK configuration.
4143
*/
@@ -50,6 +52,7 @@ public static final class Builder {
5052
private String server;
5153
private boolean localDataStoreEnabled;
5254
private OkHttpClient.Builder clientBuilder;
55+
private int maxRetries = DEFAULT_MAX_RETRIES;
5356

5457
/**
5558
* Initialize a bulider with a given context.
@@ -179,6 +182,18 @@ public Builder clientBuilder(OkHttpClient.Builder builder) {
179182
return this;
180183
}
181184

185+
/**
186+
* Set the max number of times to retry Parse operations before deeming them a failure
187+
* <p>
188+
*
189+
* @param maxRetries The maximum number of times to retry. <=0 to never retry commands
190+
* @return The same builder, for easy chaining.
191+
*/
192+
public Builder maxRetries(int maxRetries) {
193+
this.maxRetries = maxRetries;
194+
return this;
195+
}
196+
182197
/**
183198
* Construct this builder into a concrete {@code Configuration} instance.
184199
*
@@ -195,6 +210,7 @@ public Configuration build() {
195210
final String server;
196211
final boolean localDataStoreEnabled;
197212
final OkHttpClient.Builder clientBuilder;
213+
final int maxRetries;
198214

199215

200216
private Configuration(Builder builder) {
@@ -204,6 +220,7 @@ private Configuration(Builder builder) {
204220
this.server = builder.server;
205221
this.localDataStoreEnabled = builder.localDataStoreEnabled;
206222
this.clientBuilder = builder.clientBuilder;
223+
this.maxRetries = builder.maxRetries;
207224
}
208225
}
209226

@@ -369,19 +386,14 @@ public static void initialize(Configuration configuration) {
369386
// isLocalDataStoreEnabled() to perform additional behavior.
370387
isLocalDatastoreEnabled = configuration.localDataStoreEnabled;
371388

372-
ParsePlugins.Android.initialize(configuration.context, configuration);
389+
ParsePlugins.initialize(configuration.context, configuration);
373390

374391
try {
375392
ParseRESTCommand.server = new URL(configuration.server);
376393
} catch (MalformedURLException ex) {
377394
throw new RuntimeException(ex);
378395
}
379396

380-
Context applicationContext = configuration.context.getApplicationContext();
381-
382-
ParseHttpClient.setKeepAlive(true);
383-
ParseHttpClient.setMaxConnections(20);
384-
385397
ParseObject.registerParseSubclasses();
386398

387399
if (configuration.localDataStoreEnabled) {
@@ -459,7 +471,7 @@ static boolean isInitialized() {
459471

460472
static Context getApplicationContext() {
461473
checkContext();
462-
return ParsePlugins.Android.get().applicationContext();
474+
return ParsePlugins.get().applicationContext();
463475
}
464476

465477
/**
@@ -580,7 +592,7 @@ static void checkCacheApplicationId() {
580592
* processing any commands already stored in the on-disk queue.
581593
*/
582594
static ParseEventuallyQueue getEventuallyQueue() {
583-
Context context = ParsePlugins.Android.get().applicationContext();
595+
Context context = ParsePlugins.get().applicationContext();
584596
return getEventuallyQueue(context);
585597
}
586598

@@ -621,7 +633,7 @@ static void checkInit() {
621633
}
622634

623635
static void checkContext() {
624-
if (ParsePlugins.Android.get().applicationContext() == null) {
636+
if (ParsePlugins.get().applicationContext() == null) {
625637
throw new RuntimeException("applicationContext is null. "
626638
+ "You must call Parse.initialize(Context)"
627639
+ " before using the Parse library.");

Parse/src/main/java/com/parse/ParseConfigController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ public ParseConfigController(ParseHttpClient restClient,
2929

3030
public Task<ParseConfig> getAsync(String sessionToken) {
3131
final ParseRESTCommand command = ParseRESTConfigCommand.fetchConfigCommand(sessionToken);
32-
command.enableRetrying();
3332
return command.executeAsync(restClient).onSuccessTask(new Continuation<JSONObject, Task<ParseConfig>>() {
3433
@Override
3534
public Task<ParseConfig> then(Task<JSONObject> task) throws Exception {

Parse/src/main/java/com/parse/ParseFileController.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ public Task<ParseFile.State> saveAsync(
9898
.contentType(state.mimeType())
9999
.sessionToken(sessionToken)
100100
.build();
101-
command.enableRetrying();
102101

103102
return command.executeAsync(
104103
restClient,
@@ -145,7 +144,6 @@ public Task<ParseFile.State> saveAsync(
145144
.contentType(state.mimeType())
146145
.sessionToken(sessionToken)
147146
.build();
148-
command.enableRetrying();
149147

150148
return command.executeAsync(
151149
restClient,

Parse/src/main/java/com/parse/ParseHttpClient.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,6 @@ static ParseHttpClient createClient(@Nullable OkHttpClient.Builder builder) {
3838
return new ParseHttpClient(builder);
3939
}
4040

41-
private static final String MAX_CONNECTIONS_PROPERTY_NAME = "http.maxConnections";
42-
private static final String KEEP_ALIVE_PROPERTY_NAME = "http.keepAlive";
43-
44-
static void setMaxConnections(int maxConnections) {
45-
if (maxConnections <= 0) {
46-
throw new IllegalArgumentException("Max connections should be large than 0");
47-
}
48-
System.setProperty(MAX_CONNECTIONS_PROPERTY_NAME, String.valueOf(maxConnections));
49-
}
50-
51-
static void setKeepAlive(boolean isKeepAlive) {
52-
System.setProperty(KEEP_ALIVE_PROPERTY_NAME, String.valueOf(isKeepAlive));
53-
}
54-
5541
private OkHttpClient okHttpClient;
5642
private boolean hasExecuted;
5743

Parse/src/main/java/com/parse/ParseObject.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,6 @@ private ParseRESTObjectCommand currentSaveEventuallyCommand(
13441344
state,
13451345
objectJSON,
13461346
sessionToken);
1347-
command.enableRetrying();
13481347
return command;
13491348
}
13501349

@@ -1880,7 +1879,6 @@ public final Task<Void> deleteEventually() {
18801879
// See [1]
18811880
command = ParseRESTObjectCommand.deleteObjectCommand(
18821881
getState(), sessionToken);
1883-
command.enableRetrying();
18841882
command.setLocalId(localId);
18851883

18861884
runEventuallyTask = Parse.getEventuallyQueue().enqueueEventuallyAsync(command, ParseObject.this);

0 commit comments

Comments
 (0)