0

I perform an HTTP request from a client with an explicit http.Client.Timeout

client := http.Client{
    Timeout: timeout, // 5 seconds
}

httpResponse, err := client.Do(httpRequest)

But this client will perform a series of redirects (I'm not sure how many).

From what I understand, each redirect will restart the timeout, so the timeout of 5 seconds will be five-seconds * num-of-redirects.

Is it possible to rather pass in the timeout inside a context.Context?

ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
// do I need cancel?
httpRequest.WithContext(ctx)
httpResponse, err := client.Do(httpRequest)

Would this encompass the request and all redirects in the same timeout? Or do I misunderstand redirects/timeouts/contexts?

1
  • In the docs they say you need to call the cancel function no matter what. They have an example showing a defer cancel() to make sure of that. Commented Jun 3, 2020 at 18:08

1 Answer 1

1

It does not look like each redirect will reset the http.Client time out.

From the net/http documentation:

// Timeout specifies a time limit for requests made by this
// Client. The timeout includes connection time, any
// redirects, and reading the response body.

https://golang.org/pkg/net/http/

Sign up to request clarification or add additional context in comments.

1 Comment

It seems it work work as well, see this related question for an example and further explanation: stackoverflow.com/questions/43321894/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.