danielgomezrico danielgomezrico - 1 year ago 214
Android Question

OkHttp/Retrofit default timeout

I have 3 questions about mobile timeouts:

  1. For mobile we should use how much time for Http timeouts?

  2. What is the default timeout for OkHttp/Retrofit?

  3. Which timeouts use google on requests?

Answer Source
  1. There's no a magic value and depends on expectations on your backend. If someone tells you 5s is a good value and you are having 8s average on one of your endpoints at max load times, then 8s is not working for you. As general values I've seen that below 10s is consider short and between 10s and 20s being common.

  1. For OkHttp 3 and Retrofit 2.0, the default value for OkHttp is 10 seconds. Retrofit relies on the OkHttp default value.

Retrofit code snippet: (if you don't provide an OkHttpClient):

OkHttpClient client = this.client;
      if (client == null) {
        client = new OkHttpClient();

OkHttp code snippet:

private int connectTimeout = 10_000;
private int readTimeout = 10_000;
private int writeTimeout = 10_000;

  1. The only Google App I have access to is the Google IO app.

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;