I was wondering how many seconds should I set to my retrofit client.
- How many seconds should I use as default timeout?
- What is the default timeout for OkHttp/Retrofit, should we let default values?
I was wondering how many seconds should I set to my retrofit client.
Retrofit code snippet: (if you don't provide an OkHttpClient):
okhttp3.Call.Factory callFactory = this.callFactory;
if (callFactory == null) {
callFactory = new OkHttpClient();
}
OkHttp code snippet:
connectTimeout = 10_000;
readTimeout = 10_000;
writeTimeout = 10_000;
They are using different values. For example for feedback related they use:
public class FeedbackConstants{
public static final int FEEDBACK_CONNECT_TIMEOUT_MS = 15000;
public static final int FEEDBACK_READ_TIMEOUT_MS = 15000;
public static final int GOOGLE_API_CLIENT_CONNECTION_TIMEOUT_S = 10;
}
They are using Volley and you can take a look at some timeouts there as well. And yes they look short.
/** The default socket timeout in milliseconds */
public static final int DEFAULT_TIMEOUT_MS = 2500;
In a different http client they give you some clues about what they consider is a short and reasonable short timeout.
/**
* Default 2s, deliberately short. If you need longer, you should be using
* {@link AsyncHttpClient} instead.
*/
protected int connectionTimeout = 2000;
/**
* Default 8s, reasonably short if accidentally called from the UI thread.
*/
protected int readTimeout = 8000;
Source:
OkHttpClient defaultClient() {
OkHttpClient client = new OkHttpClient();
client.setConnectTimeout(15, TimeUnit.SECONDS);
client.setReadTimeout(15, TimeUnit.SECONDS);
client.setWriteTimeout(15, TimeUnit.SECONDS);
return client;
}
I'am using it like this in my RetrofitApiClient . okhttp version 3.4.1
public class RetrofitApiClient {
private static OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.connectTimeout(15, TimeUnit.SECONDS)
.readTimeout(15L, TimeUnit.SECONDS)
.writeTimeout(15L, TimeUnit.SECONDS);
public void someMethod() {
OkHttpClient client = httpClient.build();
}
}