Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.59k stars 593 forks source link

No way to set read timeout in Unirest #389

Closed brandensam26 closed 3 years ago

brandensam26 commented 3 years ago

Hi,

Recently, I'm facing read timeouts issues in rest API calls.

I've observed that when it takes more than 10s to get the response it throws Caused by: com.mashape.unirest.http.exceptions.UnirestException: java.net.SocketTimeoutException: Read timed out

I see that I can set connection timeout and socket timeout in Unirest. Increasing the connection and socket timeout did not solve this issue. Is there a way to increase the read timeout in Unirest?

ryber commented 3 years ago

Well, the first thing I would ask is to upgrade. if you still have the mashape namespace, that version is super old and has some issues.

brandensam26 commented 3 years ago

@ryber thanks for the suggestion. I've already upgraded unirest to the latest version.

But, is there a way to set/control the read timeout in unirest?

Just a note: I'm using the unirest along with springboot 2.3

ryber commented 3 years ago

It depends on what you mean by "read timeout". There is no limit in unirest for total read time, you could spend hours downloading some gigantic file, and as long as bytes are moving you're ok. The SocketTimeoutException is thrown when you exceed the timout for a single socket read. So in your case, manipulating the socket timeout is the right thing to play with. I would say that taking 10 seconds for a single socket read is pretty bad and may be an indication of network instability.

brandensam26 commented 3 years ago

@ryber Thank you so much for your valuable inputs. In my case, the socket read time can take up to 15 seconds. So wanted to increase the read timeout. I've upgraded Unirest to the latest version and the app is now under testing. I'll also check if there is any network instability in my environment.

ryber commented 3 years ago

@brandensam26 was this issue resolved for you?

elipongr commented 8 months ago

Why is ifFailure not triggered in case of a SocketTimeoutException?

ryber commented 8 months ago

ifFailure gets called in the case of a HTTP failure or a failure to parse the results into the desired body, you will note that it accepts a HttpResponse<T>, in the case of a SocketTimeoutException there is no HttpResponse. Now we could provide a dummy response, but it would not have any status code or anything. This is actually done in the async client, I'll look into doing something similar but it might be confusing for users.

ryber commented 8 months ago

Oh wait, I remember, so you can change this behavior, it does this because it has always done this and changing it at this point is a breaking change. BUT, if you provide a Interceptor and override onFail there, its default behavior is to throw but you can change it to return a FailedResponse. I might make that the default behavior but will probably wait until I have a few more breaking changes and do them all at once

elipongr commented 8 months ago

So what is your suggested fix do deal with SocketTimeoutException meanwhile? Could you maybe provice a code example?