Skip to content

Commit 2a2395d

Browse files
Tsvetan NachevTsvetan Nachev
Tsvetan Nachev
authored and
Tsvetan Nachev
committed
parse-community#498 extends Parse.Configuration with means to specify default socketTimeout and max number of retries
1 parent e797ec8 commit 2a2395d

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,20 @@ public static final class Builder {
5353
private String clientKey;
5454
private String server = "https://api.parse.com/1/";
5555
private boolean localDataStoreEnabled;
56+
private int socketTimeout = 10 * 1000; //10 seconds
57+
private int maxRetries = 4;
5658
private List<ParseNetworkInterceptor> interceptors;
5759

60+
public Builder socketTimeout(int socketTimeout) {
61+
this.socketTimeout = socketTimeout;
62+
return this;
63+
}
64+
65+
public Builder maxRetries(int maxRetries) {
66+
this.maxRetries = maxRetries;
67+
return this;
68+
}
69+
5870
/**
5971
* Initialize a bulider with a given context.
6072
*
@@ -210,13 +222,17 @@ public Configuration build() {
210222
/* package for tests */ final String server;
211223
/* package for tests */ final boolean localDataStoreEnabled;
212224
/* package for tests */ final List<ParseNetworkInterceptor> interceptors;
225+
/* package for tests */ final int socketTimeout;
226+
/* package for tests */ final int maxRetries;
213227

214228
private Configuration(Builder builder) {
215229
this.context = builder.context;
216230
this.applicationId = builder.applicationId;
217231
this.clientKey = builder.clientKey;
218232
this.server = builder.server;
219233
this.localDataStoreEnabled = builder.localDataStoreEnabled;
234+
this.socketTimeout = builder.socketTimeout;
235+
this.maxRetries = builder.maxRetries;
220236
this.interceptors = builder.interceptors != null ?
221237
Collections.unmodifiableList(new ArrayList<>(builder.interceptors)) :
222238
null;
@@ -382,6 +398,9 @@ public static void initialize(Configuration configuration) {
382398
// isLocalDataStoreEnabled() to perform additional behavior.
383399
isLocalDatastoreEnabled = configuration.localDataStoreEnabled;
384400

401+
ParsePlugins.socketOperationTimeout = configuration.socketTimeout;
402+
ParseRequest.maxNetworkRetries = configuration.maxRetries;
403+
385404
ParsePlugins.Android.initialize(configuration.context, configuration.applicationId, configuration.clientKey);
386405

387406
try {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@
6565
/* package */ File cacheDir;
6666
/* package */ File filesDir;
6767

68+
/* package */ static int socketOperationTimeout = 10 * 1000; //10 seconds
69+
6870
private ParsePlugins(String applicationId, String clientKey) {
6971
this.applicationId = applicationId;
7072
this.clientKey = clientKey;
@@ -79,9 +81,8 @@ private ParsePlugins(String applicationId, String clientKey) {
7981
}
8082

8183
/* package */ ParseHttpClient newHttpClient() {
82-
int socketOperationTimeout = 10 * 1000; // 10 seconds
8384
return ParseHttpClient.createClient(
84-
socketOperationTimeout,
85+
socketOperationTimeout,
8586
null);
8687
}
8788

@@ -171,7 +172,6 @@ private Android(Context context, String applicationId, String clientKey) {
171172
@Override
172173
public ParseHttpClient newHttpClient() {
173174
SSLSessionCache sslSessionCache = new SSLSessionCache(applicationContext);
174-
int socketOperationTimeout = 10 * 1000; // 10 seconds
175175
return ParseHttpClient.createClient(
176176
socketOperationTimeout,
177177
sslSessionCache);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static ParseRESTCommand fromJSONObject(JSONObject jsonObject) {
188188

189189
// TODO(grantland): But we don't disable retries by default...
190190
/* package */ void enableRetrying() {
191-
setMaxRetries(DEFAULT_MAX_RETRIES);
191+
setMaxRetries(ParseRequest.maxNetworkRetries);
192192
}
193193

194194
private static String createUrl(String httpPath) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ private static ThreadPoolExecutor newThreadPoolExecutor(int corePoolSize, int ma
6767
new LinkedBlockingQueue<Runnable>(MAX_QUEUE_SIZE), sThreadFactory);
6868

6969
protected static final int DEFAULT_MAX_RETRIES = 4;
70+
/* package */ static int maxNetworkRetries = DEFAULT_MAX_RETRIES;
7071
/* package */ static final long DEFAULT_INITIAL_RETRY_DELAY = 1000L;
7172

7273
private static long defaultInitialRetryDelay = DEFAULT_INITIAL_RETRY_DELAY;
@@ -78,7 +79,11 @@ public static long getDefaultInitialRetryDelay() {
7879
return defaultInitialRetryDelay;
7980
}
8081

81-
private int maxRetries = DEFAULT_MAX_RETRIES;
82+
/**
83+
* Use maxNetworkRetries as default value for each request.
84+
* This allows the value to be configured when Parse is initialized, rather then using a hardcoded value
85+
*/
86+
private int maxRetries = ParseRequest.maxNetworkRetries;
8287

8388
/* package */ ParseHttpRequest.Method method;
8489
/* package */ String url;

0 commit comments

Comments
 (0)