Retrofit first request takes exact OkHttpClient timeout
Asked Answered
E

2

6

I'm using Retrofit and OkHttpClient to build a Rest API on Android.

Some time ago, i had noticed the very first request made by the api always take way longer to process than all others... At begining i didn't care because it was an acceptable time.

But suddenly the request time jumped to 60 SECONDS

All this time is waste client side, since monitoring the server i see that the processing time takes less than 1 seconds...

I was wondering what change I made that could cause such high impact, then I realized that i changed the connection timeout of OkHttp.

I had changed the value from 10 seconds to 60 seconds just for testings...

I made some experiments setting the connectTimeout to many others values and ALWAYS the first request takes the time sighly above the timeout

Does anyone know what may cause this weird behavior? how to solve it?

PS. I needed to test the api on a desktop and this problem didnt occur, i mean it is only happening on android devices [i tried several] what is the cause?

Epizoic answered 25/9, 2018 at 16:31 Comment(4)
Hi, I have the same issue, really strange. It occurs only in some networks. Did you find some solution?Stribling
@yital9, no... i opened an issue in retrofit github many people agreed with problem but they dont recognize it as a bugEpizoic
Do we have to probably open an issue in okhttp repo? As I understood it seems to be an okhttp problem, doesn't it?Stribling
I still can't explain why and I don't have any workaround, but it seems to happen, if some api with cloudflare is requested.Stribling
B
1

https://medium.com/inloopx/okhttp-is-quietly-retrying-requests-is-your-api-ready-19489ef35ace

It is likely failing on the first few requests and timing out. OkHTTP is automatically retrying.

builder.retryOnConnectionFailure(false);

Try turning off the auto retry and see what happens.

https://www.reddit.com/r/gfycat/comments/5y2dkm/psa_for_android_devs_how_to_work_around_ipv6/

It is likely an ipv4 vs ipv6 problem. This code prioritizes ipv4 addressses.

Blackhead answered 16/5, 2019 at 16:26 Comment(0)
C
0

You could configure an EventListener in your OkHttpClient and use that to see how the time is spent.

Cirone answered 25/9, 2018 at 22:18 Comment(2)
github.com/square/okhttp/blob/master/okhttp-logging-interceptor/…Iconium
@YuriSchimke Link is now moved to square.github.io/okhttp/features/eventsScenario

© 2022 - 2024 — McMap. All rights reserved.