0

I have a client that connects to a server to read some response. The server takes around 5 minutes to respond to a particular request when I use Postman to execute the request.

I am writing this client in Go language and executing the following code to set a timeout of 10 minutes.

_client := &http.Client{
        Timeout: 10 * time.Minute,
}

resp, err := _client.Post(c.Url, "application/json", r)

However, the request terminates after 2 minutes with an error. The error just says EOF.

I tried setting the timeout to 15 seconds to check if the config works and the request terminates in 15 seconds as expected.

How can I make sure that the timeout is 10 minutes?

Kashif Nazar
  • 20,775
  • 5
  • 29
  • 46
  • 1
    What exactly is the error? – JimB Mar 17 '17 at 14:55
  • Possible duplicates: [How to set timeout for http.Get() requests in Golang?](http://stackoverflow.com/questions/16895294/how-to-set-timeout-for-http-get-requests-in-golang); and [HTTP client, idle timeout](http://stackoverflow.com/questions/27784521/http-client-idle-timeout). Do they solve your problem? – icza Mar 17 '17 at 14:57
  • 1
    @KashifNazar: EOF means the server closed the connection for some reason. Are you sure you've written the complete request? Is `r` valid, or is it just blocking the request body? – JimB Mar 17 '17 at 20:41
  • @kashif the problem is not at your end it is at your server end. your server is not handling the file operations well, hence you are getting EOF – ritzz.soni Mar 18 '17 at 06:10

3 Answers3

0

I've had the same problem but there my code was running in GAE which canceled the request after 2 min with no operation. So if you have total control over where your code runs you should be able to specify the timeout time in client.Timeout

See here

Zanndorin
  • 360
  • 3
  • 15
0

Here’s a simple way to set timeouts for an HTTP request in Go:

client := &http.Client{Timeout: 10*time.Second}
resp, err := client.Get("https://freshman.tech")

The above snippet sets a timeout of 10 seconds on the HTTP client. So any request made with the client will timeout after 10 seconds if it does not complete in that time.

Before using the response from the request, you can check for a timeout error using the os.IsTimeout() method. It returns a boolean indicating whether the error is known to report a timeout that occurred.

client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Get(url)
if err != nil {
    if os.IsTimeout(err) {
        // A timeout error occurred
        return
    }

    // This was an error, but not a timeout
}

// use the response

Another method involves using the net.Error interface along with a type assertion, but I prefer os.IsTimeout() due to its simplicity.

client := &http.Client{Timeout: 10 * time.Second}
resp, err := client.Get(url)
if err, ok := err.(net.Error); ok && err.Timeout() {
    // A timeout error occurred
} else if err != nil {
    // This was an error, but not a timeout
}
Shubham K.
  • 93
  • 1
  • 2
  • 13
-1

The real problem here is that you are using HTTP in a way implementers do not want you to. You can read more about your problem and the best practice to solve it here. The short answer is that you are better off responding quickly to the client saying the job is running along with the url to find the result when it is done.

Liam Kelly
  • 3,524
  • 1
  • 17
  • 41