I have some questions when using http.Transport to setup http client
Assume we have MaxIdleConns=10
, MaxIdleConnsPerHost=2
, five different hosts each of which has two keep-live connections and that means the number of connections reach MaxIdleConns
.
By the way, if I have an server using http.ListenAndServe
, how to configure it, like when to close keep-live connections? I would be grateful if there were any example codes.
If you are using the default Go HTTP client, it will throw an error "too many open files error"
when you reach the maximum connection limit during high frequency API calls. This happens because, the default HTTP client won't close the connections once created. In order to solve this issue, you have to create a custom HTTP client and set a timeout interval.
var netTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
var netClient = &http.Client{
Timeout: time.Second * 10,
Transport: netTransport,
}
response, _ := netClient.Get(url)
Refer this link for more details, refer this link https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779