Skip to content
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

Add connection pool that doesn't keep failed sockets #9862

Merged
merged 1 commit into from
May 21, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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));

Expand Down