From 65e177a9a5646e836dc7d56d3e60f88543d5fb68 Mon Sep 17 00:00:00 2001 From: vojtasmrcek Date: Tue, 14 May 2019 17:22:10 +0200 Subject: [PATCH] Add connection pool that doesn't keep failed sockets --- .../wordpress/android/networking/GravatarApi.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/WordPress/src/main/java/org/wordpress/android/networking/GravatarApi.java b/WordPress/src/main/java/org/wordpress/android/networking/GravatarApi.java index d34527b52f70..0ceb53859ce1 100644 --- a/WordPress/src/main/java/org/wordpress/android/networking/GravatarApi.java +++ b/WordPress/src/main/java/org/wordpress/android/networking/GravatarApi.java @@ -11,9 +11,11 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.TimeUnit; import okhttp3.Call; import okhttp3.Callback; +import okhttp3.ConnectionPool; import okhttp3.Interceptor; import okhttp3.MultipartBody; import okhttp3.OkHttpClient; @@ -22,6 +24,7 @@ public class GravatarApi { public static final String API_BASE_URL = "https://api.gravatar.com/v1/"; + private static final int DEFAULT_TIMEOUT = 15000; public interface GravatarUploadListener { void onSuccess(); @@ -31,7 +34,14 @@ public interface GravatarUploadListener { private static OkHttpClient createClient(final String accessToken) { OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder(); - + // This should help with recovery from the SocketTimeoutException + // https://github.com/square/okhttp/issues/3146#issuecomment-311158567 + httpClientBuilder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS) + .retryOnConnectionFailure(true) + .readTimeout(DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS) + .connectionPool( + new ConnectionPool(0, 1, TimeUnit.NANOSECONDS) + ); // // uncomment the following line to add logcat logging // httpClientBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));