Skip to content

Commit 65e177a

Browse files
committed
Add connection pool that doesn't keep failed sockets
1 parent dbf385c commit 65e177a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

WordPress/src/main/java/org/wordpress/android/networking/GravatarApi.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@
1111
import java.io.IOException;
1212
import java.util.HashMap;
1313
import java.util.Map;
14+
import java.util.concurrent.TimeUnit;
1415

1516
import okhttp3.Call;
1617
import okhttp3.Callback;
18+
import okhttp3.ConnectionPool;
1719
import okhttp3.Interceptor;
1820
import okhttp3.MultipartBody;
1921
import okhttp3.OkHttpClient;
@@ -22,6 +24,7 @@
2224

2325
public class GravatarApi {
2426
public static final String API_BASE_URL = "https://api.gravatar.com/v1/";
27+
private static final int DEFAULT_TIMEOUT = 15000;
2528

2629
public interface GravatarUploadListener {
2730
void onSuccess();
@@ -31,7 +34,14 @@ public interface GravatarUploadListener {
3134

3235
private static OkHttpClient createClient(final String accessToken) {
3336
OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
34-
37+
// This should help with recovery from the SocketTimeoutException
38+
// https://github.com/square/okhttp/issues/3146#issuecomment-311158567
39+
httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
40+
.retryOnConnectionFailure(true)
41+
.readTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS)
42+
.connectionPool(
43+
new ConnectionPool(0, 1, TimeUnit.NANOSECONDS)
44+
);
3545
// // uncomment the following line to add logcat logging
3646
// httpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
3747

0 commit comments

Comments
 (0)