How to disable retries for one connection with OkHttp?

okhttp retry
retryandfollowupinterceptor
http request automatic retries
okhttp queue size
okhttp slow
okhttp keep-alive
okhttp multiple requests
more than 3 retries encountered while sending the request

I'm using OkHttp and want to disable connection retries on a particular api call. Is this the correct way to do it?:

mMyGlobalClient = new OkHttpClient();

....

public void makeConnection(...) {

    OkHttpClient client = null;
    if (disableRetries) {
        OkHttpClient clone = mMyGlobalClient.clone();
        clone.setRetryOnConnectionFailure(false);
        client = clone;
    } else {
        client = mMyGlobalClient;
    }

    client.newCall(...);
}

The idea comes from this post:

https://github.com/square/okhttp/pull/1259#issue-53157152

Most applications won't want to disable retry globally. Instead, use clone() to get an OkHttpClient for a specific, non-idempotent request, then configure that client with the setting.

The reason I want to disable retries for this one call is because it can be destructive if it gets handled twice by my server:

https://github.com/square/okhttp/pull/1259#issuecomment-68430264

Thank you

Yes, this is the right way.

By the way, if you don't mind, you could write it a little simpler

mMyGlobalClient = new OkHttpClient();

....

public void makeConnection(...) {

    OkHttpClient client = null;
    if (disableRetries) {
        client = mMyGlobalClient.clone();
        client.setRetryOnConnectionFailure(false);
    } else {
        client = mMyGlobalClient;
    }

    client.newCall(...);
}

OkHttp is quietly retrying requests. Is your API ready?, OkHttp will potentially repeat your requests on a slow/unreliable request, but do recover if we encounter a timeout building a connection). You can only disable request retrying globally for the whole OkHttpClient instance. Use-case I need to make API-Requests via HTTPS from an Android App with reused sockets to avoid overhead for SSL-handshake etc. Though the requests are idempotent, my app relies on the user seeing the result of the request: the first req

In addition, I have solved the issue in retrofit2.

To prevent retry you have to use .retryOnConnectionFailure(false) method with OkHttpClient.

Sample code:

OkHttpClient okHttpClient= null;
        okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(2, TimeUnit.MINUTES)
                .readTimeout(2, TimeUnit.MINUTES)
                .writeTimeout(2, TimeUnit.MINUTES)
                .retryOnConnectionFailure(false)
                .cache(null)//new Cache(sContext.getCacheDir(),10*1024*1024)
                .build();

In previous version have some bugged and fixed after retrofit:2.1.0

So you should use updated version of retrofit2 and okhttp3:

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.okhttp3:okhttp:3.4.1' 

Issue discussion:

https://github.com/square/okhttp/pull/1259#issuecomment-68430264

Default behaviour of retryOnConnectionFailure is dangerous for , In v2 the request was always dropped, in v3 the connection is retried unless swankjesse added a commit that referenced this issue on Apr 12, 2016 OkHttp had problems with silent retries on POST-Requests #401. Closed. I am using Retrofit/OkHttp (1.6) in my Android project. I don't find any request retry mechanism built-in to either of them. On searching more, I read OkHttp seems to have silent-retries. I don't see that happening on any of my connections (HTTP or HTTPS). How to configure retries with okclient ?

use call.cancel();

with final Call call = client.newCall(request);

If you want to limit the retries, you can use :

if (responseCount(response) >= 3) {
    call.cancel(); // If we've failed 3 times, give up.
}

Okhttp 3.10.0 Multiple Concurrent POST/PUT Retries · Issue #4253 , OkHttp won't retry unless there's a connectivity problem on a recycled figuring out how to evict staled connection out of the pool and retry one more don't look at the response to do any checking if they should stop retrying. You can only disable request retrying globally for the whole OkHttpClient instance. This is done by using the OkHttpClient.Builder and setting retryOnConnectionFailure to false .

OkHttp is quietly retrying requests. Is your API ready? – Inloop , But anyway - even you disable request retrying for POST, HTTP by default is not "​reliable" You could send one single POST request to the server and it would be by the server but the answer would never get back to you (connection failed). Most applications won't want to disable retry globally. Instead, use clone () to get an OkHttpClient for a specific, non-idempotent request, then configure that client with the setting. Closes #1043. swankjesse added a commit that referenced this issue on Dec 30, 2014. New API to disable retry for an OkHttpClient.

Change Log, To enable platform-specific TLS features OkHttp must detect whether it's running in a Fix: Retry automatically when incorrectly sharing a connection among  OkHttp is the recommend HttpClient that’s used inside the Retrofit Networking Library. We’ll look into this in the next tutorial. We’ve added three buttons in the layout to invoke each of the methods, postRequest(), run() and the AsyncTask wrapper class.

OkHttpClient - OkHttp, OkHttp performs best when you create a single OkHttpClient instance and reuse it for all of your HTTP calls. This is because each client holds its own connection  Most applications won't want to disable retry globally. Instead, use clone () to get an OkHttpClient for a specific, non-idempotent request, then configure that client with the setting. Closes #1043. swankjesse force-pushed the jwilson_1230_fail branch from 162fbfb to 786e3b3 on Dec 30, 2014.

Comments
  • if we wanna get nitpicky, the null assignment is also useless, since that clone call is not long-running. :p
  • Yeah was just trying to be as clear as possible for the Q