Home > Back-end >  The network is slowing down the request or reading the response body
The network is slowing down the request or reading the response body

Time:12-23

I'm writing a client. What action should be protected by timeout? Get request resp, err := http.Get(fileURL) or read the response body n, err = resp.Body.Read(chunk). Which of these actions can be affected by the network?

CodePudding user response:

The simplest form will cover the timeout for the dial and reading the body. (If a connection is not reused)

c := &http.Client{
    Timeout: 15 * time.Second,
}
resp, err := c.Get("https://foo.bar/")

These are all http client timeouts I know.

c := &http.Client{
    Transport: &http.Transport{
        Dial: (&net.Dialer{
                Timeout:   30 * time.Second,
                KeepAlive: 30 * time.Second,
        }).Dial,
        TLSHandshakeTimeout:   10 * time.Second,
        ResponseHeaderTimeout: 10 * time.Second,
        ExpectContinueTimeout: 1 * time.Second,
    }
}

resp, err := c.Get("https://foo.bar/")
  • Related