How to set timeout for http.Get() requests in Golang?

后端 未结 7 1452
刺人心
刺人心 2020-12-22 16:15

I\'m making a URL fetcher in Go and have a list of URLs to fetch. I send http.Get() requests to each URL and obtain their response.

resp,fetch_e         


        
7条回答
  •  情深已故
    2020-12-22 16:52

    To add to Volker's answer, if you would also like to set the read/write timeout in addition to the connect timeout you can do something like the following

    package httpclient
    
    import (
        "net"
        "net/http"
        "time"
    )
    
    func TimeoutDialer(cTimeout time.Duration, rwTimeout time.Duration) func(net, addr string) (c net.Conn, err error) {
        return func(netw, addr string) (net.Conn, error) {
            conn, err := net.DialTimeout(netw, addr, cTimeout)
            if err != nil {
                return nil, err
            }
            conn.SetDeadline(time.Now().Add(rwTimeout))
            return conn, nil
        }
    }
    
    func NewTimeoutClient(connectTimeout time.Duration, readWriteTimeout time.Duration) *http.Client {
    
        return &http.Client{
            Transport: &http.Transport{
                Dial: TimeoutDialer(connectTimeout, readWriteTimeout),
            },
        }
    }
    

    This code is tested and is working in production. The full gist with tests is available here https://gist.github.com/dmichael/5710968

    Be aware that you will need to create a new client for each request because of the conn.SetDeadline which references a point in the future from time.Now()

提交回复
热议问题