OkHttp/Retrofit default timeout

后端 未结 5 1866
攒了一身酷
攒了一身酷 2020-12-23 13:47

I was wondering how many seconds should I set to my retrofit client.

  1. How many seconds should I use as default timeout?
  2. What is the default timeout fo
相关标签:
5条回答
  • 2020-12-23 14:06

    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();
            }
    }
    
    0 讨论(0)
  • 2020-12-23 14:09
    1. It shouldn't take forever and not too short. IMHO, it should be between 10s and 30s.
    2. Default connect time out setting that Retrofit gives you (if you haven't specified http client explicitly) is 15 seconds.

    Source:

    OkHttpClient defaultClient() {   
        OkHttpClient client = new OkHttpClient();
        client.setConnectTimeout(15, TimeUnit.SECONDS);
        client.setReadTimeout(15, TimeUnit.SECONDS);
        client.setWriteTimeout(15, TimeUnit.SECONDS);
        return client;
    }
    
    1. I get this feeling Google is using 30 seconds. Not sure.
    0 讨论(0)
  • 2020-12-23 14:14

    https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/OkHttpClient.kt#L471

    https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/OkHttpClient.kt#L203

    internal var connectTimeout = 10_000
    internal var readTimeout = 10_000
    internal var writeTimeout = 10_000
    
    0 讨论(0)
  • 2020-12-23 14:28
    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. As of OkHttp 3.4.1 and Retrofit 2.1.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):

      okhttp3.Call.Factory callFactory = this.callFactory;
      if (callFactory == null) {
        callFactory = new OkHttpClient();
      }
    

    OkHttp code snippet:

      connectTimeout = 10_000;
      readTimeout = 10_000;
      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;
    
    0 讨论(0)
  • 2020-12-23 14:33

    You can use

    OkHttpClient okHttpClient = new OkHttpClient.Builder()  
            .connectTimeout(1, TimeUnit.MINUTES)
            .readTimeout(30, TimeUnit.SECONDS)
            .writeTimeout(15, TimeUnit.SECONDS)
            .build();
    
    Retrofit.Builder builder = new Retrofit.Builder()  
            .baseUrl("http://10.0.2.2:3000/")
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create());
    

    for more details go to: https://futurestud.io/tutorials/retrofit-2-customize-network-timeouts

    0 讨论(0)
提交回复
热议问题